Linux上AutoTools工具集使用教程

Linux下AutoTools工具集使用教程

12.4 使用autoTools工具集

12.4.1  什么是autoTools

上几小节已经了解到了make工程管理器的强大功能。编写makefile 确实不是一件轻松的事,尤其对于一个较大的项目而言更是如此。那么,有没有一种轻松的手段生成makefile而同时又能让用户享受make 的优越性呢?本节要讲的autoTools系列工具正是为此而设的,它只需用户输入简单的目标文件、依赖文件、文件目录等就可以轻松地生成makefile了。另外,这些工具还可以完成系统配置信息的收集,从而可以方便地处理各种移植性的问题。也正是基于此,现在Linux上的软件开发一般都用autoTools 来生成makefile。

 

12.4.2  autoTools使用流程


正如上文所言,autoTools 是系列工具,它包含了aclocal、autoscan、autoconf、autoheader、和automake这些工具,使用autoTools主要就是利用各个工具的脚本文件来生成最后的makefile文件。其总体流程如下:

Linux上AutoTools工具集使用教程

图 12-1 autoTools生成makefile流程图

 

接下来用实例12-4 /*hello.c*/来做为实例:

1、建立一个hello的目录,这个目录将作为存放hello程序及其相关档案的地方。

2、用编辑器在hello目录下创建hello.c源文件,内容如下:

 

   #include <stdio.h>

   int main(int argc,char **argv)

{

    printf("Hello,Make!\n");

    return 0;

}

 

接下来就要用autoTools为我们生成makefile文档:

 

3、autoscan

[root@localhosthello]# autoscan

autom4te: configure.ac: no such file or directory

autoscan: /usr/bin/autom4te failed with exit status: 1

 

它会在给定目录及其子目录树中检查源文件,默认是在当前目录及其子目录树中进行检查。执行autoscan后会产生一个configure.scan的档案,我们可以用它做为configure.in文档的蓝本。但由上述结果可知autoscan会首先去读入configure.ac文件,但此时还没有创建该配置文件,于是它就自动生成了一个configure.scan文件。

 

[root@localhost hello]# ls

autoscan.log configure.scan  hello.c

 

4、autoconf

configure.in是autoconf的脚本配置文件,它的原型文件configure.scan改名为configure.in并把内容更改为如下所示:

# -*- Autoconf -*-             //以“#”号开始的行为注释。

AC_PREREQ(2.59)                //本文件要求的autoconf版本。

AC_INIT(hello,1.0)             // AC_INIT宏用来定义软件的名称和版本等信息。

AM_INIT_AUTOMAKE(hello,1.0)    //是automake所必备的宏,软件名称和版本号。

AC_CONFIG_SRCDIR([hello.c])    //用来侦测所指定的源码文件是否存在。

AC_CONFIG_HEADER([config.h])   //用于生成config.h文件,以便autoheader使用。

AC_PROG_CC

AC_CONFIG_FILES([Makefile])    //用于生成相应的Makefile 文件。

AC_OUTPUT

接下来运行aclocal,生成一个“aclocal.m4”文件,该文件是处理本地的宏定义。

 

 [root@localhost hello]# aclocal

 

再接着运行autoconf,生成configure可执行文件。

 

[root@localhost hello]#autoconf

[root@localhost hello]#ls

aclocal.m4  autom4te.cache autoscan.log  configure  configure.in  hello.c

 

5、autoheader

使用autoheader命令是为了成config.h.in文件。该工具通常会从acconfig.h文件中复制用户定义的符号,若此处没有自定义符号就不需创建acconfig.h文件。

 

[root@localhost hello]#autoheader

 

6、automake

这一步是创建makefile很重要的一步,automake用的脚本配置文件是Makefile.am,需要自己创建相应的文件。再用automake工具将其转成Makefile.in文件。内容如下所示:

 

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS= hello

hello_SOURCES= hello.c

 

AUTOMAKE_OPTIONS:设置automake的选项。由于GNU对自己发布的软件有严格的规范,如必须附带许可证声明文件COPYING等,否则automake执行时会报错。automake提供了3 种软件等级:foreign、gnu、gnits,我们这里用foreign只检测必要文件。

bin_PROGRAMS:定义要产生的可执行文件名。如果要产生多个执行文件用空格隔开。

hello_SOURCES:定义hello这个可执行文件所需要的源文件。如hello这个程序是由多个原始文件所产生的,则须把它所用到的所有源文件都列出来,并用空格隔开。例如定义:hello_SOURCES=hello.cstdio.h hello.h。注意:如果要定义多个执行文件,则对每个执行文件都要定义相应的_SOURCES。

 

接下来使用automake生成configure.in文件,在这里使用选项—adding-missing可以让automake自动添加有一些必需的脚本文件。如下所示:

 

[root@localhost hello]#automake --add-missing

configure.in: installing './install-sh'

configure.in: installing './missing'

Makefile.am: installing 'depcomp'

 

[root@localhost hello]#ls

aclocal.m4  autoscan.log  configure depcomp  install-sh   Makefile.in

autom4te.cache config.h.in   configure.in  hello.c Makefile.am  missing

 

7、configure

通过运行自动配置设置文件configure,把Makefile.in变成了最终的Makefile。

 

[root@localhost hello]#./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: creating config.h

config.status: executing depfiles commands

 

到此步makefile就已经自动生成了。

 

12.4.3  使用make命令

产生了makefile后我们就要使用make工程管理器去实现makefile的制定。

 

1、make

make默认执行make all命令,其执行情况如下所示:

 

[root@localhosthello]# make

make  all-am

make[1]: Entering directory `/root/hello'

if gcc -DHAVE_CONFIG_H -I.-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]: Leaving directory `/root/hello'

 

如果之前makefile操作无误,那么执行make命令会出现上面这段执行内容,而且会在目录下生成hello可执行文件,运行hello程序:

 

[root@localhosthello]# ./hello

Hello,Make!

 

2、make install

把hello程序安装到系统目录中去:

 

[root@localhosthello]# make install

make[1]: Entering directory `/root/hello'

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/hello'

 

此时,若直接运行hello能出现如下结果就说明操作正确:

 

[root@localhosthello]# hello

Hello,Make!

 

3、make clean

此命令是清除之前所编译的可执行文件及目标文件(object file, *.o):

 

[root@localhosthello]# make clean

test -z "hello" || rm -f hello

rm -f *.orm -f*.o

4、make dist

当我们做好了一切工作,最后一步是压缩文档以供发布:

 

[root@localhost hello]# make dist

 

[root@localhosthello]# ls hello-1.0.tar.gz

hello-1.0.tar.gz

 

hello-1.0-tar.gz就是我们最终要发布的压缩文件。autoTools的整个流程都介绍完毕,现实中更多会用到autoTools来为我们生成makefile文档。

 

12.5  小结

    本章介绍了make工程管理器的命令、作用和怎样编写简单实用的makefile,并且还讨论了make规则和make的一些有用的命令行选项。通过学习本章,同学们应该已经能够使用make来管理软件项目的的创建和维护过程了。