迭代 Makefile 中的列表?
我发现我正在编写很多 Makefile,可以使用 n 元组列表来清理这些文件。 但我找不到任何方法来正确(且干净地)执行此操作。 到目前为止,我只能使用 $(shell ...) 和 tr、sed 或其他非 - Makefile 标准。
例如,我想这样做:
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 ; \
)
Is有一个好的方法来迭代Makefiles中的n元组列表吗? 谢谢!
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.
For example, I'd like to do this:
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 ; \
)
Is there a good way to iterate n-tuple lists in Makefiles? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会查看 foreach 上的 GNU Make 手册。 这是我在不同项目中使用的一些随机片段...该示例不完整,但也许它会给您一些想法? 如果我有更多时间,我可能会稍后清理它......
I'd check the GNU Make manual on foreach. here are some random snips that I've used in a different project... the example is incomplete, but maybe it will give you some ideas? I might clean this up later if I've got more time...
Makefile 本质上是声明性的,所以我认为 make 本身并不能提供您想要的东西。 但是,您似乎想要将一些字符串值与特定目标相关联,因此可能
如果您还没有读过,GNU 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:
If you haven't already read it, the GNU make manual is pretty damn good.
Edit: To do what you asked about in your comment:
use:
感谢您的提示——经过一番黑客攻击后,我认为这更符合我的期望:
有人能做得更好吗?
Thanks for the hints -- after some hacking I think this is more what I was hoping for:
Can anyone do better?
据我所知,这是因为您试图强制 make 作为命令式语言工作,而事实并非如此。
在 GNU make 中,您可能想做类似的事情:
或者更好的是,重新定义 .c 文件的默认规则来为您处理链接,但是您的名称结构很奇怪(程序名称没有词法关系)源名称)使得这变得困难。
如果您希望能够快速重建而无需大量手动编辑,您可能需要编写一个脚本来从数据重新生成 makefile 片段并使用 GNU make 的
include
功能...None that I know of, but that is because you're trying to force make to work ans an imperative language, when that is not what it is.
In GNU make you'd probably want to do something like:
Or better yet, redefine the default rule for .c files to handle the linking for you, but the strange structure of your names (the program names don't have a lexical relationship to the source names) makes that hard.
If what you want to be able to rebuild this quickly without an lot of hand editing, you probably want to write a script to regenerate the makefile framgment from data and use the
include
feature of GNU make...你这是在倒退。
您试图将 make 视为脚本。 它不是,而是一组关于如何在给定 Y 的情况下创建 X 的规则。然后 make 引擎会计算出需要发生什么才能获得该结果。
对于给出的示例,您确实应该使用脚本来执行生成步骤。 也许可以从 make 调用它,但让 make 处理 CC 的东西。
You're doing it backwards.
You're trying to treat make like it's a script. It's not, instead its a set of rules on how to create X given Y. Then the make engine figures out what needs to happen to get that result.
For the example given, you really should be using a script for the generation steps. Perhaps calling that from make, but let make handle the CC stuff.
您可以对具有相同扩展名的一组文件使用默认规则,将每个
c
文件编译为o
。 当然,您不限于任何特殊的文件扩展名。 要编译一组.c
文件,您可以这样做:You can use default rules for a set of files with the same extension as in for compiling each
c
file to ano
. Of course you are not restricted to any special file extensions. For compiling a set of.c
files you could do it like this: