日期:2014-05-16  浏览次数:20744 次

我使用过的Linux命令之make - 强大的编译工具

我使用过的Linux命令之make - 强大的编译工具

本文链接:http://codingstandards.iteye.com/blog/969924 ? (转载请注明出处)

?

用途说明

make命令是一个常用的编译命令,尤其是在开发C/C++程序时,它通过Makefile文件中描述的源程序之间的依赖关系来自动进行编译。Makefile文件是按照规定的格式编写的,文件中需要说明如何编译各个源文件并连接生成可执行文件,并要求定义源文件之间的依赖关系。在首次执行make时,会将所有相关的文件都进行编译,而在以后make时,通常是进行增量编译,即只对修改过的源代码进行编译。许多Tarball格式的开源软件,在解压之后,一般先执行./configure,然后执行make,再执行make install进行安装。在进行Java编译时,我们常用的是ant,这个ant工具的发明乃是由于James被makefile的特殊格式弄烦了,采用XML格式来描述任务之间的关系,但是ant工具借鉴了make工具的做法这是肯定的。

man make 写道
The purpose of the make utility is to determine automatically which pieces of a large program need to be recom-
piled, and issue the commands to recompile them. The manual describes the GNU implementation of make, which
was written by Richard Stallman and Roland McGrath, and is currently maintained by Paul Smith. Our examples
show C programs, since they are most common, but you can use make with any programming language whose compiler
can be run with a shell command. In fact, make is not limited to programs. You can use it to describe any
task where some files must be updated automatically from others whenever the others change.

To prepare to use make, you must write a file called the makefile that describes the relationships among files
in your program, and the states the commands for updating each file. In a program, typically the executable
file is updated from object files, which are in turn made by compiling source files.

Once a suitable makefile exists, each time you change some source files, this simple shell command:

make

suffices to perform all necessary recompilations. The make program uses the makefile data base and the last-
modification times of the files to decide which of the files need to be updated. For each of those files, it
issues the commands recorded in the data base.

make executes commands in the makefile to update one or more target names, where name is typically a program.
If no -f option is present, make will look for the makefiles GNUmakefile, makefile, and Makefile, in that
order.

Normally you should call your makefile either makefile or Makefile. (We recommend Makefile because it appears
prominently near the beginning of a directory listing, right near other important files such as README.) The
first name checked, GNUmakefile, is not recommended for most makefiles. You should use this name if you have a
makefile that is specific to GNU make, and will not be understood by other versions of make. If makefile is
‘-’, the standard input is read.

make updates a target if it depends on prerequisite files that have been modified since the target was last
modified, or if the target does not exist.

使用make进行编译的关键点就是掌握makefile的编写规则,make的手册页中说道,makefile文件可以是GNUmakefile, makefile或者Makefile,但是推荐使用Makefile,因为在列出某个目录的文件时,使用Makefile作为文件名时将被排在前面。makefile文件也像C/C++代码一样支持include方式,即把一些基本的依赖规则写在一个公共的文件中,然后其他makefile文件包含此文件。我所使用的公共makefile文件名为common.mk,是多年以前从一个高人那儿拷贝而来的,现在贡献给大家。里面有些晦涩难懂的makefile指令,但是对于使用者来说可以不必关注。思路就是将makefile所在目录的源程序找出来,然后按照依赖关系进行编译。

common.mk 写道
#This is the common part for makefile

SOURCE := $(wildcard *.c) $(wildcard *.cc) $(wildcard *.cpp)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cc,%.o,$(patsubst %.cpp,%.o,$(SOURCE))))
DEPS := $(patsubst %.o,%.d,$(OBJS))
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
CPPFLAGS += -MD

.PHONY : everything objs clean veryclean vc rebuild ct rl

everything : $(TARGETS)

objs : $(OBJ