2016-12-24 74 views
1

使用GNU让4.1,在我的Makefile我可以创建这个::时间戳日志文件使用`date` 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

回答

3

每一行的建议和在使用TAB字符缩进的makefile中(在规则语句后出现)是规则配方的一部分。规则配方中的行被传递给shell进行处理,它们不被make解析(除了展开变量/函数引用)。

因此,您的行logfile:=file...正在传递给shell并被shell解释...并且在shell中没有有效的:=运算符,因此shell认为整行是单个单词并尝试运行那个名字的程序显然不存在。

你可能想创建一个使变量,必须在配方之外来完成,像这样:

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