Linux 环境下 Makefile 文件制作浅谈(一)(转)[@more@]
Linux 环境下 Makefile 文件制作浅谈(一)
编写:Leaf Zhou
EMAIL:leaf_zhou_8@hotmail.com
可自由复制但禁止删改
2003-10-12
无论对于一个初学者还是一个资深的Linux程序员,编写Makefile文件都是一件很麻烦的事;再者,开发人员应该把主要的精力放在程序代码的编写上,而在Makefile文件花费太多的精力显然是不明智的;还有,对于不同的处理器架构,往往编译器不同,环境不同,特别是一些嵌入式系统中的各种程序的编译,于是移植问题也使Makefile文件编写趋于复杂,也显得这个问题很重要。对于这些问题Linux的高手们早已想到了,所以他们开发了一些能够自动生成Makefile文件的工具。他们就是下面这些工具:
〉GNU Automake
〉GNU Autoconf
〉GNU m4
〉perl
〉GNU Libtool
因此您的操作系统必须安装以上软件,否则就不能够自动生成Makefile文件或者在生成的过程中出现各种各样的问题。用 autoconf/automake/autoheader工具来处理各种移植性的问题,用这些工具完成系统配置信息的收集,制作Makefile文件。然后在打算编译源码时只需要通过 "./configure; make"这样简单的命令就可以得到干净利落的编译。
制作Makefile文件需要以下几步:
1〉建立编译目录和源代码目录及源代码文件(即要编译的源文件)
[root@localhost leaf]#mkdir testmk
[root@localhost leaf]#cd testmk
[root@localhost testmk]#vi hello.c
编辑hello.c文件如下内容:
/*filename:hello.c*/
#include
int main(int argc,char **argv){printf("%s
","Hello, World!")return 0;}2〉利用autoscan工具产生一个configure.in文件的模板文件configure.scan文件:[root@localhost testmk]#autoscan[root@localhost testmk]#lsconfigure.scan hello.c3〉把configure.scan文件更名为configure.in文件,并编译其内容如下:[root@localhost testmk]#mv configure.scan configure.in[root@localhost testmk]#vi configure.indnl Process this file with autoconf to produce a configure script.AC_INIT(hello.c)dnl Add the file by leafAM_INIT_AUTOMAKE(hello,1.0)dnl Checks for programs.AC_PROG_CCdnl Checks for libraries.dnl Checks for header files.dnl Checks for typedefs, structures, and compiler characteristics.dnl Checks for library functions.AC_OUTPUT(Makefile)4〉执行aclocal,会产生aclocal.m4文件[root@localhost testmk]#aclocal[root@localhost testmk]#lsaclocal.m4 configure.in hello.c5〉执行autoconf,会产生confiure文件[root@localhost testmk]#autoconf[root@localhost testmk]#lsaclocal.m4 [autom4te.cache] configure configure.in hello.c6〉创建文件Makefile.am并编辑其内容如下:AUTOMAKE_OPTIONS=foreignbin_PROGRAMS=hellohello_SOURCES=hello.c其中,hello为编译后产生的可执行文件名称,而第三行等号后面为源文件列表7〉执行automake程序,automake程序会根据Makefile.am产生一些文件,其中最重要的是Makefile.in文件:[root@localhost testmk]#automake --add-missingconfigure.in: installing `./install-sh'configure.in: installing `./mkinstalldirs'configure.in: installing `./missing'Makefile.am: installing `./depcomp'[root@localhost testmk]#lsaclocal.m4 [autom4te.cache] configure configure.in depcomphello.c install-sh Makefile.am Makefile.in missingmkinstalldirs8〉执行configure脚本,生成我们需要的Makefile文件。[root@localhost testmk]#./configurechecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables...checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ANSI C... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3configure: creating ./config.statusconfig.status: creating Makefileconfig.status: executing depfiles commands9〉最后只执行make就大功告成了:[root@localhost testmk]#makesource='hello.c' object='hello.o' libtool=no depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo' depmode=gcc3 /bin/sh ./depcomp gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c `test -f 'hello.c' || echo './'`hello.cgcc -g -O2 -o hello hello.o备注:1.以上内容均在RedHat Linux 9.0环境下测试通过。2.参考书目《Linux 程序设计权威指南》于明俭、陈向阳、方汉编著3.其它国内外网站资料4.RedHat 9.0下带的程序文件及版本autoconf-2.57-3.noarch.rpmautomake-1.6.3-5.noarch.rpmgcc-3.2.2-5.i386.rpm
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8225414/viewspace-944612/
,如需转载,请注明出处,否则将追究法律责任。