|
autotools是系列工具,读者首先要确认系统是否装了以下工具(可以用which命令进行查看)。
使用autotools主要就是利用各个工具的脚本文件以生成最后的Makefile。其总体流程是这样的:
接下来,笔者将通过一个简单的hello.c例子带领读者熟悉autotools生成makefile的过程,由于在这过程中有涉及到较多的脚本文件,为了更清楚地了解相互之间的关系,强烈建议读者实际动手操作以体会其整个过程。 1.autoscan它会在给定目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目录树中进行检查。它会搜索源文件以寻找一般的移植性问题并创建一个文件“configure.scan”,该文件就是接下来autoconf要用到的“configure.in”原型。如下所示: [root@localhost automake]# autoscan autom4te: configure.ac: no such file or directory autoscan: /usr/bin/autom4te failed with exit status: 1 [root@localhost automake]# ls autoscan.log configure.scan hello.c 如上所示,autoscan首先会尝试去读入“configure.ac”(同configure.in的配置文件)文件,此时还没有创建该配置文件,于是它会自动生成一个“configure.in”的原型文件“configure.scan”。 2.autoconfconfigure.in是autoconf的脚本配置文件,它的原型文件“configure.scan”如下所示: # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ(2.59) #The next one is modified by sunq #AC_INIT(FULL-PACKAGE-NAME,VERSION,BUG-REPORT-ADDRESS) AC_INIT(hello,1.0) # The next one is added by sunq AM_INIT_AUTOMAKE(hello,1.0) AC_CONFIG_SRCDIR([hello.c]) AC_CONFIG_HEADER([config.h]) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_CONFIG_FILES([Makefile]) AC_OUTPUT 下面对这个脚本文件进行解释:
效性。在此处为当前目录下的hello.c。
接下来首先运行aclocal,生成一个“aclocal.m4”文件,该文件主要处理本地的宏定义。如下所示: [root@localhost automake]# aclocal 再接着运行autoconf,生成“configure”可执行文件。如下所示: [root@localhost automake]# autoconf [root@localhost automake]# ls aclocal.m4 autom4te.cache autoscan.log configure configure.in hello.c 3.autoheader接着使用autoheader命令,它负责生成config.h.in文件。该工具通常会从“acconfig.h”文件中复制用户附加的符号定义,因此此处没有附加符号定义,所以不需要创建“acconfig.h”文件。如下所示: [root@localhost automake]# autoheader 4.automake这一步是创建Makefile很重要的一步,automake要用的脚本配置文件是Makefile.am,用户需要自己创建相应的文件。之后,automake工具转换成Makefile.in。在该例中,笔者创建的文件为Makefile.am如下所示: AUTOMAKE_OPTIONS=foreign bin_PROGRAMS= hello hello_SOURCES= hello.c 下面对该脚本文件的对应项进行解释。
接下来可以使用automake对其生成“configure.in”文件,在这里使用选项“—adding-missing”可以让automake自动添加有一些必需的脚本文件。如下所示: [root@localhost automake]# automake –add-missing configure.in: installing ‘./install-sh’ configure.in: installing ‘./missing’ Makefile.am: installing ‘depcomp’ [root@localhost automake]# ls aclocal.m4 autoscan.log configure.in hello.c Makefile.am missing autom4te.cache configure depcomp install-sh Makefile.in config.h.in 可以看到,在automake之后就可以生成configure.in文件。 5.运行configure在这一步中,通过运行自动配置设置文件configure,把Makefile.in变成了最终的Makefile。如下所示: [root@localhost automake]# ./configure checking for a BSD-compatible install… /usr/bin/install -c checking whether build enVironment is sane… yes 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 ANSI C… none needed checking for style of include used by make… GNU checking dependency style of Gcc… Gcc3 configure: creating ./config.status config.status: creating Makefile config.status: executing depfiles commands 可以看到,在运行configure时收集了系统的信息,用户可以在configure命令中对其进行方便地配置。在./configure的自定义参数有两种,一种是开关式(–enable-XXX或–disable-XXX),另一种是开放式,即后面要填入一串字符(–with-XXX=yyyy)参数。读者可以自行尝试其使用方法。另外,读者可以查看同一目录下的”config.log”文件,以方便调试之用。 到此为止,makefile就可以自动生成了。回忆整个步骤,用户不再需要定制不同的规则,而只需要输入简单的文件及目录名即可,这样就大大方便了用户的使用。下面的图3.9总结了上述过程: ![]() autotools生成Makefile流程图 图 autotools生成Makefile流程图 6.make键入make默认执行”make all”命令,即目标体为all,其执行情况如下所示: [root@localhost automake]# make if Gcc -DPACKAGE_NAME=\”\” -DPACKAGE_TARNAME=\”\” -DPACKAGE_VERSION=\”\” -DPACKAGE_STRING=\”\” -DPACKAGE_BUGREPORT=\”\” -DPACKAGE=\”hello\” -DVERSION=\”1.0\” -I. -I. -g -O2 -MT hello.o -MD -MP -MF “.deps/hello.Tpo” -c -o hello.o hello.c; \ then mv -f “.deps/hello.Tpo” “.deps/hello.Po”; else rm -f “.deps/hello.Tpo”; exit 1; fi Gcc -g -O2 -o hello hello.o 此时在本目录下就生成了可执行文件“hello”,运行“./hello”能出现正常结果,如下所示: [root@localhost automake]# ./hello Hello!Autoconf! 7.make install此时,会把该程序安装到系统目录中去,如下所示: [root@localhost automake]# make install if Gcc -DPACKAGE_NAME=\”\” -DPACKAGE_TARNAME=\”\” -DPACKAGE_VERSION=\”\” -DPACKAGE_STRING=\”\” -DPACKAGE_BUGREPORT=\”\” -DPACKAGE=\”hello\” -DVERSION=\”1.0\” -I. -I. -g -O2 -MT hello.o -MD -MP -MF “.deps/hello.Tpo” -c -o hello.o hello.c; \ then mv -f “.deps/hello.Tpo” “.deps/hello.Po”; else rm -f “.deps/hello.Tpo”; exit 1; fi Gcc -g -O2 -o hello hello.o make[1]: Entering directory ‘/root/workplace/automake’ test -z “/usr/local/bin” || mkdir -p — “/usr/local/bin” /usr/bin/install -c ‘hello’ ‘/usr/local/bin/hello’ make[1]: Nothing to be done for ‘install-data-am’. make[1]: LeaVing directory ‘/root/workplace/automake’ 此时,若直接运行hello,也能出现正确结果,如下所示: [root@localhost automake]# hello Hello!Autoconf! 8.make clean此时,make会清除之前所编译的可执行文件及目标文件(object file, *.o),如下所示: [root@localhost automake]# make clean test -z “hello” || rm -f hello rm -f *.o 9.make dist此时,make将程序和相关的文档打包为一个压缩文档以供发布,如下所示: [root@localhost automake]# make dist [root@localhost automake]# ls hello-1.0-tar.gz hello-1.0-tar.gz 可见该命令生成了一个hello-1.0-tar.gz的压缩文件。 由上面的讲述读者不难看出,autotools确实是软件维护与发布的必备工具,也鉴于此,如今GUN的软件一般都是由automake来制作的。 |
autotools生成makefile文件实例
最新推荐文章于 2020-01-09 02:12:24 发布
本文详细介绍了如何使用Autotools工具集自动生成Makefile文件。从安装所需工具开始,逐步演示了从创建基本的C语言程序到最终生成Makefile的全过程。
autotools生成makefile文件实例
2009年11月29日 星期日 下午 02:00

1224

被折叠的 条评论
为什么被折叠?



