- 浏览: 4729874 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
我使用过的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工具的做法这是肯定的。
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所在目录的源程序找出来,然后按照依赖关系进行编译。
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 : $(OBJS)
clean :
@$(RM) *.o
@$(RM) *.d
veryclean: clean
@$(RM) $(TARGETS)
@$(RM) cscope.out
@$(RM) core*
vc: veryclean
ct:
@$(RM) $(TARGETS)
rl: ct everything
rebuild: veryclean everything
ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS) :
@$(RM) $(patsubst %.d,%.o,$@)
endif
-include $(DEPS)
怎么来使用这个common.mk来帮助我们编写makefile文件呢,首先我们来看一下编译成静态库的情况。见下面文件,其中的libhyfcd.a就是目标静态库文件的名称,INCS定义了依赖的包含文件路径。
BIN1 = libhyfcd.a
BIN1_OBJS = $(OBJS)
BIN1_LIBS =
BIN1_LFLAGS =
INCS = -I.. -I../../hycu2 -I/usr/include/mysql
#CC := g++
CC := gcc
CXX := gcc
CFLAGS := -g $(INCS) -Wall -D_REENTRANT -D__DECLSPEC_SUPPORTED -DOPENSSL_NO_KRB5 #-DNDEBUG
CXXFLAGS := $(CFLAGS)
include common.mk
# $(BIN1) : $(BIN1_OBJS)
# $(CC) -g -o $@ $(BIN1_LFLAGS) $^ $(addprefix -l,$(BIN1_LIBS))
$(BIN1): $(BIN1_OBJS)
ar rcs $@ $^
再来看一下编译成可执行文件的情况。见下面文件,其中msgc就是目标执行文件,BIN1_LIBS是依赖的库。
BIN1 = msgc
BIN1_OBJS = $(OBJS)
BIN1_LIBS = curses pthread
BIN1_LFLAGS = #-L../hyfc/lib
INCS = #-I../hyfc
CC := g++
CFLAGS := -g $(INCS) -Wall
#CFLAGS := -g $(INCS) -Wall -DLOG_CHECK
#CFLAGS := -g $(INCS) -Wall #-DNDEBUG
CXXFLAGS := $(CFLAGS)
include /usr/include/hyfc/common.mk
$(BIN1) : $(BIN1_OBJS)
$(CC) -g -o $@ $(BIN1_LFLAGS) $^ $(addprefix -l,$(BIN1_LIBS))
补充(2011.08.07):列位看官,如果你关注的是Makefile的语法结构,那么不妨看参考资料【5】《GNU make中文手册》。
常用参数
格式:make
使用默认的makefile文件进行编译,按照GNUmakefile, makefile和Makefile的顺序进行查找。编译目标all。
格式:make -f makefile.debug
使用指定的makefile进行编译,此处就是makefile.debug。编译目标all。
格式:make install
编译目标install。通常用于安装软件。
格式:make clean
编译目标clean。通常用于清除目标文件.o。
格式:make veryclean
编译目标clean。通常用于清除目标文件.o以及执行文件等,意思是干净的清除掉除makefile和源程序之外的文件。
格式:make rl
编译目标rl。rl是relink的缩写,即重新链接,常用于某个依赖的库文件发生变化时强制重新链接生成执行文件。
使用示例
示例一 编译安装mysql++2.3.2的过程
[root@jfht setup]# ls mysql++-2.3.2.tar.gz
mysql++-2.3.2.tar.gz
[root@jfht setup]# tar zxf mysql++-2.3.2.tar.gz
[root@jfht setup]# cd mysql++-2.3.2
[root@jfht mysql++-2.3.2]# ./configure --prefix=/usr
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for ranlib... ranlib
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln -s works... yes
checking whether make sets $(MAKE)... yes
checking for ar... ar
checking for strip... strip
checking for nm... nm
checking if make is GNU make... yes
checking for dependency tracking method... gcc
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ANSI C... (cached) none needed
checking how to run the C preprocessor... gcc -E
checking for egrep... grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking zlib.h usability... yes
checking zlib.h presence... yes
checking for zlib.h... yes
checking for gzread in -lz... yes
checking whether -lm is needed to use C math functions... no
checking whether -lsocket is needed... no
checking whether -lnsl is needed... no
checking for MySQL library directory... /usr/lib
checking for MySQL include directory... /usr/include/mysql
checking for mysql_store_result in -lmysqlclient... yes
checking for mysql_ssl_set in -lmysqlclient... yes
checking for localtime_r()... yes
checking for main in -lintl... no
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for STL slist extension... <ext/slist>, namespace __gnu_cxx
configure: creating ./config.status
config.status: creating Makefile
config.status: creating mysql++.spec
config.status: creating lib/Doxyfile
config.status: creating lib/mysql++.h
config.status: creating config.h
[root@jfht mysql++-2.3.2]# make && make install
此处省略较多输出。
/usr/bin/install -c -d /usr/lib
/usr/bin/install -c -m 644 libmysqlpp.so /usr/lib
/usr/bin/install -c libmysqlpp.so.2.3.2 /usr/lib
(cd /usr/lib ; rm -f libmysqlpp.so libmysqlpp.so.2; ln -s libmysqlpp.so.2.3.2 libmysqlpp.so.2; ln -s libmysqlpp.so.2 libmysqlpp.so)
/usr/bin/install -c -d /usr/include/mysql++
(cd . ; /usr/bin/install -c -m 644 lib/*.h /usr/include/mysql++)
[root@jfht mysql++-2.3.2]#
示例二 使用common.mk进行编译的例子
[root@jfht src]# make
make: Nothing to be done for `everything'.
[root@jfht src]# make clean
[root@jfht src]# make
g++ -g -Wall -MD -c -o compiler.o compiler.cpp
g++ -g -Wall -MD -c -o file_updater.o file_updater.cpp
g++ -g -Wall -MD -c -o file_util.o file_util.cpp
g++ -g -Wall -MD -c -o gen_async.o gen_async.cpp
g++ -g -Wall -MD -c -o gen_c.o gen_c.cpp
g++ -g -Wall -MD -c -o gen_cpp.o gen_cpp.cpp
g++ -g -Wall -MD -c -o gen_fmdo2.o gen_fmdo2.cpp
g++ -g -Wall -MD -c -o gen_fmdo.o gen_fmdo.cpp
g++ -g -Wall -MD -c -o gen_hyfc.o gen_hyfc.cpp
g++ -g -Wall -MD -c -o gen_hyfcw.o gen_hyfcw.cpp
g++ -g -Wall -MD -c -o gen_java.o gen_java.cpp
g++ -g -Wall -MD -c -o gen_jdom.o gen_jdom.cpp
g++ -g -Wall -MD -c -o gen_jt.o gen_jt.cpp
g++ -g -Wall -MD -c -o gen_jxh.o gen_jxh.cpp
g++ -g -Wall -MD -c -o gen_pas.o gen_pas.cpp
g++ -g -Wall -MD -c -o gen_php.o gen_php.cpp
g++ -g -Wall -MD -c -o gen_struts.o gen_struts.cpp
g++ -g -Wall -MD -c -o gen_tag.o gen_tag.cpp
g++ -g -Wall -MD -c -o gen_udpsw.o gen_udpsw.cpp
g++ -g -Wall -MD -c -o gen_wsdl.o gen_wsdl.cpp
g++ -g -Wall -MD -c -o gen_xml.o gen_xml.cpp
g++ -g -Wall -MD -c -o msgc.o msgc.cpp
g++ -g -Wall -MD -c -o string_util.o string_util.cpp
g++ -g -o msgc compiler.o file_updater.o file_util.o gen_async.o gen_c.o gen_cpp.o gen_fmdo2.o gen_fmdo.o gen_hyfc.o gen_hyfcw.o gen_java.o gen_jdom.o gen_jt.o gen_jxh.o gen_pas.o gen_php.o gen_struts.o gen_tag.o gen_udpsw.o gen_wsdl.o gen_xml.o msgc.o string_util.o -lcurses -lpthread
cp -af msgc /usr/bin
cp -af msgc msgc.`uname -r`
[root@jfht src]# make rl
g++ -g -o msgc compiler.o file_updater.o file_util.o gen_async.o gen_c.o gen_cpp.o gen_fmdo2.o gen_fmdo.o gen_hyfc.o gen_hyfcw.o gen_java.o gen_jdom.o gen_jt.o gen_jxh.o gen_pas.o gen_php.o gen_struts.o gen_tag.o gen_udpsw.o gen_wsdl.o gen_xml.o msgc.o string_util.o -lcurses -lpthread
cp -af msgc /usr/bin
cp -af msgc msgc.`uname -r`
[root@jfht src]#
问题思考
1. 很多自由软件中使用的Makefile是由autoconf和automake来生成的,怎么使用法?
2. Makefile中一些常见的指令的含义如何?
相关资料
【1】LinuxSky Linux平台Makefile文件的编写基础篇
http://www.linuxsky.org/doc/dev/200709/117.html
【2】学习在线 Linux/Unix环境下的make命令详解
http://www.3648.com/article/sort01/sort021/sort086/info-2667.html
【3】鸟哥的Linux私房菜 第二十二章、軟體安裝:原始碼與 Tarball
http://linux.vbird.org/linux_basic/0520source_code_and_tarball.php#make
【4】新浪学园 Linux下Makefile的automake生成全攻略
http://tech.sina.com.cn/s/2004-10-19/1115443045.shtml
【5】GNU make中文手册
http://www.linuxsir.org/main/doc/gnumake/GNUmake_v3.80-zh_CN_html/index.html#content
发表评论
-
在Linux用tar归档压缩文件时忽略某些文件和目录
2013-02-01 10:19 16913在Linux下,常用tar对文 ... -
使用nmap扫描服务器端口的一次操作
2012-11-01 17:00 15069使用nmap扫描服务器端口的一次操作 本文来自:http ... -
我使用过的Linux命令之wget - ooo
2011-09-14 13:10 0我使用过的Linux命令之wg ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:53 9078简明 Vim 练级攻略 http://coolshell.c ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:49 1简明 Vim 练级攻略 http://coolshell.c ... -
我使用过的Linux命令之:(冒号) - 啥也不做(除了……)
2011-08-29 13:18 11967我使用过的Linux命令之: ... -
我使用过的Linux命令之date - 显示、修改系统日期时间
2011-08-25 09:21 41895我使用过的Linux命令之da ... -
我使用过的Linux命令之declare - 声明shell变量(不知道没关系、知道了就更好的内建命令)
2011-08-16 09:22 21731我使用过的Linux命令之declare - 声明shell变 ... -
我使用过的Linux命令之alias - 设置命令的别名,让 Linux 命令更简练
2011-08-11 09:31 28698我使用过的Linux命令之alias - 设置命令的别名,让 ... -
我使用过的Linux命令之ar - 创建静态库.a文件
2011-08-08 10:40 51801我使用过的Linux命令之ar - 创建静态库.a文件 本 ... -
我使用过的Linux命令之crontab - 设置例行任务(类似于Windows中的任务计划)
2011-08-04 22:26 9686我使用过的Linux命令之crontab - 设置例行任务(类 ... -
我使用过的Linux命令之chmod - 改变文件/目录的访问权限
2011-08-03 21:33 10634我使用过的Linux命令之chmod - 改变文件/目录的访问 ... -
我使用过的Linux命令之export - 设置或显示环境变量
2011-08-02 19:55 25356我使用过的Linux命令之export - 设置或显示环境变量 ... -
我使用过的Linux命令之wc - 统计文件行数、单词数或字节数
2011-07-26 10:50 28918我使用过的Linux命令之wc - 统计文件行数、单词数或字节 ... -
我使用过的Linux命令之groupdel - 删除群组
2011-07-22 22:13 9611我使用过的Linux命令之gr ... -
我使用过的Linux命令之ifconfig - 网络配置命令
2011-07-21 20:43 60356我使用过的Linux命令之ifconfig - 网络配置命令 ... -
我使用过的Linux命令之ll - 列出文件/目录的详细信息
2011-07-20 21:22 7093我使用过的Linux命令之ll ... -
我使用过的Linux命令之mkdir - 创建目录
2011-07-20 20:58 13599我使用过的Linux命令之mkdir - 创建目录 本文链 ... -
我使用过的Linux命令之perror - 解释错误代码
2011-07-18 20:29 24948我使用过的Linux命令之perror - 解释错误代码 ... -
我使用过的Linux命令之ping - 测试与目标主机的连通性
2011-07-16 10:46 26204我使用过的Linux命令之ping - 测试与目标主机的连通性 ...
相关推荐
linux内核配置make-menuconfig菜单详解.doc
make-3.81.exe 测试可以安装,win10,windows make命令 安装 make-3.81.exe
android-4.4-源码编译 linux-x86/bin目录下的工具 包含make_ext4fs工具;unyaffs;imgdiff;android工具
linux离线安装gcc、make、build-essential、Realtek8125b网卡驱动,解压后查看说明,按照说明一步到位,无需网络。
mingw32-make-3.80.0-3 a linux shell under windows system
./configure CC="/home/toolchain/broadcom/mipsel-unknown-linux-gnu/bin/mipsel-unknown-linux-gnu-gcc-4.4.6" --host=mipsel-linux (路径根据自己环境实际调整) make 该目录下生成可在目标系统执行文件:gzip ...
GitBash Windows终端工具MinGW64下执行makefile编译make命令报错:“make: command not found” 此时你需要安装次扩展工具包。 使用方法:解压后把所有文件夹及其文件复制到目录合并:C:\Program Files\Git\mingw64 ...
官方离线安装包,测试可用。请使用rpm -ivh [rpm完整包名] 进行安装
cmder软件windows 命令行工具使用linux命令,免翻墙下载。 Cmder是一个软件包,它是由于Windows上缺少优秀的控制台模拟器而产生的。它基于令人惊叹的软件,并添加了Monokai配色方案和自定义提示布局,从一开始就看...
在 Linux(unix)环境下使用 GNU 的 make 工具能够比较容易的构建一个属于你自己的工程,整个工程的编译只需要一个命令就可以完成编译、连接以至于最后的执行。不过这需要我们投入一些时间去完成一个或者多个称之为 ...
还有其它平台使用如 Ant、Automake、CMake等等,而GNU社区也有自己的自动化编译工具,它就是 GNU make。很早之前深谙自动化编译的重要,只是一直没有机会写一篇相关的文章,这次真的是巧遇了,@亦楠 同学在《head...
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz(安装步骤).txt 截至2017.11.08最新新mysql for linux的安装步骤。 glibc版本是不需要make的。 所以选择这个安装包不需要任何依赖包,包你100%安装成功
windows下的make工具。在编译NRF52系列的Bootloater的时候会用到这个工具来生成.micro_ecc_lib_nrf52.lib文件。
官方离线安装包,亲测可用
若要自定义交叉编译工具,比如在嵌入式系统运行,则只要修改 unix\Makefile文件下的: #CC = cc 这句屏蔽 并修改对应的路径 CC = /home/mipsel-unknown-linux-gnu/bin/mipsel-unknown-linux-gnu-gcc-4.4.6 然后重新...
mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz(安装步骤).txt 截至2016.04.20最新新mysql for linux的安装步骤。 glibc版本是不需要make的。 所以选择这个安装包不需要任何依赖包,包你100安装成功
linux rpm安装包,可直接在linux arm64版本上安装使用
make-4.3.tar.gz
可以直接make的linux-0.11源码,得到Image