继续翻译
6.5 Setting Variables ===================== To set a variable from the makefile, write a line starting with the variable name followed by `=' or `:='. Whatever follows the `=' or `:=' on the line becomes the value. For example, objects = main.o foo.o bar.o utils.o defines a variable named `objects'. Whitespace around the variable name and immediately after the `=' is ignored. Variables defined with `=' are "recursively expanded" variables. Variables defined with `:=' are "simply expanded" variables; these definitions can contain variable references which will be expanded before the definition is made. *Note The Two Flavors of Variables: Flavors. The variable name may contain function and variable references, which are expanded when the line is read to find the actual variable name to use. There is no limit on the length of the value of a variable except the amount of swapping space on the computer. When a variable definition is long, it is a good idea to break it into several lines by inserting backslash-newline at convenient places in the definition. This will not affect the functioning of `make', but it will make the makefile easier to read. Most variable names are considered to have the empty string as a value if you have never set them. Several variables have built-in initial values that are not empty, but you can set them in the usual ways (*note Variables Used by Implicit Rules: Implicit Variables.). Several special variables are set automatically to a new value for each rule; these are called the "automatic" variables (*note Automatic Variables::). If you'd like a variable to be set to a value only if it's not already set, then you can use the shorthand operator `?=' instead of `='. These two settings of the variable `FOO' are identical (*note The `origin' Function: Origin Function.): FOO ?= bar and ifeq ($(origin FOO), undefined) FOO = bar endif
6.5 设置变量
=====================
要从makefile中设置一个变量,要写一行开始于=或者 :=的变量。跟在=或者:=后面的成为变量的值。例如,
objects = main.o foo.o bar.o utils.o
定义了一个名字为 objects 的变量。围绕着变量名的空格和 = 后面的空格被忽略。
用=定义的变量是 递归式扩展的变量,用:=定义的变量是简单扩展式变量;这些定义可以包含变量参照,此参照将要在定义之前被扩展。 *Note the Two Flavors of Variables: Flavors.
变量名可能会包含函数或者变量参照,它们会在该行被读取的时候进行扩展以找到实际的变量名来使用。
只要不超出计算机的交换空间的大小,对变量值的长度没有什么限制。当一个变量的定义很长,通过在适当的位置插入反斜线把它分割成绩行是一个好主意。者不会影响到make的功能,但是会让makefile更容易被人读懂。
如果事先没有进行过设置,大多数的变量名被认为把空串当成一个值。有几个变量有内置的初始值,但是你可以按照通常的方式来设置它们(*note Variables Used by Implicit Rules: Implicit Variables.)。几个特殊的变量会被自动地根据每个规则来被设置;它们被称为自动变量(*note Automatic Variables::)
如果你想要一个变量仅仅是在还没有被设置好的情况下才被设置,那么你可以用操作符 ?=, 而不是用 =。这两个对变量FOO的设置都是一样的(*note The origin Function: Origin Function):
FOO ?= bar
和
ifeq ($(origin FOO), undefined)
FOO = bar
endif
后文待续