Makefile:读取输入变量并设置多个变量
我有一个makefile,我想从输入中读取文件名,然后根据文件名生成其他名称。我尝试了以下代码Makefile:读取输入变量并设置多个变量,makefile,Makefile,我有一个makefile,我想从输入中读取文件名,然后根据文件名生成其他名称。我尝试了以下代码 mlext = .ml testext = test.ml nativeext = test.native test: @read -p "Enter file name: " file; \ echo $$file$(mlext); \ echo $$file$(testext); \ echo $$file$(nativeext) 例如: 如果我输入:foo
mlext = .ml
testext = test.ml
nativeext = test.native
test:
@read -p "Enter file name: " file; \
echo $$file$(mlext); \
echo $$file$(testext); \
echo $$file$(nativeext)
例如:
如果我输入:foo
然后我想得到foo.ml
,footest.ml
,footest.native
但是,我只能获得foo.ml
。对于其余两个,我只得到.ml
和.native
如何修复此问题?首先,让我们通过删除Makefile中的
@
来查看shell的确切配方:
read -p "Enter file name: " file; \
echo $file.ml; \
echo $filetest.ml; \
echo $filetest.native;
因此,问题是$(testext)
的内容被附加到$$file
,从而创建shell变量$filetest
,该变量(很可能)不存在,最终导致一个空字符串。这不会发生在$(mlext)
中,因为初始点不能是变量名的一部分
要克服这一问题,请在Makefile规则中使用
$${file}
而不是$$file
。这种交互式处理实际上不是make的域,是否有任何原因使您不能接受输入变量,例如Makefile=foo
?