Makefile文件除了可以识别环境变量外,还可以识别执行make命令时传入的参数,即make可以向Makefile传参数。向Makefile传参数时是比较智能的,make程序会自动识别参数名称和参数值。
例如:make name=mycc
就把mycc这个值通过变量name传入到了Makefile文件中,在Makefile中可以得到$(name)变量,获取其值mycc。
如下面示例:
[cpp@wen*mdb_new:~]$ cat Makefile
flag=-gdb3
ifeq ($(AA),hello)
flag+=" -O3"
else
flag+=" -O3 -Wall"
endif
all:fun
fun:
@echo "$(AA)"
@echo "$(BB)"
@echo "$(flag)"
@test "$(AA)" = "hello" && echo "the 1th parameter is hello" || echo "the 1th parameter is not hello"
@test "$(BB)" = "world" && echo "the 2th parameter is world" || echo "the 2th parameter is not world"
在执行make AA=hello BB=world命令后,就把hello通过变量$(AA)传入Makefile文件,把world通过变量$(BB)传入Makefile。其执行效果为:
应用命令行传参方法,结合ifeq()、ifneq()比较函数、ifdef、 ifndef变量检测,可以建立适用于不同平台或不同编译条件的Makefile文件。
铁锈笔记2022-01-02
本文暂时没有评论,来添加一个吧(●'◡'●)