在Makefile变量中使用`date`

use*_*834 3 makefile gnu-make

使用GNU Make 4.1,在我的Makefile中,我可以用这个创建一个时间戳日志文件::

flush_log:
        @echo "==> flush logs"
        cat file > file_`date +%FT%T%Z`.log
Run Code Online (Sandbox Code Playgroud)

但是谁应该:

file_`date +%FT%T%Z`.log
Run Code Online (Sandbox Code Playgroud)

在Makefile里面的var中,例如wc在它上面吗?

我试过(没有成功):

flush_log:
    @echo "==> flush logs"
    logfile:=file_$(shell date +%FT%T%Z).log
    cat my_log > $(logfile)
    echo `wc -l $(logfile)`
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

$ make flush_log
==> flush logs
logfile:=file_2016-12-24T20:09:52CET.log
/bin/sh: 1: logfile:=file_2016-12-24T20:09:52CET.log: not found
Makefile:7: recipe for target 'flush_log' failed
make: *** [flush_log] Error 127
$
Run Code Online (Sandbox Code Playgroud)

我遵循/sf/answers/1045760411/和Simply扩展变量的建议https://www.gnu.org/software/make/manual/html_node/Flavors.html#Flavors

Mad*_*ist 8

使用TAB字符缩进的makefile中的每一行(显示在规则语句之后)都是规则配方的一部分.规则的配方中的行被传递给shell进行处理,它们不会被make解析(扩展变量/函数引用除外).

所以你的行logfile:=file...被传递给shell并由shell解释...并且shell中没有有效的:=运算符,因此shell认为整行是单个单词并尝试运行具有该名称的程序,显然不存在.

您可能想要创建一个make变量,必须在配方之外完成,如下所示:

logfile := file_$(shell date +%FT%T%Z).log
flush_log:
        @echo "==> flush logs"
        cat my_log > $(logfile)
        echo `wc -l $(logfile)`
Run Code Online (Sandbox Code Playgroud)