GNU Makefile-具有多个具有一个依赖项的目标的模式规则将忽略除第一个目标以外的所有目标 [英] GNU Makefile - Pattern rule with multiple targets with one dependency ignores all targets but the first

查看:57
本文介绍了GNU Makefile-具有多个具有一个依赖项的目标的模式规则将忽略除第一个目标以外的所有目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使语言成为目标.特别是:我有一个源文件,我想创建不同的对象,然后将它们添加到相应的语言文件夹中.编译器将获得该单一源文件在C-Flags中的不同.只要我以静态方式使用它,它就可以正常工作.

I want to make a language depend target. In Particular: I have one source-file and I want to create different Objects which where add to the corresponding language folder. That single source file will differ in the C-Flags, the compiler will get. As long as I used it in a static way, it works quite fine.

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

现在我想,如果它更具动态性,那将很棒,以防万一我要添加新的语言依赖文件.因此,我使用通配符,如下所示:

Now I thought, it would be great if it is a bit more dynamic, in case i'll add a new language depending file. So I used a wildcard as followed:

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

但是现在它只是确定第一个目标,而忽略其余目标. Make-Debug打印以下内容:

But now it just make the first target and ignores the rest. The Make-Debug prints the following thing:

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

以防万一:不,对象不存在.因此,没有目标,而是现有的依赖关系,因此make应该执行规则.

Just in case: No, the objects do not exist. So there is no target, but an existing dependencie, so make should execute the rules.

找到了该问题的解决方案.

Found a solution for that Problem.

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

推荐答案

模式规则的工作方式不同于隐式规则.而诸如

Pattern rules work differently than implicit rules. While an implicit rule such as

a b c: d
      command

等同于较长的符号

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

这不适用于模式规则.明确要求具有多个目标的模式规则,以通过一次调用command来构建其目标的全部.因此,您将不得不写

this does NOT hold for pattern rules. Pattern rules with multiple targets are explicitly required to build all of their targets in a single invocation of command. Thus you would have to write

$ 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模式规则简介中找到:

The relevant documentation is found in 10.5.1 Introduction to Pattern Rules of the GNU make manual:

模式规则可能有多个目标.与普通规则不同,这不会像具有相同先决条件和配方的许多不同规则一样起作用.如果阵列规则有多个目标,请让该规则的配方负责所有目标.配方仅执行一次即可构成所有目标.在搜索与目标匹配的模式规则时,除了与需要规则的目标匹配的规则以外,其他规则的目标模式都是偶然的:仅担心为当前存在问题的文件提供配方和前提条件.但是,运行该文件的配方时,其他目标将被标记为自己已更新.

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.

这篇关于GNU Makefile-具有多个具有一个依赖项的目标的模式规则将忽略除第一个目标以外的所有目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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