简体   繁体   English

Makefile延迟变量扩展

[英]makefile delay variable expansion

I have some rules in my root makefile that look something like this: 我的根makefile中有一些规则,如下所示:

DEPS += a
DEPS += b
...

$(THE_BINARY) : $(DEPS)

Individual app makefiles include this root makefile after providing some required variables. 在提供一些必需的变量之后,各个应用程序生成文件都include此根生成文件。 I want to make it so that apps can add to the DEPS variable too. 我想这样做,以便应用程序也可以添加到DEPS变量中。 This works: 这有效:

DEPS += some_other_dep
include root.make

but this doesn't: 但这不是:

include root.make
DEPS += some_other_dep

Is this because ($DEPS) is expanded at the point where the rule is assigned, and not at the end? 这是因为($DEPS)是在分配规则的地方而不是在结尾处扩展的吗? Is there a way to write the root makefile in such a way that both DEPS += ... lines do the same thing? 有没有一种方法可以写根生成文件,使得两个DEPS += ...行都做同样的事情?

Aggieboy's comment is correct as to the issue: if you include the makefile first then the rule using $(DEPS) is already expanded before you add extra things to it, so it doesn't see the extra things. Aggieboy对此问题的评论是正确的:如果您首先包含makefile,则在添加其他内容之前已经扩展了使用$(DEPS)的规则,因此看不到其他内容。 My advice for people writing this sort of makefile environment with common include files is that the included files that define any rules should always be at the end. 我对使用通用包含文件编写这种makefile环境的人的建议是,定义任何规则的包含文件应始终位于末尾。

It's true the .SECONDEXPANSION will solve this problem, if you want to go that way, and it's not actually that hard to understand. 的确,如果您想这样做, .SECONDEXPANSION将解决此问题,并且实际上并不难理解。 If the description in the manual is too hard to read, it might be useful to look at this discussion of expansion in GNU make , in particular the one on secondary expansion . 如果手册中描述太难懂了,那么看一下有关GNU make扩展的讨论可能会很有用,特别是关于二次扩展的讨论

I don't typically recommend this because although it will solve this problem, you'll hit the same issue with the next rule... you'll basically have to use secondary expansion for all your rules if you want to allow include files in any order WRT variable assignments. 我通常不建议这样做,因为尽管它可以解决问题,但您会在下一条规则中遇到相同的问题...如果要允许在其中include文件,则基本上必须对所有规则使用二级扩展任何订单WRT变量分配。 And, secondary expansion only works for variables used in prerequisite lists. 而且,二次扩展仅适用于先决条件列表中使用的变量。 You're out of luck if you want to defer expansion of variables used in/as targets. 如果您想推迟在目标中/用作目标的变量的扩展,那将很不幸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM