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

本文深入探讨了Makefile中抑制变量继承的方法,介绍了如何使用`private`修饰符来阻止变量被目标文件继承,提供了具体的示例并解释了其应用场景。

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

继续翻译

6.13 Suppressing Inheritance
============================

As described in previous sections, `make' variables are inherited by
prerequisites.  This capability allows you to modify the behavior of a
prerequisite based on which targets caused it to be rebuilt.  For
example, you might set a target-specific variable on a `debug' target,
then running `make debug' will cause that variable to be inherited by
all prerequisites of `debug', while just running `make all' (for
example) would not have that assignment.

   Sometimes, however, you may not want a variable to be inherited.  For
these situations, `make' provides the `private' modifier.  Although
this modifier can be used with any variable assignment, it makes the
most sense with target- and pattern-specific variables.  Any variable
marked `private' will be visible to its local target but will not be
inherited by prerequisites of that target.  A global variable marked
`private' will be visible in the global scope but will not be inherited
by any target, and hence will not be visible in any recipe.

   As an example, consider this makefile:
     EXTRA_CFLAGS =

     prog: private EXTRA_CFLAGS = -L/usr/local/lib
     prog: a.o b.o

   Due to the `private' modifier, `a.o' and `b.o' will not inherit the
`EXTRA_CFLAGS' variable assignment from the `progs' target.

6.13 抑制继承
============================

在前面的章节中所描述的那样,make 变量通过前提条件继承。这个能力允许你根据目的,去改变一个前提条件的行为。例如,你可能在一个叫debug的目的上设置列一个目的特定的变量,然后运行 make debug。这可以导致变量被所有debug 的前提条件所继承。但是运行 make all(例如)就不会有这些赋值。

A global variable marked
`private' will be visible in the global scope but will not be inherited
by any target, and hence will not be visible in any recipe.

但是有时候,你也许不希望一个变量被继承。在这种场合下,make 提供了一个 private 修饰符。尽管这个private 修饰符可以用在任何变量赋值上面,到那时它在目的或者模式特定的变量赋值上面更有意义。任何标记为 private 的目的仅仅对本地的目的可见,并且不会被其前提条件所继承。

考虑下面的例子:
EXTRA_CFLAGS =

prog: private EXTRA_CFLAGS = -L/usr/local/lib
prog: a.o b.o

由于`private' 修饰符的关系, `a.o' 和 `b.o' 将不会从prog目的 继承 
`EXTRA_CFLAGS' 变量。

后文待续

转载于:https://www.cnblogs.com/gaojian/archive/2012/10/05/2712140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值