gpt4 book ai didi

makefile - GNU Makefile - 具有一个依赖项的多个目标的模式规则会忽略除第一个目标之外的所有目标

转载 作者:行者123 更新时间:2023-12-02 00:23:08 28 4
gpt4 key购买 nike

我想制作一个语言依赖目标。特别是:我有一个源文件,我想创建不同的对象,将其添加到相应的语言文件夹中。编译器将获得该单个源文件的 C-Flags 不同。只要我以静态方式使用它,它就工作得很好。

de/info.o en/info.o es/info.o : info.c
$(ECHO) (DEP) $< for $@

现在我想,如果它更加动态一点那就太好了,以防我添加一个新的语言依赖文件。所以我使用了通配符,如下所示:

de/%.o en/%.o es/%.o : %.c
$(ECHO) (DEP) $< for $@

但现在它只创建第一个目标并忽略其余的。 Make-Debug 打印以下内容:

Successfully remade target file `de/info.o'.
Considering target file `en/info.o'.
File `en/info.o' was considered already.

以防万一:不,这些对象不存在。因此没有目标,但有一个现有的依赖项,因此 make 应该执行规则。

编辑:找到了该问题的解决方案。

define FOO

$(1)/%.o : %.c
$(ECHO) $$< for $(1)

endef

$(foreach lang,$(LANGUAGE_LIST), $(eval $(call FOO,$(lang))))

灵感来源:http://www.gnu.org/software/make/manual/make.html#Eval-Function

最佳答案

模式规则的工作方式与隐式规则不同。虽然隐含规则如

a b c: d
command

相当于更长的符号

a: d
command
b: d
command
c: d
command

这不适用于模式规则。明确要求具有多个目标的模式规则在单次调用命令中构建其所有目标。因此你必须写

$ cat GNUmakefile
all: de/x.o en/x.o es/x.o

de/%.o: %.c
@echo $@ from $<
en/%.o: %.c
@echo $@ from $<
es/%.o: %.c
@echo $@ from $<
$ gmake
de/x.o from x.c
en/x.o from x.c
es/x.o from x.c

相关文档可以在 GNU make 手册的10.5.1 模式规则简介中找到:

Pattern rules may have more than one target. Unlike normal rules, this does not act as many different rules with the same prerequisites and recipe. If a pattern rule has multiple targets, make knows that the rule’s recipe is responsible for making all of the targets. The recipe is executed only once to make all the targets. When searching for a pattern rule to match a target, the target patterns of a rule other than the one that matches the target in need of a rule are incidental: make worries only about giving a recipe and prerequisites to the file presently in question. However, when this file’s recipe is run, the other targets are marked as having been updated themselves.

关于makefile - GNU Makefile - 具有一个依赖项的多个目标的模式规则会忽略除第一个目标之外的所有目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27836246/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com