【问题标题】:GNU Make. Variables with a scope limited to a single MakefileGNU 制造。范围限于单个 Makefile 的变量
【发布时间】:2012-10-19 09:01:24
【问题描述】:

我尝试在我当前的项目中实现一个非递归的 make 构建系统。我挣扎的是变量范围。目标特定变量不符合我的需求,因为变量通常定义目标而不是先决条件。 我需要的是:

Makefile1:

SOMEVAR := original_value
include Makefile2
$(warning $(SOMEVAR))

Makefile2:

#some magic here to do what I want and make me happy
SOMEVAR := included_value
#and maybe here

我想要的输出是'original_value'。

有什么策略可以让它成为现实吗?

编辑:我目前唯一的解决方案是强迫和组织自己将所有包含在每个特定 Makefile 的末尾并使用立即变量赋值:=

【问题讨论】:

  • 这个解决方案听起来不错。它出什么问题了?还有SOMEVAR是什么,有什么用?
  • 我不喜欢这个解决方案的地方是它非常不稳定。如果某个将取代我位置的可怜人不明白这个想法和位置包括其他地方,他会得到很多惊喜
  • 如果 Makefile2 被多次包含,并且它定义了一个引用 $(SOMEVAR) 的配方,那么它会严重损坏 --- 配方中的变量在执行配方时被评估,这意味着他们都会看到 $(SOMEVAR) 最后一个值,而不是定义配方时的值。

标签: makefile gnu-make


【解决方案1】:

当您只有全局变量时,一种策略是变量名冲突的老式解决方案:以穷人命名空间的形式为变量名添加前缀。

Makefile1:

Makefile1_SOMEVAR := original_value
include Makefile2
$(warning $(Makefile1_SOMEVAR))

Makefile2:

# no magic needed
Makefile2_SOMEVAR := included_value
# rest of Makefile2 uses $(Makefile2_SOMEVAR) of course

嘿,presto,按照这样的约定,就好像每个 makefile 都有自己的局部变量(或者,至少,在不与任何其他 makefile 冲突的命名空间中拥有自己的变量)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2014-04-27
    • 2017-06-12
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多