Makefile中.PHONY的做用

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

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

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

举个例子:app

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

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

1 .PHONY: clean
  • 直接Make看看
$ ls -l total 8
-rw-r--r-- 1 huanli huanli 18 Jul 13 17:51 Makefile1 -rw-r--r-- 1 huanli huanli 32 Jul 13 17:51 Makefile2 $ make -f Makefile1 clean rm -f foo $ make -f Makefile2 clean rm -f foo

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

  • 建立一个文件clean, 再make看看
$ touch clean $ ls -l total 8
-rw-r--r-- 1 huanli huanli  0 Jul 13 18:06 clean -rw-r--r-- 1 huanli huanli 18 Jul 13 17:51 Makefile1 -rw-r--r-- 1 huanli huanli 32 Jul 13 17:51 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发挥了做用。
spa

小结:code

.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.

参考资料:htm

相关文章
相关标签/搜索