使配方执行两次 [英] make recipe execute twice
问题描述
是否有一种方法可以多次执行配方? 在下面的SSCCE中,该配方仅执行一次:
Is there a way to execute a recipe more than once? In the SSCCE below it appears that the recipe is executed only once:
$ cat Makefile
.PHONY: a b
a: b b
b:
echo "beta"
$ make
echo "beta"
beta
推荐答案
一旦您阅读并理解了注释;-),我可以想到两种方法来运行两次配方:
Once you've read and understood the comments ;-), there are two ways I can think of to run a recipe twice:
@OliCharlesworth提到了第一个-在配方内使用循环:
@OliCharlesworth mentions the first - use a loop inside your recipe:
.PHONY: a b
a: b b
b:
for i in 1 2; do \
echo "beta" ;\
done
请注意,在配方中嵌入多行shell表达式时需要非常小心.除非您使用反斜杠终止行,否则make会将每行视为单独的shell调用,这对于循环无效.
Note you need to be quite careful when embedding multi-line shell expressions in recipes. Unless you terminate lines with backslashes, make will treat each line as a separate shell invocation, which wouldn't work for a loop.
另一种方法是复制b目标,以便两个副本具有相同的配方:
The other way is to duplicate your b target, so that both copies have the same recipe:
.PHONY: a b1 b2
a: b1 b2
b1 b2:
echo "beta"
这用相同的配方定义了b1
和b2
目标.然后a
取决于b1
和b2
,因此该配方被调用两次.请注意,不能保证b1
和b2
的调用顺序-如果您的-j因子大于1,则很可能会同时调用它们.
This defines the b1
and b2
targets with the same recipe. Then a
depends on both b1
and b2
, so the recipe gets called twice. Note that there is no guarantee of the order in which b1
and b2
will be invoked - and they'll likely be invoked concurrently if you have a -j factor greater than 1.
这篇关于使配方执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!