立即赋值和延迟赋值
:= 立即赋值 按照先后顺序执行,立即赋值= 延迟赋值,赋值的结果会等到整个路径执行完成以后确定。换句话说,后面会覆盖前面的。
- 测试用例 ```makefile a = foo b1 := $(a) bar b2 = $(a) bar a = xyz
all: @echo b1=$(b1) @echo b2=$(b2)
- 执行结果```bashzhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make allb1=foo barb2=xyz bar
变量赋值和目标执行
我们先查看下面的例子,
a ?= aaab := $(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 = bbbb := $(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 = cccb := $(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 allall:dddall:cccall:dddall:fileall:fileall:filezhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make test1test1:dddtest1:ccctest1:dddtest1:filetest1:filetest1:filezhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make test2test2:dddtest2:ccctest2:dddtest2:filetest2:filetest2: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=123test2:123test2:123test2:123test2:command linetest2:filetest2:filezhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make test2 b=123test2:dddtest2:123test2:dddtest2:filetest2:command linetest2:file
通过环境变量赋值
zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ export a=123456zhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ ecport a = 12345^Czhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ make -e allall:123456all:123456all:123456all:environment overrideall:fileall:filezhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ unset azhou@zhou:~/100ask-imx6-ull/Linux-project/Linux_Device_Driver/temp$ b=343 make -e allall:dddall:343all:dddall:fileall:environment overrideall: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
