遍历Makefile中的列表? [英] Iterating over lists in Makefiles?

查看:163
本文介绍了遍历Makefile中的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我正在编写许多Makefile,可以使用 n -tuple列表清除它们.但是我找不到任何正确(干净)地进行此操作的方法.到目前为止,我只能提出使用 $(shell ...) tr sed 或其他方法, Makefile标准.

I find I'm writing a lot of Makefiles that could be cleaned up with the use of n-tuple lists. But I can't find any way to do this properly (and cleanly). So far I've only been able to come up with using $(shell ...) and tr, sed, or otherwise non-Makefile standards.

例如,我想这样做:

XYZs = \
    dog.c  pull_tail bark  \
    duck.c chase     quack \
    cow.c  tip       moo

all:
    @- $(foreach X Y Z,$(XYZs), \
        $(CC) $X -o bully/$Y ; \
        ln bully/$Y sounds/$Z ; \
    )

是否有一种很好的方法来迭代Makefile中的 n 个元组列表?谢谢!

Is there a good way to iterate n-tuple lists in Makefiles? Thanks!

推荐答案

Makefile本质上是声明性的,因此我不认为make本身可以提供所需的内容.但是,您似乎想将某些字符串值与特定目标相关联,因此

Makefiles are essentially declarative in nature, so I don't think that make itself provides what you want. However, you seem to be wanting to associate some string values with specific targets, so maybe the Target specific variable values feature of GNU make will be of interest. This is an extract from the manual:

还有一个特殊的功能 目标特定的变量:当您 定义特定于目标的变量, 该变量值也有效 对于此目标的所有依赖关系 (除非这些依赖项覆盖了它 拥有自己特定的目标 可变值).因此,例如 这样的声明:

There is one more special feature of target-specific variables: when you define a target-specific variable, that variable value is also in effect for all dependencies of this target (unless those dependencies override it with their own target-specific variable value). So, for example, a statement like this:

prog : CFLAGS = -g

prog : prog.o foo.o bar.o

将在命令中将CFLAGS设置为-g prog的脚本,但它也会 在命令中将CFLAGS设置为-g 创建prog.o,foo.o, 和bar.o,以及任何命令脚本 会创建它们的依赖关系.

will set CFLAGS to -g in the command script for prog, but it will also set CFLAGS to -g in the command scripts that create prog.o, foo.o, and bar.o, and any command scripts which create their dependencies.

如果您还没有读过它,那么GNU make手册非常不错.

If you haven't already read it, the GNU make manual is pretty damn good.

编辑:要执行您在评论中提出的要求:

To do what you asked about in your comment:

dog: ANIMAL=dog.c BULLY=pull_tail SOUND=bark

使用:

dog: ANIMAL=dog.c 
dog: BULLY=pull_tail 
dog: SOUND=bark

这篇关于遍历Makefile中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆