GNU make 不构建目标

GNU make not building target

我想要以下规则:

foo_% bar_%:
        @echo "Complex building instructions for $@"

all: foo_xyz bar_xyz

然后 运行 'make all' 得到:

Complex building instructions for foo_xyz
Complex building instructions for bar_xyz

但是,因为 '%' 匹配同一个字符串两次 (xyz) 它只是 "built" 第一次,所以我得到的只是第一行。

有没有办法让 GNU make 在 '$@' 第二次不同时执行 'echo' 两次?毕竟,由于 $@ 不同,构建指令也不同 :/

如您所见,多个模式目标的行为与普通目标不同:Make 将考虑负责制作所有模式的配方。

解决这个问题的一种方法是使用定义:

define complex-rule
:
    @echo "Complex building instructions for $$@"
endef

$(eval $(call complex-rule,foo_%))
$(eval $(call complex-rule,bar_%))

all: foo_xyz bar_xyz

请注意,由于它将被 evaled,我将 $@ 转义为 $$@ 因为我希望它在食谱运行时展开,而不是在我 eval 它。另一方面,我没有转义 </code> 因为我希望第一个参数在 <code>call.

上扩展