我现在正在学习如何使用makefile,并且遇到了在规则内部使用变量的问题。我的目标是解析包含其他文件名的文件,将其保存在变量中,并将该变量用作“目标模式”以在每个文件上运行规则。这是我的问题的一个简单示例(我在文件中写入文件名使其更简单)。
当我运行以下内容时:
variable = file1.txt file2.txt file3.txt
run : $(variable)
$(variable): %.txt:
echo File is $@
我得到了我想要的以下内容:
我得到了我想要的以下内容:
$ make run
echo File is file1.txt
File is file1.txt
echo File is file2.txt
File is file2.txt
echo File is file3.txt
File is file3.txt
问题是我想通过解析一个文件来定义变量
,并且我希望该文件成为依赖项,因为如果它不存在,我将使用makefile中的另一条规则来创建它。因此,当我像这样在规则中定义变量时(我知道它在我的echo中):
target1 :
$(eval variable = file1.txt file2.txt file3.txt)
echo $(variable)
run : $(variable)
$(variable): %.txt:
echo File is $@
我得到了以下结果:
$ make target1
echo file1.txt file2.txt file3.txt
file1.txt file2.txt file3.txt
还有这个:
$ make run
make: Nothing to be done for `run'.
所以我的问题是如何在一个规则中定义变量并在类似以下内容中使用它:
$(variable): %.txt:
echo File is $@
我已经尝试阅读手册和Google搜索,但我无法弄清楚这个问题。我想可能有一些简单的答案我没有想到。非常感谢您的帮助!:)
target1
的配方时,整个 makefile 的其余部分已经被解析,variable
已经被扩展为空字符串。 - MadScientist