有条件地附加到Makefile目标内的变量

bra*_*amp 3 variables makefile gnu-make

我有一个看起来有点像这样的GNU Makefile:

LIST = item1

.PHONY: targetMain targetA targetB preA preB

targetMain:
# DO all the work in here
    echo $(LIST)

targetA: preA targetMain
targetB: preB targetMain

preA:
LIST += itemA

preB:
LIST += itemB
Run Code Online (Sandbox Code Playgroud)

我的想法是运行make targetA或make targetB.他们两个都做了非常类似的事情,但有一个不同的项目列表.问题是变量没有有条件地附加到,它总是附加到,这意味着我的输出始终是"item1 itemA itemB".

我如何有条件地附加到变量?

小智 8

LIST = item1

.PHONY: targetMain targetA targetB

targetMain:
# DO all the work in here
    echo $(LIST)

targetA: LIST+=itemA
targetB: LIST+=itemB

targetA targetB: targetMain
Run Code Online (Sandbox Code Playgroud)

  • 一点解释:这称为特定于目标的变量.它的工作方式是现在真的有两个*`LIST`变量:一个用于targetA,一个用于targetB.`target:VARIABLE = value`语法是指定特定于目标的变量的方式.扩展了两个`LIST`变量中的哪一个取决于您所在的目标命令脚本.请注意,由于此原因,目标特定变量只能*从命令脚本中引用(它们不能在先决条件,甚至). (5认同)