变量定义*和* undefined

时间:2011-02-26 14:41:20

标签: makefile

我想这在某种程度上是有道理的,但我无法理解为什么:在下面的代码中,我得到两个警告(请注意原始代码是使用制表符缩进的):

define variable-definition
    ifndef $1
        $(warning $1 is undefined)
    else
        $(warning $1 is defined)
    endif
endef

PS:我想检查名称传递为$1的变量是否存在,而不是$1是否通过。

PPS:对整个事物进行Dedenting并没有帮助。

2 个答案:

答案 0 :(得分:9)

Beta对根本原因的分析是正确的,您没有在$来电中转发$(warning)。以下是我修复它的方法:

define variable-def
  ifndef $1
    $$(warning $1 is undefined)
  else
    $$(warning $1 is defined)
  endif
endef
FOO=bar
$(eval $(call variable-def,FOO))

请注意,我使用空格缩进,而不是制表符。如果您使用制表符缩进,则会收到以下错误:*** commands commence before first target. Stop.

这使用GNUisms,但你的样本也是如此(我认为)。

答案 1 :(得分:1)

它同时给你两个警告的原因是当你调用这个函数(即扩展这个变量)时,Make会扩展其中的变量,包括两个警告。如果尝试评估ifndef(并且可能失败),则会发生这种情况。 Make只是不按照你想要的方式处理条件。

这是一种方法,有点笨重但有效。 (要做到这一点非常顺利,因为一个功能可能需要相当多的黑魔法)。写一个名为variable-definition的单独makefile:

# In makefile variable-definition

ifndef $(FOO)
 $(warning $(FOO) is undefined)
else
 $(warning $(FOO) is defined)
endif

然后在主makefile中:

# In the main makefile

bar = something
FOO := bar

include variable-definition

FOO := baz

include variable-definition