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

相關文章
相關標籤/搜索