Linux make 文件中的 := 和 += 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10227598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between := and += in make file?
提问by Naina B
what is working difference in the below statements?
以下陈述中的工作差异是什么?
LDDIRS := -L$(ORACLE_LIB)
LDDIRS += -L$(ORACLE_LIB)
回答by David Rodríguez - dribeas
:=
Defines the variable hereto be the left hand side, +=
adds the right hand side to the existing value of the variable. Compare :=
with =
which evaluates the right hand side at the place of use (rather than in this particular line)
:=
将此处的变量定义为左侧,+=
将右侧添加到变量的现有值。比较:=
与=
该评估在使用场所的右手边(而不是这一行)
You can look at the manual here(Assuming that you are using GNU make)
你可以看这里的手册(假设你使用的是GNU make)
回答by Geoffroy
The :=
is for assignation, in the same manner as =
.
的:=
是用于分配,以相同的方式作为=
。
+=
add a new value to the variable.
+=
为变量添加一个新值。
回答by Serdalis
From This website
从这个网站
for the syntax :=
Link to place on page
Simply expanded variables are defined by lines using ‘:=' (see Setting Variables). 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.
简单扩展的变量由使用 ':=' 的行定义(请参阅设置变量)。简单扩展变量的值将被一劳永逸地扫描,在定义变量时扩展对其他变量和函数的任何引用。简单扩展变量的实际值是扩展您编写的文本的结果。它不包含对其他变量的任何引用;它包含截至定义该变量时的值。
for the syntax +=
Link to place on page
When the variable in question has not been defined before, ‘+=' acts just like normal ‘=': it defines a recursively-expanded variable. However, when there is a previous definition, exactly what ‘+=' does depends on what flavor of variable you defined originally. See The Two Flavors of Variables, for an explanation of the two flavors of variables.
当所讨论的变量之前没有定义过时,'+=' 的作用就像普通的 '=':它定义了一个递归扩展的变量。但是,当有先前的定义时, '+=' 的确切作用取决于您最初定义的变量的风格。请参阅变量的两种风味,了解变量的两种风味。
回答by fduff
the :=
will set the value once to the variable, ie it wont be re-evaluated everytime make encouters that variable. Can make a huge difference in performance when compiling the code.
在:=
将设定值一次的变量,即它不会被重新评估每次化妆encouters变量。编译代码时可能会对性能产生巨大的影响。
+=
will simply add up a value to the variable.
+=
将简单地将一个值添加到变量中。
回答by Omji Mishra
:= (Simply Expanded Variable ) The value is scanned for once and for all expanding any
references to other variables and functions, when variable is defined. e.g.x:=foo
y:=$(x) bar
x:=later
so above is equivalent toy:=foo bar
x:=later
+= is used for appending more text to variables e.g.
objects=main.o foo.o bar.o
objects+=new.o
which will set objects to 'main.o foo.o bar.o new.o'= is for recursively expanded variable.The value is install verbatim; if it contains reference to other variables these variables are expanded whenever this variable is substituted.And this is known as recursive expansion.
:= (Simply Expanded Variable )
当变量被定义时,该值被扫描一次并扩展到其他变量和函数的任何引用。例如x:=foo
y:=$(x) bar
x:=later
上面的等价于y:=foo bar
x:=later
+= 用于将更多文本附加到变量,例如
objects=main.o foo.o bar.o
objects+=new.o
将对象设置为“main.o foo.o bar.o new.o”= 用于递归扩展变量。该值是逐字安装;如果它包含对其他变量的引用,则在替换此变量时扩展这些变量。这称为递归扩展。
回答by Zhile Zou
"=" is for defining recursively expanded variable. The follow make file will print out "y is later bar"
"=" 用于定义递归扩展变量。下面的make文件会打印出“y is later bar”
x = foo
y = $(x) bar
x = later
all:;echo "y is" $(y)
":=" is for defining simply expanded variable, which is expanded once and for all. The following make file will print out "y is foo bar"
":=" 用于定义简单扩展的变量,它被一劳永逸地扩展。下面的make文件会打印出“y is foo bar”
x := foo
y := $(x) bar
x := later
all:;echo "y is" $(y)
Also, as other people pointed earlier, you can get more details in Using Variablessection of the GNU make manual.
此外,正如其他人之前指出的,您可以在 GNU make 手册的使用变量部分中获得更多详细信息。
Hope this helps :-)
希望这可以帮助 :-)
回答by user10838108
GNU Make has three assignment operators, ":=" , "=", "?=" and one "+=" for appending
to the varibles.
- ":=" performs immediate evaluation of the right-hand side and stores an actual
string into the left-hand side.
eg:
x:=foo
y:=$(x) bar
x:=later
so above is equivalent to
y:=foo bar
x:=later
test above example
x := foo
y := $(x) bar
x := later
all:;echo "y is" $(y)
output
------
y is foo bar
- "=" is like a formula definition; it stores the right-hand side in an
unevaluated form and then evaluates this form each time the left-hand
side is used.
eg:
x = foo
y = $(x) bar
x = later
all:;echo "y is" $(y)
output
------
y is later foo
"?=" Assign only if it's not set/doesn't have a value. eg: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR) Would print "foo"
"+=" is used for appending more text to variables. e.g. objects=main.o foo.o bar.o objects+=new.o
which will set objects to 'main.o foo.o bar.o new.o'
"?=" 仅在未设置/没有值时才分配。例如:KDIR ?= "foo" KDIR ?= "bar" 测试: echo $(KDIR) 会打印 "foo"
"+=" 用于将更多文本附加到变量。例如 objects=main.o foo.o bar.o objects+=new.o
这会将对象设置为“main.o foo.o bar.o new.o”