【问题标题】:Conditionally appending to a variable inside a Makefile target有条件地附加到 Makefile 目标内的变量
【发布时间】:2010-01-27 17:16:43
【问题描述】:

我有一个看起来有点像这样的 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

我的想法是运行 make targetA 或 make targetB。他们俩都做非常相似的事情,但项目列表不同。问题是变量不是有条件地附加到,它总是附加到,这意味着我的输出总是“item1 itemA itemB”。

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

【问题讨论】:

    标签: variables makefile gnu-make


    【解决方案1】:
    LIST = item1
    
    .PHONY: targetMain targetA targetB
    
    targetMain:
    # DO all the work in here
        echo $(LIST)
    
    targetA: LIST+=itemA
    targetB: LIST+=itemB
    
    targetA targetB: targetMain
    

    【讨论】:

    • 稍微解释一下:这称为特定于目标的变量。它的工作方式是现在确实有两个 LIST 变量:一个用于targetA,一个用于targetB。 target: VARIABLE = value 语法是分配目标特定变量的方式。扩展两个LIST 变量中的哪一个取决于您所在的目标的命令脚本。请注意,正因为如此,特定于目标的变量只能从命令脚本内部引用(它们可以' 甚至不会在 prerequesites 行中被引用)。
    • 太好了,这似乎符合我的要求!
    • 谢谢,丹,我应该添加一些解释。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    相关资源
    最近更新 更多