继续翻译

6.2 The Two Flavors of Variables
================================There are two ways that a variable in GNU `make' can have a value; we
call them the two "flavors" of variables.  The two flavors are
distinguished in how they are defined and in what they do when expanded.The first flavor of variable is a "recursively expanded" variable.
Variables of this sort are defined by lines using `=' (*note Setting
Variables: Setting.) or by the `define' directive (*note Defining
Multi-Line Variables: Multi-Line.).  The value you specify is installed
verbatim; if it contains references to other variables, these
references are expanded whenever this variable is substituted (in the
course of expanding some other string).  When this happens, it is
called "recursive expansion".For example,foo = $(bar)bar = $(ugh)ugh = Huh?all:;echo $(foo)will echo `Huh?': `$(foo)' expands to `$(bar)' which expands to
`$(ugh)' which finally expands to `Huh?'.This flavor of variable is the only sort supported by other versions
of `make'.  It has its advantages and its disadvantages.  An advantage
(most would say) is that:CFLAGS = $(include_dirs) -Oinclude_dirs = -Ifoo -Ibarwill do what was intended: when `CFLAGS' is expanded in a recipe, it
will expand to `-Ifoo -Ibar -O'.  A major disadvantage is that you
cannot append something on the end of a variable, as inCFLAGS = $(CFLAGS) -Obecause it will cause an infinite loop in the variable expansion.
(Actually `make' detects the infinite loop and reports an error.)
Another disadvantage is that any functions (*note Functions for
Transforming Text: Functions.)  referenced in the definition will be
executed every time the variable is expanded.  This makes `make' run
slower; worse, it causes the `wildcard' and `shell' functions to give
unpredictable results because you cannot easily control when they are
called, or even how many times.To avoid all the problems and inconveniences of recursively expanded
variables, there is another flavor: simply expanded variables."Simply expanded variables" are defined by lines using `:=' (*note
Setting Variables: Setting.).  The value of a simply expanded variable
is scanned once and for all, expanding any references to other
variables and functions, when the variable is defined.  The actual
value of the simply expanded variable is the result of expanding the
text that you write.  It does not contain any references to other
variables; it contains their values _as of the time this variable was
defined_.  Therefore,x := fooy := $(x) barx := lateris equivalent toy := foo barx := laterWhen a simply expanded variable is referenced, its value is
substituted verbatim.Here is a somewhat more complicated example, illustrating the use of
`:=' in conjunction with the `shell' function.  (*Note The `shell'
Function: Shell Function.)  This example also shows use of the variable
`MAKELEVEL', which is changed when it is passed down from level to
level.  (*Note Communicating Variables to a Sub-`make':
Variables/Recursion, for information about `MAKELEVEL'.)
ifeq (0,${MAKELEVEL})whoami    := $(shell whoami)host-type := $(shell arch)MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}endifAn advantage of this use of `:=' is that a typical `descend into a
directory' recipe then looks like this:
${subdirs}:${MAKE} -C $@ allSimply expanded variables generally make complicated makefile
programming more predictable because they work like variables in most
programming languages.  They allow you to redefine a variable using its
own value (or its value processed in some way by one of the expansion
functions) and to use the expansion functions much more efficiently
(*note Functions for Transforming Text: Functions.).You can also use them to introduce controlled leading whitespace into
variable values.  Leading whitespace characters are discarded from your
input before substitution of variable references and function calls;
this means you can include leading spaces in a variable value by
protecting them with variable references, like this:nullstring :=space := $(nullstring) # end of the lineHere the value of the variable `space' is precisely one space.  The
comment `# end of the line' is included here just for clarity.  Since
trailing space characters are _not_ stripped from variable values, just
a space at the end of the line would have the same effect (but be
rather hard to read).  If you put whitespace at the end of a variable
value, it is a good idea to put a comment like that at the end of the
line to make your intent clear.  Conversely, if you do _not_ want any
whitespace characters at the end of your variable value, you must
remember not to put a random comment on the end of the line after some
whitespace, such as this:dir := /foo/bar    # directory to put the frobs inHere the value of the variable `dir' is `/foo/bar    ' (with four
trailing spaces), which was probably not the intention.  (Imagine
something like `$(dir)/file' with this definition!)
There is another assignment operator for variables, `?='.  This is
called a conditional variable assignment operator, because it only has
an effect if the variable is not yet defined.  This statement:FOO ?= baris exactly equivalent to this (*note The `origin' Function: Origin
Function.):ifeq ($(origin FOO), undefined)FOO = barendifNote that a variable set to an empty value is still defined, so `?='
will not set that variable.

6.2 变量的两个风格
================================

有两种方法可以使得GNU make中的一个变量拥有一个值;我们成其为变量的两个风格。这两个趣味由他们如何定义和如何扩展而区分开来。

第一个变量的趣味来自递归扩展变量。此类变量用 =来定义(*note Setting Variables: Setting),或者用 define 指令(*note Defining Multi-Line Varaibles: Multi-Line)

你所定义的值会被逐字翻译;如果它包含了对其他变量的参照。这些参照会在此变量被替换的时候进行扩展(在对其他字符串的扩展时)。当它发生的时候,可以称为递归扩展。

例如,

foo = $(bar)
bar = $(ugh)
ugh = Huh?

all:;echo $(foo)

会显示`Huh?': `$(foo)' 扩展为 `$(bar)',它再继续扩展为`$(ugh)' ,最终扩展为`Huh?'。

This flavor of variable is the only sort supported by other versions
of `make'. It has its advantages and its disadvantages. An advantage
(most would say) is that:

这种风格的变量是唯一被其他版本make所支持的。它有优点和缺点。一个优点(大多数情况下)是:

CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar

将会执行所期待的: 当 CFLAGS 在片段中被扩展,它会扩展为  -Ifoo -Ibar -O。一个主要的确定是你无法在变量后面最加一些东西,就像

CFLAGS = $(CFLAGS) -O

因为它将导致一个变量扩展的无限的循环(实际上make会检测到这个无限循环报告一个错误)。

另一个坏处是,在定义中所参照的任何函数(*note Functiosn for Transforming Text: Functions.)在每次变量扩展的时候都要执行。这导致make 运行缓慢。更糟糕的是,它导致通配符和shell 函数给出不确定的结果,因为你无法简单地控制它们何时以何种方式被调用多少次。

为了防止上述的所有问题和不方便之处,这里有另外一个风格:简单扩展变量。

"简单扩展变量"由:=来定义(*note Setting Variables: Setting.),简单扩展变量的值只被扫描一次,在任何其他的对此变量进行参照的变量和函数中扩展。简单扩展变量的实际值就是你所写的文本扩展结果。它不包含任何对其他变量的参照;它包含此变量被定义时的值。因此,

x := foo
y := $(x) bar
x := later

等价于:

y := foo bar

x := later

当一个简单扩展变量被参照的时候,它的值会被逐字逐句替换。

This example also shows use of the variable
`MAKELEVEL', which is changed when it is passed down from level to
level. (*Note Communicating Variables to a Sub-`make':
Variables/Recursion, for information about `MAKELEVEL'.)

这里有一个更加复杂的例子,显示了和shell 函数结合,对:=的利用。(*Note the 'shell' Function:Shell Function.)

ifeq (0,${MAKELEVEL})
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif

使用这种 :=的一个好处就是 典型的进入目录的片段会看起来像这样:

${subdirs}:
${MAKE} -C $@ all

简单扩展变量通常会使得复杂的makefile编程更精确,因为它们就像大多数的编程语言一样进行工作。它们允许你用变量的值来重新定义自己(或者用某种方式在扩展函数中处理),可以更加高效地运行扩展函数(*note Functions for Transforming Text: Functions.)

你也可以用它们来在变量值中引入前导空格。在替换到变量参照和函数调用之前,前导空格会被忽略;这种方式下你可以通过利用变量参照来保护前导空格,像下面这样:

nullstring :=
space := $(nullstring) # end of the line

这里变量space 的值是精确的一个空格。注释'# end of the line' 在这里只是为了更加清晰地说明问题。因为末尾的空格字符不会从变量值中剥除,在行末的一个空格也可以达到同样的效果(但是读取来很困难)。

如果你在一个变量值得最后面附加一个空格,像上述那样在此行后面附加一个注释是一个好主意。相反地,如果你不想任何空格出现在你的变量末尾,你必须记住不要在行的末尾,在若干空格后面加一个注释。就像这样:

dir := /foo/bar # directory to put the frobs in

此时,dir 变量的值是 '/foo/bar '(末尾有四个空格),这可能不符合我们的预期。(想象比如 $(dir)/file这样的东西)

这一由另外一个对变量的复制操作符号 ?=。这被常委一个条件变量夫之妇好,一位只有在一个变量尚未确定时它才有效果。下面的句子:

FOO ?= bar

等价于 (*note The `origin' Function: Origin
Function.):

ifeq ($(origin FOO), undefined)
FOO = bar
endif

请注意一个便来能够设置为空值也相当于定义了,所以`?='不会设定给那个变量。
后文待续

转载于:https://www.cnblogs.com/gaojian/archive/2012/10/03/2710745.html

GNU make manual 翻译( 一百五十五)相关推荐

  1. GNU make manual 翻译( 一百七十五)

    继续翻译 `.DEFAULT_GOAL' Sets the default goal to be used if no targets were specified onthe command lin ...

  2. GNU make manual 翻译( 一百二十四)

    继续翻译 Note that such a prerequisite constitutes mentioning `main.o' in a makefile, so it can never be ...

  3. GNU make manual 翻译( 一百六十)

    继续翻译 The only restriction on this sort of use of nested variable references is that they cannot spec ...

  4. GNU make manual 翻译( 一百六十四)

    继续翻译 When you add to a variable's value with `+=', `make' acts essentially as if you had included th ...

  5. GNU make manual 翻译( 一百六十二)

    继续翻译 6.5 Setting Variables =====================To set a variable from the makefile, write a line st ...

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

    继续翻译 `ifdef VARIABLE-NAME' The `ifdef' form takes the _name_ of a variable as its argument, not a re ...

  7. GNU make manual 翻译( 一百五十四)

    继续翻译 6.1 Basics of Variable References =================================To substitute a variable's v ...

  8. GNU make manual 翻译( 一百五十九)

    继续翻译 A computed variable name need not consist entirely of a single variable reference. It can conta ...

  9. GNU make manual 翻译( 一百四十九)

    继续翻译 5.7.4 The `--print-directory' Option ------------------------------------If you use several lev ...

最新文章

  1. 超1500人报名参加的公开课,了解一下
  2. NHibernate学习手记(3) - NH的配置信息
  3. 监督分类空白处也被分类了_用于半监督短文本分类的异构图注意网络
  4. C#多线程学习 整理打包电子书
  5. css3动画之——动态的省略号
  6. 阻燃防火电缆在数据中心建设中的应用
  7. C++ map嵌套使用 链接
  8. 一键导出/备份「有道云笔记」工具
  9. Gateway currency conversion utility
  10. flex布局_Flex布局,真香
  11. Linux如何清除last信息,linux清除last、lastb和history记录
  12. 1.10 编程基础之简单排序 10 单词排序 python
  13. RobHess的SIFT源码分析:imgfeatures.h和imgfeatures.c文件
  14. 第二次提交文件 merge上次版本
  15. ios 构建版本一直在处理中_app已审核通过,ios构建版本失败,提示此构建版本...
  16. 八年测开经验面试28K公司后,吐血整理出高频面试题和答案
  17. 需要掌握的flex知识点
  18. visual studio 2008 提示 “函数xxx 已有主体”
  19. 认识c语言程序,认识C语言
  20. faker 无敌了,专注于制作假数据

热门文章

  1. for循环下标 shell_如果再写for循环,我就锤自己
  2. 合泰单片机市场占有率_holtek单片机图文全面详解
  3. python opencv 直方图均衡_详解python OpenCV学习笔记之直方图均衡化
  4. 信息安全工程师笔记-网络安全测评技术与标准
  5. Java笔记-多线程相关
  6. Qt工作笔记-使用SQL中的LIMIT进行数据分页
  7. Linux学习笔记-管道的读写特性
  8. 函数指针与指针函数(C++工厂设计最喜欢用这个)
  9. QGroupBox详解
  10. 全面介绍Windows内存管理机制及C++内存分配实例(二):内存状态查询