【问题标题】:Output multiline variable to a file with GNU Make使用 GNU Make 将多行变量输出到文件
【发布时间】:2011-09-02 09:13:43
【问题描述】:

我很难编写在文件中输出多行变量的 makefile 规则。

这是我的代码:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

VARS += $(VAR1)
VARS += $(VAR2)

all:
    echo "$(VARS)" > test

但是,由于我不知道的原因,回显未能告诉“未终止的引用字符串”。 我如何将文件中的每一行声明在单独的行中?

【问题讨论】:

    标签: makefile


    【解决方案1】:

    如果将变量导出到 shell 并将其作为 shell 变量引用,而不是 make 变量,那么运气会更好:

    define VAR1
        /dev d 755 - - - - -
    endef
    
    define VAR2
        /test d 777 - - - - -
        /test2 d 777 - - - - -
    endef
    
    define VARS
    $(VAR1)
    $(VAR2)
    endef
    export VARS
    
    all:
        echo "$$VARS" > test
    

    请注意对 makefile 的以下调整:

    • 我使用define 创建VARS,而不是一系列+= 赋值,这样更容易在VAR1VAR2 的值之间换行。
    • 我将export VARS 添加到您的makefile 中,以便将变量推送到环境中以进行shell 调用。
    • 我使用$$VARS 而不是$(VARS) 来取消引用它——这将扩展留给shell,而不是make,这将避免“未终止的引用字符串”错误。

    【讨论】:

    • 我怎么会错过呢? +1
    【解决方案2】:

    GNU make 4.0 增加了the ability to write files directly:

    define VAR1
        /dev d 755 - - - - -
    endef
    
    define VAR2
        /test d 777 - - - - -
        /test2 d 777 - - - - -
    endef
    
    define VARS :=
    $(VAR1)
    $(VAR2)
    endef
    
    all:
            $(file > test,$(VARS))
    

    请注意,您仍然需要使用define 来定义VARS,否则VAR1 的最后一行和VAR2 的第一行将显示在一行上。另外,不要在$(file ...) 构造中的逗号后加空格,否则输出中会出现前导空格!

    【讨论】:

    • 谢谢你——这正是我此时需要知道的,因为我在 Windows 上遇到了命令行参数限制,破坏了我的 make 构建......这很有帮助,因为没有调用传递参数的shell。干杯!
    【解决方案3】:

    看起来好像你得到了“未终止的引用字符串”,因为 Make 在单独的 shell 中执行配方的每一行,第一行是:

    echo "    /dev d 755 - - - - -
    

    这是我能想到的最好的解决方案(我承认这不是很好,但你违背了 Make 的原则)是将 VARS 传递给调用 $(info ...) 的子 Make:

    生成文件:

    define VAR1
        /dev d 755 - - - - -
    endef
    
    define VAR2
        /test d 777 - - - - -
        /test2 d 777 - - - - -
    endef
    
    define VARS
    $(VAR1)
    $(VAR2)
    endef
    
    export VARS
    
    all:
           $(MAKE) -f Makefile.print > test                                        
    

    Makefile.print:

    $(info $(VARS))
    
    .PHONY:all
    all:
            @# do nothing
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多