在Makefile中经常看见这几个赋值运算符(“=”,“:=”,“?=”,“+=”)
新建一个Makefile,内容如下:
ifdef aa
var="hello world"
endif
ifeq ($(bb),define)
var ?= "hello world 1"
endif
ifeq ($(bb),define1)
var +="bb"
endif
ifeq ($(bb),define2)
var := "hello world is over"
endif
all:
@echo $(var)
输入一下命令:
make aa=true bb=define 输出:hello world
make aa=true bb=define1 输出:hello worldbb
make aa=true bb=define2 输出:hello world is over
make aa=bb=define 输出:hello world 1
make aa=bb=define1 输出:bb
make aa=bb=define2 输出:hello world is over
从输出可以看到他们的区别
"=" :是最基本的赋值运算
":=":是覆盖之前的值
"?=":是如果没有被赋值过的就赋予等号后面的值
"+=":是添加等号后面的值
注:
make会将整个Makefile展开,再决定变量的值。也是说,变量的值将会是整个Makefile最后被指定的值。
“=”的例子1:
x=foo
y=$(x)bar
x=xyz
在上面例子中y的值将是xyzbar,而不是foobar。
“:=”的例子2:
“:=”表示变量的值决定于它在Makefile中的位置,而不是整个Makefile展开后的最终值。
x:=foo
y:=$(x)bar
x:=xyz
在例子中,y的值将会输出foobar,而不是xyzbar了。