现在的位置: 首页 > 综合 > 正文

makefile小结

2014年09月05日 ⁄ 综合 ⁄ 共 1697字 ⁄ 字号 评论关闭

首先牢记make的规则

A:B

     C

A依赖B,当B的日期比A新时,执行C

 

1.伪目标:

 

官方指南:

4.6 Phony Targets

http://www.gnu.org/software/make/manual/make.html#Phony-Targets

A phony target is one that is not really the name of a file; rather it is just a name for a recipe to be executed when you make an explicit request. There are two reasons to use a phony target: to avoid a conflict with a file of the same name, and to improve performance.

If you write a rule whose recipe will not create the target file, the recipe will be executed every time the target comes up for remaking. Here is an example:

     clean:
             rm *.o temp

Because the rm command does not create a file named clean, probably no such file will ever exist. Therefore, the rm command will be executed every time you say ‘make clean’. The phony target will cease to work if anything ever does create a file named clean in this directory. Since it has no prerequisites, the file clean would inevitably be considered up to date, and its recipe would not be executed. To avoid this problem, you can explicitly declare the target to be phony, using the special target .PHONY (see Special Built-in Target Names) as follows:

     .PHONY : clean

Once this is done, ‘make clean’ will run the recipe regardless of whether there is a file named clean.

Since it knows that phony targets do not name actual files that could be remade from other files, make skips the implicit rule search for phony targets (see Implicit Rules). This is why declaring a target phony is good for performance, even if you are not worried about the actual file existing.

 

 

   伪目标是一个标签,它的目的不在于生成文件,而是执行命令(可能是命令不以生成文件为目的。不用太纠结于定义,记住MAKE的规则就能知道它怎么做的)

  

clean:

rm *.o temp

 

 

clean 依赖于空的文件。个人认为在出现这种空依赖时,

A)如果当前目录没有同名文件或文件夹时, make将其认为是.PHONY。

所以当我们敲make clean或make(如果clean在第一个)时,它会被执行(伪目标是用来执行命令的,所以当你调用时,它会被执行)

 

B)如果有同名的情况下,它会当普通规则来处理。因为目录下的clean文件总是比它的空依赖新,所以命令不会被执行。

 

 

    all: proc1 proc2

 

proc1:

         $(MAKE) -C src/proc1

.........

 

由于all依赖于proc1, proc2,所以它会去检查这两个的情况。而proc1是个伪目标(注意重名),所以它会执行下面的命令。

 

抱歉!评论已关闭.