Makefile里的PHONY理解和实例

.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中的作用了吧,加油吧,少年!





  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fttony2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值