神奇的make自动生成include file的功能

嗯,今天研究公司makefile的代码,始终搞不明白有一段下载编译依赖的rule recipe(对这个名词不了解请参考make的官方文档)是怎么执行的。明明在执行的时候并指定的target并没有依赖那段recipe对应的target啊。原来,make有一个不指定target也能运行相应target recipe的功能,见如下Makefile:

include file.mk

file.mk :
    echo "#this is auto-gen file" >file.mk

运行这个makefile之前,file.mk是不存在的。看看运行结果:

C:Users***>make
Makefile:1: file.mk: No such file or directory
echo "#this is auto-gen file" >file.mk
make: `file.mk' is up to date.

再看看运行的文件夹,file.mk被创建了。原来,make在include文件的时候,如果文件不存在,make并不立即报fatal并停止,而是会去找是否有对应的rule能创建(或更新)这个target。如果有,创建完之后再include。

学习了。。。

原文地址:https://www.cnblogs.com/waytofall/p/4747985.html