对于Makefile变量的每个目标

-1 linux gnu makefile gnu-make

我的makefile看起来如下

apps = app1 app2 app3

all: dir app1 app2 app3 zip cleanup

现在我想在appsvarible 列表上做一些循环,

就像是

`loop on apps

endloop`
Run Code Online (Sandbox Code Playgroud)

是否有可能在makefile上循环,我需要在appsvarible列表上进行循环

更新

可以说,该变量(apps是)生成由我中的程序make文件,它提供了应用程序的每个项目不同势值,有时其apps= app1 app2有时其apps= app1有时可以是20个应用程式或更多apps= app1 app2 appN

我如何迭代apps变量并做一些事情,例如在每次迭代中打印如下:

now im in `app1`
now im in `app2`
etc
Run Code Online (Sandbox Code Playgroud)

尝试以下时

.PHONY: foo
all: foo
APPS = app1 app2 app3 app4
foo : $(APPS)
    for $$f in $(APPS); do echo $$f is here; done
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

make: *** No rule to make targetapp1',需要的 foo'. Stop.

Mad*_*ist 6

我看到你来回走了一段时间,所以让我发表评论,这可能会有所帮助,也可能没有帮助.

通常,您不会在make recipes中编写循环,因为make本身提供了"循环".所以,当你写一个规则,如:

all: app1 app2 app3 app4
Run Code Online (Sandbox Code Playgroud)

make将尝试逐个构建这些先决条件中的每一个.因此,如果你想让一个makefile为apps变量中的每个条目回显一行,你会这样做:

all: $(apps)

$(apps):
        @echo $@
Run Code Online (Sandbox Code Playgroud)

这告诉make从一个目标开始all并尝试"构建"它的每个先决条件,即apps变量中的值.

然后,你定义如何构建应用程序的规则,并为每一个你说的那个规则是echo $@哪里$@是一个自动变量展开成建设当前目标.

在make中,语法为:

foo bar biz:
        some command
Run Code Online (Sandbox Code Playgroud)

是写作的简写和相同:

foo:
        some command
bar:
        some command
biz:
        some command
Run Code Online (Sandbox Code Playgroud)

编写makefile时,关键是您要考虑如何编写规则以从零个或多个必备文件创建一个文件(目标).然后,您将担心如何将所有这些先决条件连接在一起并正确排序.

ETA 如果要在$(apps)变量中保存的长列表中为某个特定目标设置特殊规则,则可以执行以下操作:

$(filter-out bar,$(apps)):
        @echo print $@

bar:
        some other command
Run Code Online (Sandbox Code Playgroud)