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.

参考资料:

posted @ 2018-07-13 18:09  veli  阅读(77829)  评论(0编辑  收藏  举报