2.7 Rules for Cleaning the Directory(清理目录的规则)

Compiling a program is not the only thing you might want to write rules for. Makefiles commonly(通常) tell how to do a few other things besides(除了) compiling a program: for example, how to delete all the object files and executables so that the directory is ‘clean’.

Here is how we could write a make rule for cleaning our example editor:

clean:
        rm edit $(objects)

In practice(实际上), we might want to write the rule in a somewhat(有点) more complicated(复杂的) manner to handle unanticipated(未预料到的) situations. We would do this:

.PHONY : clean
clean :
        -rm edit $(objects)

This prevents make from getting confused by an actual file called clean and causes it to continue in spite of errors from rm

这句话需要解释下:

  • .PHONY声明了clean是一个伪目标,而不是需要生成的文件,只是一个动作标签,如果没有加.PHONY声明时,而此时当前目录下正好有一个clean文件,则执行make clean命令时,是不会执行任何操作的。
  • 在rm的前面加上一个横杠(-),是防止rm命令执行失败而导致make操作中断。

A rule such as this should not be placed at the beginning of the makefile, because we do not want it to run by default! Thus, in the example makefile, we want the rule for edit, which recompiles the editor, to remain the default goal.

Since clean is not a prerequisite of edit, this rule will not run at all if we give the command ‘make’ with no arguments. In order to make the rule run, we have to type ‘make clean’. 

<<<返回主目录

发布了200 篇原创文章 · 获赞 37 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/wo198711203217/article/details/104333994