使用命令行我得到所需的输出
$ ./program $(< file.txt)
./program 1 2 3 4 5
但是有一个makefile
all: program file.txt
./program $(< file.txt)
@rm -f program
program: program.c
gcc program.c -o program
我得到了输出
gcc program.c -o program
./program
因为在makefile中,$(...)
语法用于变量插值。因此,您的makefile尝试扩展名为的makefile变量/环境变量的值< file.txt
。如果未设置,它将扩展为空字符串。
证明:
all:
echo $(< file.txt)
和file.txt包含
now it works
然后执行
% env '< file.txt=Hello world' make
echo Hello world
Hello world
即通过设置一个名为< file.txt
value的环境变量Hello world
,问候语被打印出来。解决方法是$
通过将字符加倍来转义该字符:
all:
echo $$(< file.txt)
接着
% make
echo $(< file.txt)
now it works
QED
最后,虽然$()
在POSIX插语法壳中,$(< file.txt)
是不是,但你可以将其替换为$(cat file.txt)
因此它具有微创符合POSIX的炮弹工作。当然,在makefile中,您再次需要将美元增加一倍,从而获得最大程度的兼容性
$$(cat file.txt)
或者,您可以使用类似的makefile工具$(shell )
,即
$(shell cat file.txt)
也会工作...(现在有一个$
)。最后,您也可以使用$(file )
GNU makefile函数读取文件,即
all:
echo $(file <file.txt)
会类似地工作,但根本不会调用shell。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句