GNU make manual 翻译( 一百七十九)

本文详细解析了Makefile中的条件编译机制及其工作原理,通过实例展示了如何根据不同条件选择性地编译不同的库文件,并介绍了如何利用条件化的变量赋值来简化Makefile配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继续翻译

   As this example illustrates, conditionals work at the textual level:
the lines of the conditional are treated as part of the makefile, or
ignored, according to the condition.  This is why the larger syntactic
units of the makefile, such as rules, may cross the beginning or the
end of the conditional.

   When the variable `CC' has the value `gcc', the above example has
this effect:

     foo: $(objects)
             $(CC) -o foo $(objects) $(libs_for_gcc)

When the variable `CC' has any other value, the effect is this:

     foo: $(objects)
             $(CC) -o foo $(objects) $(normal_libs)

   Equivalent results can be obtained in another way by
conditionalizing a variable assignment and then using the variable
unconditionally:

     libs_for_gcc = -lgnu
     normal_libs =

     ifeq ($(CC),gcc)
       libs=$(libs_for_gcc)
     else
       libs=$(normal_libs)
     endif

     foo: $(objects)
             $(CC) -o foo $(objects) $(libs) 

正如这个例子所揭示的那样,条件式在文本层面上工作:条件式的行被当作makefile的一部分,或者被忽略,这取决于条件本身。这就是为什么makefile中的大一些的语法单位,例如规则,可以跨越条件式的开始或终了之处。

当变量CC拥有值gcc, 上述的例子有如下的效果:

foo: $(objects)
$(CC) -o foo $(objects) $(libs_for_gcc)

如果变量 CC 有其他的值,则效果如下:

foo: $(objects)
$(CC) -o foo $(objects) $(normal_libs)

用另外的方法可以得到同样的结果--把一个对变量的赋值进行条件化,然后使用此变量。

libs_for_gcc = -lgnu
normal_libs =

ifeq ($(CC),gcc)
libs=$(libs_for_gcc)
else
libs=$(normal_libs)
endif

foo: $(objects)
$(CC) -o foo $(objects) $(libs)

后文待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值