【问题标题】:makefile variable assignment under a target目标下的makefile变量赋值
【发布时间】:2015-04-09 09:32:30
【问题描述】:

我在 Makefile 的目标中运行以下命令。

target1:
        <run some command which creates ids.log>
        $(eval id1 := $(shell cat ids.log | head -1))
        @echo id is $(id1)
        <some other command which uses the value $(id1)>

我的任务是获取“ids.log”中的第一行并将其保存在变量 id1 中,这样我就可以在此目标中接下来执行的命令中使用该值。

运行此命令时出现错误“cat: ids.log: No such file or directory”。看起来 $(eval .. ) 命令在 make 开始时执行,而不是作为目标 make 的一部分。而且由于在目标 make 完成之前不会创建 ids.log,因此我们会收到此错误。

谁能帮助我获取我执行的 shell 命令的值并将其放入变量中以供按需使用?

【问题讨论】:

    标签: variables makefile eval


    【解决方案1】:

    有一个单独的目标,首先创建ids.log,然后将其作为target 的依赖项。

    例如:

    ids.log:
        echo 1 > ids.log
    
    target: ids.log
        $(eval id1 := $(shell cat ids.log | head -1))
        @echo id is $(id1)
    

    【讨论】:

    • 非常感谢,西蒙。有用。有没有办法在不添加依赖项的情况下做到这一点?
    • 我猜你在后续命令中使用$(id1) 用反引号中的cat ids.log | head -1 替换,但我认为我在答案中给出的解决方案更干净。
    • @SimonGibbons 您好,感谢您的回答。我试过了,但echo $(shell whoami) &gt; whoami.log但没有用。知道如何从命令中获取目标的值吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多