将目标名称传递给makefile中的依赖项 [英] Passing target name to a dependency in makefile

查看:147
本文介绍了将目标名称传递给makefile中的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Makefile中具有以下规则:

If I have the following rule in a makefile:

$(OBJ)/%.o: $(SRC)/%.c
    $(CC) -c -o $@ $< $(CFLAGS)

每个与前缀./obj/和sufix .o匹配的文件都将其主干传递给%,因此我可以根据其名称提供一些依赖项.

Every file matching the prefix ./obj/ and sufix .o will have its stem passed to %, so I can provide some dependencies based on its name.

但是,假设我有这种规则,我会逐个指定我想要的目标:

But, suppose I have this kind of rule, which I specify one by one the targets I want:

OBJECTS=abc.o bca.o cba.o
$(OBJECTS): $(SRC)/%.c
    $(CC) -c -o $@ $< $(CFLAGS)

如何使%词干对正在执行的当前目标名称​​ make 起作用?仅使用%不起作用,$@也不起作用.

How do I make the % stem actually work for the current target name make is executing? Just using % doesn't work, neither $@.

请注意,我正在尝试将实际的目标名称写入其自己的依赖项.例如,当 make 执行abc.o的规则时,它将包括$(SRC)/abc.c以及它(类似于$(patsubst %.o, $(SRC)/%.c, MAGIC_TARGET_NAME_VARIABLE)).

Note that I'm trying to write the actual target name to its own dependency. For example, when make is executing the rule for abc.o, it would include $(SRC)/abc.c and just it (something like $(patsubst %.o, $(SRC)/%.c, MAGIC_TARGET_NAME_VARIABLE)).

推荐答案

您可以替换以下规则:

$(OBJECTS): $(SRC)/%.c

具有:

$(OBJECTS) : %.o : $(SRC)/%.c

如果仍要在配方的-o部分中添加$(OBJ),则需要将它们添加到配方中:

You will need to add the $(OBJ) to the -o part of the recipe if you still want them built there:

$(OBJECTS) : %.o : $(SRC)/%.c
     $(CC) -c -o $(OBJ)/$@ $< $(CFLAGS)

这篇关于将目标名称传递给makefile中的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆