Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Makefile:读取输入变量并设置多个变量_Makefile - Fatal编程技术网

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

我有一个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

然后我想得到
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