makefile - 在 Makefile 变量中使用 `date`

标签 makefile gnu-make

使用 GNU Make 4.1,在我的 Makefile 中,我可以使用以下内容创建时间戳日志文件:

flush_log:
        @echo "==> flush logs"
        cat file > file_`date +%FT%T%Z`.log

但该放谁:
file_`date +%FT%T%Z`.log

在 Makefile 内的 var 中,例如在其上制作 wc

我试过(没有成功):
flush_log:
    @echo "==> flush logs"
    logfile:=file_$(shell date +%FT%T%Z).log
    cat my_log > $(logfile)
    echo `wc -l $(logfile)`

我收到此错误:
$ 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
$

我遵循 https://stackoverflow.com/a/14939434/3313834 和简单扩展变量 https://www.gnu.org/software/make/manual/html_node/Flavors.html#Flavors 的建议

最佳答案

makefile 中的每一行(出现在规则语句之后)用 TAB 字符缩进都是规则配方的一部分。规则配方中的行被传递到 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)`

关于makefile - 在 Makefile 变量中使用 `date`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41316167/

相关文章:

c++ - 这是一个有效的 makefile 吗?

c++ - xcode 6.3.2 外部构建

c++ - GNU make 有时不在 VPATH 中声明的文件夹中搜索

c - 对于 Makefile 目录中的每个文件

makefile - make vs $(MAKE) for submakes in makefile - 有什么区别?

go - 生成后的模板标识

linux - 使 MahApp 需要 : No rule to make target 'part2.d' ,

makefile - 创建嵌套的 makefile

makefile - % 和 * 一起放在依赖行上

c++ - Makefile 创建两个库——一个依赖另一个