.PHONY

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

     target ... : prerequisites ...

             command

             ...

             ...



比如 lz 要把一个 hello.cpp 文件编译成 hello

Perl code ?

1

2

3

4

5

6

7

all : hello another

 

hello : hello.cpp

    g++ -o $@ $<

 

another : another.cpp

    g++ -o $@ $<



直接 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都会生成。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值