Makefile中.PHONY的作用


單詞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
     1    clean:
     2        rm -f foo
$ cat -n Makefile2
     1    .PHONY: clean
     2    clean:
     3        rm -f foo

Makefile1和Makefile2的差別就是在Makefile2中定義了:

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的行為沒有啥子區別嘛,呵呵

  • 創建一個文件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發揮了作用。

小結:

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

參考資料:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM