单词phony (即phoney)的意思是:伪造的,假的。来自collins的解释是:

If you describe something as phoney, you disapprove of it because it is false
rather than genuine.

那么,在Makefile中,.PHONY后面的target表示的也是一个伪造的target, 而不是真实存在的文件target,注意Makefile的target默认是文件。

举个例子:

$ cat -n Makefile1
clean:
rm -f foo
$ cat -n Makefile2
.PHONY: clean
clean:
rm -f foo

Makefile1和Makefile2的差别就是在Makefile2中定义了:

 .PHONY: clean
  • 直接Make看看
$ ls -l
total
-rw-r--r-- huanli huanli Jul : Makefile1
-rw-r--r-- huanli huanli Jul : Makefile2
$ make -f Makefile1 clean
rm -f foo
$ make -f Makefile2 clean
rm -f foo

Makefile1和Makefile2的行为没有啥子区别嘛,呵呵

  • 创建一个文件clean, 再make看看
$ touch clean
$ ls -l
total
-rw-r--r-- huanli huanli Jul : clean
-rw-r--r-- huanli huanli Jul : Makefile1
-rw-r--r-- huanli huanli Jul : Makefile2
$ make -f Makefile1 clean
make: 'clean' is up to date.
$ make -f Makefile2 clean
rm -f foo

区别来了,Makefile1拒绝了执行clean, 因为文件clean存在。而Makefile2却不理会文件clean的存在,总是执行clean后面的规则。由此可见,.PHONY clean发挥了作用。

小结:

.PHONY: clean
o means the word "clean" doesn't represent a file name in this Makefile;
o means the Makefile has nothing to do with a file called "clean"
in the same directory.

参考资料:

05-11 11:05