12

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

生成文件1:

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

生成文件2:

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

我想要的输出是'original_value'。

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

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

4

1 回答 1

9

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

生成文件1:

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

生成文件2:

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

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

于 2012-10-19T09:36:26.223 回答