具有多个规则的Makefile共享同一配方 [英] Makefile with multiples rules sharing same recipe

查看:103
本文介绍了具有多个规则的Makefile共享同一配方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以编写带有多个规则的Makefile,每个规则定义自己的先决条件,并在不重复该食谱的情况下,以相同的食谱执行所有这些前提条件.示例:

I'd like to know if it's possible to write a Makefile with several rules, each one defining its own prerequisites and executing all of them the same recipe without duplicating the recipe. Example:

TARGETS= file1 file2 file3

all: $(TARGETS)

file1: dep1 dep2
file2: dep2 dep3 dep4
file3: dep2 dep1
    cat $^ > $@

谢谢!

推荐答案

是的,它的编写方式非常明显:

Yes, it is written in a rather obvious way:

TARGETS= file1 file2 file3

all: $(TARGETS)

file1: dep1 dep2
file2: dep2 dep3 dep4
file3: dep2 dep1

$(TARGETS):
    cat $^ > $@

UPD.

只是澄清一下.

UPD.

Just to clarify.

通用 make规则如下所示:

targets... : prerequisites...
    recipe
    ...

该规则的任何部分都可以省略.如果没有该配方,则可以在makefile中的任何位置填充先决条件列表,目标可以出现在多个规则语句的左侧.

Any part of the rule can be omitted. Without the recipe, one can populate the list of prerequisites anywhere in the makefile, a target can occur in the left hand side of multiple rule statements.

例如,以下内容等同于上面的示例(嗯,假设Makefile的设计正确,因此前提条件的顺序无关紧要):

For example, the following is equivalent to the example above (well, assuming that the Makefile is properly designed so that the order of prerequisites does not matter):

file1 file3       : dep1
file1 file2 file3 : dep2
file2             : dep3 dep4

与列出先决条件不同,每个目标最多可以有一个明确的配方.在食谱中,您可以使用自动变量来获取目标名称,列表先决条件等.

Unlike listing prerequisites, there can be at most one explicit recipe for each target. Inside the recipe you can use automatic variables to get the target name, the list of prerequisites, etc.

正如@Calmarius在评论中提到的,这不适用于样式规则,例如%.txt: %.foo. 目标中有多种模式,这意味着规则会一次一次产生所有这些目标.

As @Calmarius mentions in comments this doesn't apply to pattern rules, like %.txt: %.foo. Multiple patterns within targets mean that the rule produces all these targets at once.

此模式规则有两个目标:

This pattern rule has two targets:

%.tab.c %.tab.h: %.y
    bison -d $<

这说明配方bison -d x.y将同时制成x.tab.cx.tab.h. 如果文件foo取决于 文件parse.tab.oscan.o以及文件scan.o取决于 文件parse.tab.h,当更改parse.y时,配方bison -d parse.y 将只执行一次,并且两者的先决条件 parse.tab.oscan.o将被满足.

This tells make that the recipe bison -d x.y will make both x.tab.c and x.tab.h. If the file foo depends on the files parse.tab.o and scan.o and the file scan.o depends on the file parse.tab.h, when parse.y is changed, the recipe bison -d parse.y will be executed only once, and the prerequisites of both parse.tab.o and scan.o will be satisfied.

尽管可以在模式规则中定义多个先决条件(也就是说,前提是其目标包含%词干,否则它将是常规规则).

One may define multiple prerequisites in a pattern rule though (that is, provided that its targets contain a % stem, otherwise it would be a regular rule).

这篇关于具有多个规则的Makefile共享同一配方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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