makefile在循环中解释变量

问题描述:

我试图让一个变量被解释为for循环中的另一个变量,我无法弄清楚这应该如何完成。我的测试是:makefile在循环中解释变量

TEST= one two three 

one := one1 
two := two2 
three := three3 

all: 
    @echo $(TEST) 
    @for NUM in $(TEST) ; do \ 
     echo $($$NUM) ; \ 
    done ; exit 0 

foo: 
    echo 

最终结果是:我有很多的makefile与包括指向一个中心makefile文件。我想让这些独立的makefile包含文件的变量来一起协调 - 但不同的目录有不同的文件。因此,./1/中的makefile可能是: one:=“file0 file1 file2” two:=“other0 other1 other2” three:=“boo.txt blah.txt” include ../main.mk

和./2/可能是 之一:= “file0 file5 file6” 二:= “富酒吧巴兹” 三:= “blah.txt” 包括../main.mk

那不能按照你写的方式工作。你正在穿越边界。

当你的shell循环运行时,make变量已经被扩展了,所以你的循环体最终包含的是'echo;'。

如果您想获得预期的输出,您需要在make级别进行循环。

喜欢的东西:

TEST= one two three 

one := one1 
two := two2 
three := three3 

define loop 
     $(foreach n,$(TEST),@echo $($n) 
) 
endef 

all: 
     @echo $(TEST) 
     $(loop) 

虽然我觉得应该有一个稍微简单的办法,我只是没有看到它的那一刻。