立即赋值和延迟赋值
:= 立即赋值 按照先后顺序执行,立即赋值
= 延迟赋值,赋值的结果会等到整个路径执行完成以后确定。换句话说,后面会覆盖前面的。
- 测试用例 ```makefile a = foo b1 := $(a) bar b2 = $(a) bar a = xyz
all: @echo b1=$(b1) @echo b2=$(b2)
- 执行结果
```bash
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make all
b1=foo bar
b2=xyz bar
变量赋值和目标执行
我们先查看下面的例子,
a ?= aaa
b := $(a)
c = $(a)
a_origin = $(origin a)
b_origin = $(origin b)
c_origin = $(origin c)
all:
@echo all:$(a)
@echo all:$(b)
@echo all:$(c)
@echo all:$(a_origin)
@echo all:$(b_origin)
@echo all:$(c_origin)
a = bbb
b := $(a)
c = $(a)
test1:
@echo test1:$(a)
@echo test1:$(b)
@echo test1:$(c)
@echo test1:$(a_origin)
@echo test1:$(b_origin)
@echo test1:$(c_origin)
a = ccc
b := $(a)
c = $(a)
test2:
@echo test2:$(a)
@echo test2:$(b)
@echo test2:$(c)
@echo test2:$(a_origin)
@echo test2:$(b_origin)
@echo test2:$(c_origin)
a = ddd
- 执行默认的target
发现执行得结果都是一样得,所以,结论是,Makefile 中所有变量赋值的语句在所有 target 之前完成,跟变量赋值与 target 的相对位置无关。zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make all
all:ddd
all:ccc
all:ddd
all:file
all:file
all:file
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make test1
test1:ddd
test1:ccc
test1:ddd
test1:file
test1:file
test1:file
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make test2
test2:ddd
test2:ccc
test2:ddd
test2:file
test2:file
test2:file
另外,我们可以看到 b 没有跟上 c 的节奏,拿到 ccc 就不再跟 c 一样去拿最后设置的 ddd 了,体现了 “:=” 的 “立即赋值”,而 c 一直等到了 Makefile 最后的 a。另外,三个变量最后的值都是文件内部赋值,所以 origin 是 file。通过命令行赋值
由次可以断定通过命令行传入得参数优先级高。zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make test2 a=123
test2:123
test2:123
test2:123
test2:command line
test2:file
test2:file
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make test2 b=123
test2:ddd
test2:123
test2:ddd
test2:file
test2:command line
test2:file
通过环境变量赋值
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ export a=123456
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ ecport a = 12345^C
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make -e all
all:123456
all:123456
all:123456
all:environment override
all:file
all:file
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ unset a
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ b=343 make -e all
all:ddd
all:343
all:ddd
all:file
all:environment override
all:file
调试与跟踪
调试跟踪
# 调试
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make --debug all
# 跟踪
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make --trace all
日志
$(info ...)
$(warning ...)
# 下面这一个会停止运行Makefile
$(error ...)
- Environment dumping
# 将所有的变量保存到文件里面
make -p xxx > xxx.data.dump