Makefile中几个赋值运算符

在Makefile中常常看见这几个赋值运算符(“=”,“:=”,“?=”,“+=”).net

新建一个Makefile,内容以下:get

ifdef aaclass

    var="hello world"变量

endiffile

ifeq ($(bb),define)db

    var ?= "hello world 1"di

endifmake

ifeq ($(bb),define1)运算符

    var +="bb"referer

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了。

相关文章
相关标签/搜索