GNU make manual 翻译( 一百八十二)

ifdef命令详解
本文详细解析了Makefile中ifdef命令的工作原理及其应用场景。ifdef用于检查变量是否有非空值,通过不同的分支逻辑实现变量值的判断。文章通过具体示例说明了ifdef与其他命令的区别,以及如何正确地使用ifdef来避免常见错误。

继续翻译

复制代码
`ifdef VARIABLE-NAME'
     The `ifdef' form takes the _name_ of a variable as its argument,
     not a reference to a variable.  The value of that variable has a
     non-empty value, the TEXT-IF-TRUE is effective; otherwise, the
     TEXT-IF-FALSE, if any, is effective.  Variables that have never
     been defined have an empty value.  The text VARIABLE-NAME is
     expanded, so it could be a variable or function that expands to
     the name of a variable.  For example:

          bar = true
          foo = bar
          ifdef $(foo)
          frobozz = yes
          endif

     The variable reference `$(foo)' is expanded, yielding `bar', which
     is considered to be the name of a variable.  The variable `bar' is
     not expanded, but its value is examined to determine if it is
     non-empty.

     Note that `ifdef' only tests whether a variable has a value.  It
     does not expand the variable to see if that value is nonempty.
     Consequently, tests using `ifdef' return true for all definitions
     except those like `foo ='.  To test for an empty value, use
     `ifeq ($(foo),)'.  For example,

          bar =
          foo = $(bar)
          ifdef foo
          frobozz = yes
          else
          frobozz = no
          endif

     sets `frobozz' to `yes', while:

          foo =
          ifdef foo
          frobozz = yes
          else
          frobozz = no
          endif

     sets `frobozz' to `no'.
复制代码

`ifdef VARIABLE-NAME'


`ifdef'  把变量的名字作为它的参数,并不是对变量的一个参照。此变量的值有一个非空值。此时TEXT-IF-TRUE is effective 有效;否则,若TEXT-IF-FALSE存在,则TEXT-IF-FALSE有效。从未定义的变量拥有一个空值 。VARIABLE-NAME 文本会被扩展,因此它可以是一个变量或者函数的扩展,例如:

bar = true

foo = bar
ifdef $(foo)
frobozz = yes
endif

变量参照$(foo)被扩展,转化为bar,它可以视为一个变量的名字。变量bar不再扩展,但是它的值会用来被检查看其是否为空。

请注意 ifdef 只测试是否一个变量有一个值。它并不扩展词比变量来看它是否为非空。此外,用ifdef来测试会返回真,只有像foo=那种测试才会是例外。为了测试一个空值,可以使用 ifeq($(foo),)。例如:

bar =
foo = $(bar)
ifdef foo
frobozz = yes
else
frobozz = no
endif

设置 `frobozz' 为 `yes', 而:

foo =
ifdef foo
frobozz = yes
else
frobozz = no
endif

设置 `frobozz' 为 `no'.

后文待续






本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/10/08/2715187.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值