Makefile 中的.PHONY
2014年09月26日 00:12:40 a1232345 阅读数:4174
Makefile 中的.PHONY
一直不知道Makefile中.PHONY是什么意思,查了查便记下来。
所谓的PHONY这个单词就是伪造的意思,makefile中将.PHONY放在一个目标前就是指明这个目标是伪文件目标,如下:
.PHONY:clean
这里clean目标没有依赖文件,如果执行make命令的目录中出现了clean文件,由于其没有依赖文件,所以它永远是最新的,所以根据make的规则clean目标下的命令是不会被执行的。如下的例子:
[yangfan@dhcp-13-42 test]$ cat Makefile obj = 1.c 2.c 3.c 4.c all: touch $(obj) clean: rm -rf $(obj) [yangfan@dhcp-13-42 test]$ [yangfan@dhcp-13-42 test]$ make touch 1.c 2.c 3.c 4.c [yangfan@dhcp-13-42 test]$ ls 1.c 2.c 3.c 4.c Makefile [yangfan@dhcp-13-42 test]$ make clean rm -rf 1.c 2.c 3.c 4.c [yangfan@dhcp-13-42 test]$ ls Makefile [yangfan@dhcp-13-42 test]$ make touch 1.c 2.c 3.c 4.c [yangfan@dhcp-13-42 test]$ touch clean [yangfan@dhcp-13-42 test]$ ls 1.c 2.c 3.c 4.c clean Makefile [yangfan@dhcp-13-42 test]$ make clean make: `clean' is up to date. [yangfan@dhcp-13-42 test]$ ls 1.c 2.c 3.c 4.c clean Makefile [yangfan@dhcp-13-42 test]$
这个Makefile中all目标是创建空的1.c 2.c 3.c 和4.c 。 clean目标是删除这些文件,但是如果当前目录中出现了一个clean文件,在执行
make clean时就不会在执行clean目标下的命令了。下面看看在clean目标前加上.PHONY之后的情况:
[yangfan@dhcp-13-42 test]$ cat Makefile obj = 1.c 2.c 3.c 4.c all: touch $(obj) .PHONY: clean clean: rm -rf $(obj) [yangfan@dhcp-13-42 test]$ ls 1.c 2.c 3.c 4.c clean Makefile [yangfan@dhcp-13-42 test]$ make clean rm -rf 1.c 2.c 3.c 4.c [yangfan@dhcp-13-42 test]$ ls clean Makefile [yangfan@dhcp-13-42 test]$
用这个例子很好理解....
Makefile 的规则格式是这样的
Perl code ?
1 2 3 4 |
|
比如 lz 要把一个 hello.cpp 文件编译成 hello
Perl code ?
1 2 3 4 5 6 7 |
|
直接 make 或 make all 的话会执行 hello.cpp 和 another.cpp 的编译命令
后面不加参数的话,会把第一个目标作为默认的
make hello 的话只编译 hello.cpp
make another 的话只编译 another.cpp
Q:我的源码一个顶层目录下分成include source 两个目录,我顶层的目录的makefile要怎么写才能让在顶层make就执行source下的makefile?
all : xxxx
xxxx: xx
(cd source && make)
.phony是表示 目标是伪目标,并不生成相应的文件。.phony标志的文件总是执行的。
all : p1 p2 p3
.PHONY : all
这样在编译以后,不会生成all文件,但是p1,p2,p3都会生成。