在变量名中使用 % 模式

Use % pattern in variable name

我正在尝试编写一个对多个变量使用相同配方的 Makefile。变量被分成几个批次,需要单独构建,但使用相同的方法。

在下面的示例中,我希望能够使用相同的配方构建 output1.txtoutput2.txt 等。要注意的是,这些变量中的每一个都依赖于相应的变量 $(target1)$(target2) 等。我尝试使用 % 模式来扩展这些变量,但这没有用,因为 %最后展开。

我发现我可以使用递归变量修改变量调用,例如。 $($(subst 0,$(N),target0))。但是,除非已知号码,否则无法执行此操作。

有没有简单的方法可以做到这一点?

target1:=a_5_2 a_3_5 a_6_2 a_0_0 a_9_1
target2:=a_2_2 a_1_3 a_5_9 a_2_7 a_3_6
target3:=a_2_3 a_6_5 a_9_0 a_3_4 a_3_9
target4:=a_7_8 a_8_2 a_4_8 a_7_1 a_0_7

output%.txt: $($(subst 0,%,target0))
    cat $^ > $@  # do some actual stuff here

您可以使用 secondary expansion:

.SECONDEXPANSION:

output%.txt: $$(target$$*)
        ...

当然,您也可以将变量改为先决条件设置;大约是相同的输入量:

output1.txt : a_5_2 a_3_5 a_6_2 a_0_0 a_9_1
output2.txt : a_2_2 a_1_3 a_5_9 a_2_7 a_3_6
output3.txt : a_2_3 a_6_5 a_9_0 a_3_4 a_3_9
output4.txt : a_7_8 a_8_2 a_4_8 a_7_1 a_0_7

output%.txt:
        cat $^ > $@  # do some actual stuff here