Makefile GNU make条件的求值
下面的代码片段Makefile GNU make条件的求值,makefile,gnu-make,Makefile,Gnu Make,下面的代码片段 ifeq (1,1) a = 1 $(info true) endif ifeq (1,0) a = 0 $(info false) endif $(info $(a)) 印刷品 true 1 我明白了。如果我把同样的东西放在一个定义中,然后计算 define foo ifeq (1,1) a = 1 $(info true) endif ifeq (1,0) a = 0 $(info false) endif
ifeq (1,1)
a = 1
$(info true)
endif
ifeq (1,0)
a = 0
$(info false)
endif
$(info $(a))
印刷品
true
1
我明白了。如果我把同样的东西放在一个定义中,然后计算
define foo
ifeq (1,1)
a = 1
$(info true)
endif
ifeq (1,0)
a = 0
$(info false)
endif
endef
$(eval $(call foo))
$(info $(a))
它打印
true
false
1
ifeq(1,0)
的计算结果不是真的(因为$(a)
的值最终是1)。但是为什么它要打印false
?,因为make在解析计算结果之前,首先展开要计算的字符串
在make解析代码之前,展开eval
的参数时,info
函数正在展开。您需要将info
延迟到eval
通过转义$
来检查它:
define foo
ifeq (1,1)
a = 1
$$(info true)
endif
ifeq (1,0)
a = 0
$$(info false)
endif
endef
啊。取消引用语法导致执行内置函数。有道理。谢谢。我想说清楚,扩展会导致所有变量和函数的扩展,不管是内置的还是非内置的。而且,扩展是递归的。只有几种方法可以避免它(转义$
或使用$(值…
函数)。