在makefile中经常使用4种赋值符号,作用又都不相同,这里记录一下分别的作用。
使用一个例子先看一下:
ifdef DEFINE_VER
VER = "hello world!"
else
endif
ifeq ($(OPT),define)
VER ?= "hello world!firsr!"
endif
ifeq ($(OPT),add)
VER += "kelly!"
endif
ifeq ($(OPT),recover)
VER := "helloworld again!!!!"
endif
all:
@echo $(VER)
在命令行下分别执行
[(8) root@Fedora~/test_makefile]#make DEFINE_VER=true OPT=define
hello world!
[(9) root@Fedora~/test_makefile]#make DEFINE_VER=true OPT=add
hello world! kelly!
[(10) root@Fedora~/test_makefile]#make DEFINE_VER=true OPT=recover
helloworld again!!!!
[(11) root@Fedora~/test_makefile]#make DEFINE_VER= OPT=define
hello world!firsr!
[(12) root@Fedora~/test_makefile]#make DEFINE_VER= OPT=add
kelly!
[(13) root@Fedora~/test_makefile]#make DEFINE_VER= OPT=recover
helloworld again!!!!
从上面的结果中就可以看到他们的区别:
= 是最基本的赋值
:= 是覆盖之前的赋值
?= 是如果没有被赋值就赋予等号后面的值
+= 是添加等号后面的值
1、“=”
make会将整个makefile展开后,再决定变量的值。也就是说,变量的值将会是整个makefile中最后被指定的值。看例子:
x = foo
y = $(x) bar
x = xyz
在上例中,y的值将会是 xyz bar ,而不是 foo bar 。
2、“:=”
“:=”表示变量的值决定于它在makefile中的位置,而不是整个makefile展开后的最终值。
x := foo
y := $(x) bar
x := xyz
在上例中,y的值将会是 foo bar ,而不是 xyz bar 了