Linux下AutoTools工具集使用教程


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

autoTools使用流程

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

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就已经自动生成了。

  • 1
  • 2
  • 下一页

相关内容