0

我试图让一个变量被解释为 for 循环中的另一个变量,但我不知道应该如何完成。我的测试是:

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 可能是:一个:=“file0 file1 file2”两个:=“other0 other1 other2”三个:=“boo.txt blah.txt”包括../main.mk

和 ./2/ 可能是一 := "file0 file5 file6" 二 := "foo bar baz" 三:= "blah.txt" include ../main.mk

4

1 回答 1

1

这不能像你写的那样工作。你正在跨越边界。

当你的 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)

虽然我觉得应该有一个稍微简单的方法,但我现在还没有看到。

于 2013-02-12T12:57:23.257 回答