【问题标题】:Makefile: read input variable and set several variablesMakefile:读取输入变量并设置几个变量
【发布时间】:2016-02-08 03:01:20
【问题描述】:

我有一个生成文件,我想从输入中读取文件名,然后根据它的名称生成其他名称。我试过下面的代码

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.mlfootest.mlfootest.native

但是,我只能得到foo.ml。剩下的两个我只得到.ml.native

我该如何解决这个问题?

【问题讨论】:

  • 这种交互处理真的不是make的领域,有什么理由你不能接受输入变量,例如make file=foo?

标签: makefile


【解决方案1】:

首先,让我们通过删除 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

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多