Makefile里的PHONY理解和实例

版权声明:版权归版主所有,转载需要声明出处 https://blog.csdn.net/fightingTony/article/details/80054642

.phony是一个特殊工作目标(special target) , 它用来指定一个假想的工作目标(也就是说phony后面的名字在make之后并不会生成一个相应名字的实际文件)而且肯定要视为未被更新(makefile只有在依赖文件更新之后才会进行再次make,此处未更新的意思是说.phony后面这个名字指定的文件未被更新,意思即生成此文件的条件总是满足,需要处理)

PHONY 目标并非实际的文件名:只是在显式请求时执行命令的名字。有两种理由需要使用PHONY 目标:
1 避免和同名文件冲突;

2 改善性能。

all : 8puzzle findpath minpathbucharest tests


minpathbucharest : min_path_to_Bucharest.cpp stlastar.h
	g++ -Wall min_path_to_Bucharest.cpp -o minpathbucharest

8puzzle : 8puzzle.cpp stlastar.h
	g++ -Wall 8puzzle.cpp -o 8puzzle

findpath : findpath.cpp stlastar.h
	g++ -Wall findpath.cpp -o findpath

tests : tests.cpp 8puzzle findpath minpathbucharest
	g++ -Wall tests.cpp -o tests

test: tests
	./tests

#.PHONY: clean
clean:
	rm -rfv 8puzzle findpath minpathbucharest tests

这时候,可以直接 

make clean && make
rm -rfv 8puzzle findpath minpathbucharest tests
g++ -Wall 8puzzle.cpp -o 8puzzle
g++ -Wall findpath.cpp -o findpath
g++ -Wall min_path_to_Bucharest.cpp -o minpathbucharest
g++ -Wall tests.cpp -o tests

但是,创建一个clean的文件之后就不一样了

touch clean
make clean && make 
make: “clean”是最新的。
make: 没有什么可以做的为 `all'。

因为clean文件已经存在,已经是最新的,就是不会重新,不会重新编译。

all : 8puzzle findpath minpathbucharest tests


minpathbucharest : min_path_to_Bucharest.cpp stlastar.h
	g++ -Wall min_path_to_Bucharest.cpp -o minpathbucharest

8puzzle : 8puzzle.cpp stlastar.h
	g++ -Wall 8puzzle.cpp -o 8puzzle

findpath : findpath.cpp stlastar.h
	g++ -Wall findpath.cpp -o findpath

tests : tests.cpp 8puzzle findpath minpathbucharest
	g++ -Wall tests.cpp -o tests

test: tests
	./tests

.PHONY: clean
clean:
	rm -rfv 8puzzle findpath minpathbucharest tests

取消掉这句前面的注释

.PHONY: clean

然后在编译一下,结果就不一样了

make clean && make
rm -rfv 8puzzle findpath minpathbucharest tests
已删除"8puzzle"
已删除"findpath"
已删除"minpathbucharest"
已删除"tests"
g++ -Wall 8puzzle.cpp -o 8puzzle
g++ -Wall findpath.cpp -o findpath
g++ -Wall min_path_to_Bucharest.cpp -o minpathbucharest
g++ -Wall tests.cpp -o tests
至此,应该能比较好的理解PHONY在Makefile中的作用了吧,加油吧,少年!





猜你喜欢

转载自blog.csdn.net/fightingTony/article/details/80054642
今日推荐