Makefile编译原理 makefile中的include关键字

一.makefile中的include关键字

类似C语言中的include

将其他文件的内容原封不动的搬入当前文件

make对include关键字的处理方式:

在当前目录搜索或指定目录搜索目标文件

搜索成功:将文件内容搬入当前makefile中

搜索失败:产生警告,并以文件名作为目标查找并执行对应规则

Makefile编译原理 makefile中的include关键字_第1张图片

实验1 :当前目录中没有 test.txt文件,makefile 中有对应规则

.PHONY : all

include test.txt

all : 
	@echo "this is all"
	
test.txt :
	@echo "test.txt"
		

mhr@ubuntu:~/work/makefile1$ 
mhr@ubuntu:~/work/makefile1$ make all
makefile:4: test.txt: No such file or directory
this is test.txt
this is all
mhr@ubuntu:~/work/makefile1$ 

实验2 :makefile 中 没有 include关键字后面 所对应的目标,当前目录也没有对应文件

.PHONY : all

include test.txt

all : 
	@echo "this is all"
	

/makefile1$ make all
makefile:4: test.txt: No such file or directory
make: *** No rule to make target 'test.txt'.  Stop.
mhr@ubuntu:~/work/makefile1$ 

实验3:当前目录中没有 test.txt文件,makefile 中有对应规则,并在规则中创建 test.txt .执行两次 make all

.PHONY : all

include test.txt

all : 
	@echo "this is all"
	
test.txt :
	@echo "test.txt"
		
	#创建 test.txt
	@touch test.txt
	


mhr@ubuntu:~/work/makefile1$ 
mhr@ubuntu:~/work/makefile1$ ls
func.c  func.h  func.o  hello.out  main.c  main.o  makefile
mhr@ubuntu:~/work/makefile1$ 
mhr@ubuntu:~/work/makefile1$ 
mhr@ubuntu:~/work/makefile1$ make all
makefile:5: test.txt: No such file or directory
this is test.txt
this is all
mhr@ubuntu:~/work/makefile1$ ls
func.c  func.h  func.o  hello.out  main.c  main.o  makefile  test.txt
mhr@ubuntu:~/work/makefile1$ 
mhr@ubuntu:~/work/makefile1$ make all
this is all
mhr@ubuntu:~/work/makefile1$ 

第一次执行 make all 时候,执行include关键字 所对应的规则,打印字符串病创建 test.txt文件。结果在当前目录生成了test.txt文件。

第二次执行 make all的时候,include关键字找到了 对应的test,txt文件,于是将 test.txt文件里面的内容拷贝过来了,就没必要再去执行 对应的规则了。

你可能感兴趣的:(Linux驱动,linux,运维,服务器)