下面两个语句有什么工作上的区别?
LDDIRS := -L$(ORACLE_LIB)
LDDIRS += -L$(ORACLE_LIB)
:= (简单变量扩展) 变量定义时,其值会被扫描一次并展开任何对其他变量和函数的引用。例如:
x:=foo
y:=$(x) bar
x:=later
因此上述代码等同于
y:=foo bar
x:=later
+= 用于向变量追加更多文本,例如:
objects=main.o foo.o bar.o
objects+=new.o
这将使 objects 的值为 'main.o foo.o bar.o new.o'
= 用于递归地扩展变量。该值原封不动地安装;如果它包含对其他变量的引用,则每当此变量被替换时,这些变量就会被展开。这被称为递归扩展。
"=" 用于定义递归扩展变量。下面的 Makefile 将打印出 "y is later bar"。
"x = foo
y = $(x) bar
x = later
all:;echo "y is" $(y)
":=" 用于定义简单扩展变量,该变量只会被扩展一次,并永久地保留其值。以下的Make文件将打印出 "y is foo bar"。
x := foo
y := $(x) bar
x := later
all:;echo "y is" $(y)
另外,就像其他人之前指出的那样,您可以在GNU make手册的“Using Variables”部分中获取更多细节信息。使用变量
希望这可以帮助您 :-)
GNU Make 有三个赋值运算符:":=","=","?=",以及用于向变量追加的 "+="。
":=" performs immediate evaluation of the right-hand side and stores an actual
string into the left-hand side.
e.g.:
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.
e.g.:
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.
e.g.:
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
:=
将变量 here 定义为左侧,+=
将右侧添加到变量的现有值中。将 :=
与 =
进行比较,=
在使用位置计算右侧表达式(而不是在特定行中)。
您可以查看手册here(假设您正在使用GNU make)。
:=
会将值设置一次给变量,即每当编译器遇到该变量时不会重新评估。在编译代码时,这可能会对性能产生极大的影响。
+=
只会将一个值加到变量上。
:=
是用于赋值的,与 =
的方式相同。
+=
将一个新值添加到变量中。