使用 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/