Autoconf、automake工具组使用简单介绍 Autoconf、automake工具组是Unix/Linux下编译大型项目的有力工具。使用它们可以省去我们手工编写makefile文件的麻烦,而且还能提高可靠性和项目移植性。在这里简单介绍automake工具组的使用。 以大家都熟知的helloworld为例,首先建立专门的目录,如helloworld, $ mkdir helloworld $ cd helloworld/ $ vi helloworld.c 1 #include<stdio.h> 2 int main() 3 { 4 printf("Hello,World!\n"); 5 return 0; 6 } 运行autoscan: $ autoscan $ ls autoscan.log configure.scan helloworld.c $ vi configure.scan 1 # -*- Autoconf -*- 2 # Process this file with autoconf to produce a configure script. 3 4 AC_PREREQ([2.63]) //autoconf版本号 5 AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) //软件名称、版本和提交bug报告的邮箱等信息 6 AC_CONFIG_SRCDIR([helloworld.c]) //侦测指定的源码文件文件是否存在,来确定源码目录的有效性。此处为当前目录下的helloworld.c 7 AC_CONFIG_HEADERS([config.h]) //用以生成config.h文件,以便autoheader使用 8 9 # Checks for programs. 10 AC_PROG_CC //指定编译器,默认指定gcc 11 12 # Checks for libraries. 13 14 # Checks for header files. 15 16 # Checks for typedefs, structures, and compiler characteristics. 17 18 # Checks for library functions. 19 20 AC_OUTPUT //用来设定configure所要产生的文件,如果是makefile,configure会把它检查出来的结果带入makefile.in文件,产生合适的makefile。 configure.scan似乎只是个模板,还要根据自己的情况填写修改相应内容。修改后的内容为: 1 # -*- Autoconf -*- 2 # Process this file with autoconf to produce a configure script. 3 4 AC_PREREQ([2.63]) 5 AC_INIT(helloworld, 1.0) 6 AC_CONFIG_SRCDIR(helloworld.c) 7 AC_CONFIG_HEADERS(config.h) 8 AM_INIT_AUTOMAKE(helloworld,1.0) //添加该宏用于automake,参数分别是程序的名字和版本 9 10 # Checks for programs. 11 AC_PROG_CC 12 13 # Checks for libraries. 14 15 # Checks for header files. 16 17 # Checks for typedefs, structures, and compiler characteristics. 18 19 # Checks for library functions. 20 21 AC_OUTPUT(Makefile) 修改configure.scan的名字为configure.in: $ mv configure.scan configure.in $ ls autoscan.log configure.in helloworld.c 有的文章是改为configure.ac,可能是automake工具组不同版本的问题吧。 之后运行autoheader命令: $ autoheader ll 总计 20 drwxr-xr-x 2 zhl zhl 4096 09-23 23:17 autom4te.cache -rw-rw-r-- 1 zhl zhl 0 09-23 22:37 autoscan.log -rw-rw-r-- 1 zhl zhl 471 09-23 23:17 config.h.in -rw-rw-r-- 1 zhl zhl 474 09-23 23:08 configure.in -rw-rw-r-- 1 zhl zhl 71 09-23 22:36 helloworld.c -rw-rw-r-- 1 zhl zhl 79 09-23 23:12 Makefile.am 接着,创建Makefile.am文件,为生成Makefile.in文件做准备: $ vi Makefile.am 1 AUTOMAKE_OPTIONS=foreign 2 bin_PROGRAMS=helloworld 3 helloworld_SOURCES=helloworld.c 其中: 1、 AUTOMAKE_OPTIONS为设置automake的选项。GNU对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING等,否则automake执行时会报错。automake提供了3种软件等级:foreign、gnu和gnits,供用户选择,默认等级为gnu。本例使用foreign等级,它只检测必须的文件。 2、 bin_PROGRAMS定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。 3、 helloworld_SOURCES定义“helloworld”这个执行程序所需要的原始文件。如果“helloworld”这个程序是由多个原始文件所产生的,则必须把它用到的所有原始文件都列出来,并用空格隔开。例如:若目标体“helloworld”需要“helloworld.c”、“helloworld.h”两个依赖文件,则定义helloworld_SOURCES=helloworld.c helloworld.h。 继续运行aclocal命令: $ aclocal $ ls aclocal.m4 autoscan.log configure.in Makefile.am autom4te.cache config.h.in helloworld.c 运行autoconf命令: $ autoconf $ ls aclocal.m4 autoscan.log configure helloworld.c autom4te.cache config.h.in configure.in Makefile.am 使用automake命令生成Makefile.in文件,使用选项“—add-missing”可以让automake自动添加一些必需的脚本文件。 $ automake --add-missing configure.in:8: installing `./install-sh' configure.in:8: installing `./missing' $ ls aclocal.m4 autoscan.log configure helloworld.c Makefile.am missing autom4te.cache config.h.in configure.in install-sh Makefile.in 此时,运行自动配置文件configure,就可以把Makefile.in变成最终的Makefile文件了。 $ ./configure checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /bin/mkdir -p checking for gawk... gawk checking whether make sets $(MAKE)... yes 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 ISO C89... none needed checking for style of include used by make... GNU checking dependency style of gcc... none configure: creating ./config.status config.status: creating Makefile config.status: creating config.h config.status: executing depfiles commands $ ls aclocal.m4 config.h config.status helloworld.c Makefile.am stamp-h1 autom4te.cache config.h.in configure install-sh Makefile.in autoscan.log config.log configure.in Makefile missing 可以看到Makefile文件已经产生出来了,之后就可以使用方便的使用make命令了。 $ make make all-am make[1]: Entering directory `/home/zhl/pthread/helloworld' gcc -DHAVE_CONFIG_H -I. -g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo -c -o helloworld.o helloworld.c mv -f .deps/helloworld.Tpo .deps/helloworld.Po gcc -g -O2 -o helloworld helloworld.o make[1]: Leaving directory `/home/zhl/pthread/helloworld' $ ls aclocal.m4 config.h.in configure helloworld install-sh Makefile.in autom4te.cache config.log configure.in helloworld.c Makefile missing config.h config.status depcomp helloworld.o Makefile.am stamp-h1 可以看到已经生成可执行文件helloworld。 另外,我们还可以执行以下符合GNU Makefile惯例的Makefile预定义操作: make clean:清除上次的make命令所产生的object文件(后缀为“.o”的文件)及可执行的文件。 make install:将编译成功的可执行文件安装到系统目录中,一般为/usr/local/bin目录。 make dist:产生发布软件包文件(即distribution package)。这个命令将会将可执行文件及相关文件打包成一个tar.gz压缩的文件用来作为发布软件的软件包。 它会在当前目录下生成一个名字类似“PACKAGE-VERSION.tar.gz”的文件。PACKAGE和VERSION,是我们在configure.in中定义的AM_INIT_AUTOMAKE(PACKAGE,VERSION)。 make distcheck:生成发布软件包并对其进行测试检查,以确定发布包的正确性。这个操作将自动把压缩包文件解开,然后执行configure命令,并且执行make来确定编译不出现错误,最后提示你软件包已经准备好,可以发布了。 make distclean:类似make clean,但同时也将configure生成的文件全部删除掉,包括Makefile。
总结:本文章只能算抛砖引玉,使大家对automake有个大致的了解。automake工具组的使用绝不仅仅这么简单,还有许多复杂的配置,需要查询更多的资料。上面的讲解中使用了好几个文件和命令,它们之间的关系可能一时难以搞清楚。所以用一幅图来表示它们之间的关系。 |
|