初识autoconf和automake

来源:互联网 发布:淘宝摄影室内布光 编辑:程序博客网 时间:2024/04/28 05:05

    如果需要了解autoconf和automake,那一定需要先了解Makefile。

    Makefile其按类型、功能、模块分别放在若干个目录中。makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为 makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。Make工具最主要也是最基本的功能就是通过makefile文件来描述源程序之间的相互关系并自动维护编译工作。

    对于在Linux上进行C/C++开发时,如果一个项目只有一两个文件时,其实写Makeflie是没有什么必要的,但当一个庞大的项目,是多层次目录结构,每个目录中都需要自己的Makefile,而且使用了大量的动态链接库时,那么这个Makefile的书写就相当复杂了。这样我们就必须引入新的技术来完成更上一层楼的自动化——autoconf和automake。

    

    那我们先看看其实际操作流程:

    以hello为例: 

    执行:

    autoscan

    aclocal

    autoconf

    automake --add-missing

    ./configure

    make

     ./helloworld

 



    命令流程比较多,我们先一个一个了解其作用:

    autoscan

    autoscan程序可以用来为软件包创建configure.in文件。autoscan在以命令行参数中指定的目录为根(如果未给定参数,则以当前目录为根)的目录树中检查源文件。它为通常的轻便问题搜索源文件,并且为那个包创建一个configure.scan文件,这个文件就是configure.in的前身。

    

    configure.scan

    AC_PREREQ(2.59)

    autoconf的版本必须达到2.59

    AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)

    第一个参数:发布的文件名,即生成的tar.gz文件名字

    第二个参数:表示版本号

    第三个参数:当程序有bug,需要通知你的时候的email地址

    AC_CONFIG_SRCDIR([helloworld.cpp])

    源代码文件位置

    AC_CONFIG_HEADER([config.h])

    宏定义文件 

 

    # Checks for programs.

    AC_PROG_CXX

    确定C++编译器。检查环境变量CXX或者CCC(按照这个顺序)是否被设置了;如果设置了,就把输出变量 CXX设置成它的值。否则就搜索类似名称(c++、g++、gcc、CC、 cxx和cc++)的C++编译器。如果上述测试都失败了,最后的办法就是把CXX设置成 gcc。 

    # Checks for libraries.

    AC_CHECK_LIB([pthread], [pthread_create])//本例无此值

    这个就是检查库文件

    第一个参数:表示检查的库文件名字

    第二个参数:表示检查库中是否有这个函数

    其实后面还可以有参数,分别表示,如果检查到有,该如何如何,和检查没有,该如何如何.一般检查到了会在编译的时候加上。

    # Checks for header files.

    AC_CHECK_HEADER (header-file, [action-if-found [, action-if-not-found]]) //本例无此值

    如果系统头文件header-file存在,就执行shell命令action-if-found,否则执行action-if-not-found。如果你只需要在可以使用头文件的时候定义一个符号。 

    AC_CHECK_HEADERS (header-file... [, action-if-found [, action-if-not-found]])  //本例无此值

    对于每个在以空格分隔的参数列表header-file出现的头文件,如果存在,就定义 HAVE_header-file(全部大写)。如果给出了action-if-found,它就是在找到一个头文件的时候执行的附加shell代码。你可以把`break'作为它的值以便在第一次匹配的时候跳出循环。如果给出了action-if-not-found,它就在找不到某个头文件的时候被执行。 

    # Checks for typedefs, structures, and compiler characteristics.

    # Checks for library functions.

    AC_OUTPUT

    当执行./configure生成Makefile过程中时,如果出现各种问题,automake生成config.status文件,以便问题解决

 

    configure.in

    AC_INIT(helloworld.c)

    AM_INIT_AUTOMAKE(helloworld,1.0)

    # 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_OUTPUT(Makefile)

 

    

    aclocal

    aclocal程序会基于configure.in的内容,自动生成aclocal.m4文件。这提供了一个方便的方式来获得Automake系统提供的宏。其aclocal是一个perl 脚本程序。

 

    autoconf
    
Autoconf 是一个在Bourne shell下制作供编译、安装和打包软件的配置脚本的工具。Autoconf并不受程式语言限制,常用于C、C++ 、Erlang和Objective-C。配置脚本控制了一个软件包在特定系统上的安装。在进行一系列测试后,配置脚本从模板中生成makefile与头文件进而调整软件包,使之适应某一种系统。

    根据configure.in和aclocal.m4来产生configure文件。configure是一个脚本,它能设置源程序来适应各种不同的操作系统平台,并且根据不同的系统来产生合适的Makefile,从而可以使你的源代码能在不同的操作系统平台上被编译出来。configure.in文件的内容是一些宏,这些宏经过autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。

     

    aclocal.m4

    M4 是一个宏处理器.将输入拷贝到输出,同时将宏展开. 宏可以是内嵌的也可以是用户定义的. 除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行Unix命令,整数运算,文本操作,循环等. m4既可以作为编译器的前端也可以单独作为一个宏处理器.

    m4 将输入拷贝到输出,同时将宏展开. 宏可以是内嵌的也可以是用户定义的. 除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行Unix命令,整数运算,文本操作,循环等. m4既可以作为编译器的前端也可以单独作为一个宏处理器。

 

    

    Makefile.am

    可以这样简单看看他们之间的关系

    automake   读取   Makefile.am   来产生   Makefile.in, 

    configure   读取   Makefile.in   来产生   Makefile 

    

    AUTOMAKE_OPTIONS

    这个是automake的选项。在执行automake时,它会检查目录下是否存在标准GNU软件包中应具备的各种文件,例如AUTHORS、ChangeLog、NEWS等文件。我们将其设置成foreign时,automake会改用一般软件包的标准来检查。

    bin_PROGRAMS

    这个是指定我们所要产生的可执行文件的文件名。如果你要产生多个可执行文件,那么在各个名字间用空格隔开。

    helloworld_SOURCES

    这个是指定产生“helloworld”时所需要的源代码。如果它用到了多个源文件,那么请使用空格符号将它们隔开。比如需要helloworld.h,helloworld.c那么请写成helloworld_SOURCES= helloworld.h helloworld.c。

 

    Automake

    Automake是一个从文件Makefile.am自动生成Makefile.in的工具。每个Makefile.am基本上是一系列make的宏定义。选项--add-missing的定义是“add missing standard files to package”。它会让automake加入一个标准的软件包所必须的一些文件。产生出来的Makefile.in文件是符合GNU Makefile惯例的。

 

    


 

<转载>案例:

 

1、建目录

 

  在你的工作目录下建一个helloworld目录,我们用它来存放helloworld程序及相关文件,如在/home/my/build下:

 

      $ mkdir helloword

 

      $ cd helloworld

 

 

 

 

2、 helloworld.c

 

  然后用你自己最喜欢的编辑器写一个hellowrold.c文件,如命令:vi helloworld.c。使用下面的代码作为helloworld.c的内容。

 

  int main(int argc, char** argv)

 

  {

 

          printf("Hello, Linux World! ");

 

          return 0;

 

  } 

 

  完成后保存退出。现在在helloworld目录下就应该有一个你自己写的helloworld.c了。

 

 

 

3、生成configure

 

  我们使用autoscan命令来帮助我们根据目录下的源代码生成一个configure.in的模板文件。

 

  命令:

 

  $ autoscan

 

  $ ls

 

  configure.scan helloworld.c 

 

  执行后在hellowrold目录下会生成一个文件:configure.scan,我们可以拿它作为configure.in的蓝本。

 

4,生成configure.in

 

   现在将configure.scan改名为configure.in,并且编辑它,按下面的内容修改,去掉无关的语句:

 

 

 

 

Code

=======================configure.in内容开始===========================

 

# -*- Autoconf -*-

 

# Process this file with autoconf to produce a configure script.

 

 

AC_INIT(helloworld.c)

 

AM_INIT_AUTOMAKE(helloworld, 1.0)

 

 

# 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_OUTPUT(Makefile)

 

============================configure.in内容结束========================================= 

 

 

 

5 执行aclocal和autoconf

 

  然后执行命令aclocal和autoconf,分别会产生aclocal.m4及configure两个文件:

 

 

 

Code

$ aclocal

 

  $ls

 

  aclocal.m4 configure.in helloworld.c

 

  $ autoconf

 

  $ ls

 

  aclocal.m4 autom4te.cache configure configure.in helloworld.c 

 

 

 

 

  大家可以看到configure.in内容是一些宏定义,这些宏经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。

 

  autoconf 是用来生成自动配置软件源代码脚本(configure)的工具。configure脚本能独立于autoconf运行,且在运行的过程中,不需要用户的干预。

 

  要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4。

 

  aclocal根据configure.in文件的内容,自动生成aclocal.m4文件。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。

 

  autoconf从configure.in这个列举编译软件时所需要各种参数的模板文件中创建configure。

 

  autoconf需要GNU m4宏处理器来处理aclocal.m4,生成configure脚本。

 

  m4是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,循环等。m4既可以作为编译器的前端,也可以单独作为一个宏处理器.

 

 

 

6、新建Makefile.am

 

  新建Makefile.am文件,命令:$ vi Makefile.am 

 

  内容如下:

 

 

 

Code

AUTOMAKE_OPTIONS=foreign

 

bin_PROGRAMS=helloworld

 

helloworld_SOURCES=helloworld.c 

 

 

  automake会根据你写的Makefile.am来自动生成Makefile.in。Makefile.am中定义的宏和目标,会指导automake生成指定的代码。例如,宏bin_PROGRAMS将导致编译和连接的目标被生成。

 

7、运行automake

 

  命令:

 

 

 

 

Code

$ automake --add-missing

 

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

 

configure.in: installing `./mkinstalldirs'

 

configure.in: installing `./missing'

 

Makefile.am: installing `./depcomp' 

 

 

 

  automake会根据Makefile.am文件产生一些文件,包含最重要的Makefile.in。

 

 

 

8、执行configure生成Makefile

 

 

 

 

Code

$ ./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 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

 

$ ls -l Makefile

 

-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile 

 

 

 

你可以看到,此时Makefile已经产生出来了。

 

 

 

9、使用Makefile编译代码

 

 

 

 

Code

$ make

 

if gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -

 

 

DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="helloworld" -DVERSION="1.0"

 

 

-I. -I. -g -O2 -MT helloworld.o -MD -MP -MF ".deps/helloworld.Tpo"

 

-c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c;

 

then mv -f ".deps/helloworld.Tpo" ".deps/helloworld.Po";

 

else rm -f ".deps/helloworld.Tpo"; exit 1;

 

fi

 

gcc -g -O2 -o helloworld helloworld.o 

 

 

 

10, 运行helloworld

 

 

 

Code

$ ./helloworld

 

Hello, Linux World! 

 

 

  这样helloworld就编译出来了,你如果按上面的步骤来做的话,应该也会很容易地编译出正确的helloworld文件。你还可以试着使用一些其他的make命令,如make clean,make install,make dist,看看它们会给你什么样的效果。感觉如何?自己也能写出这么专业的Makefile,老板一定会对你刮目相看。

 

<结束>


 

 

<转载>

http://hi.baidu.com/yzkuang/blog/item/557e4f24423d8136c9955908.html

 

图解autoscan、aclocal、autoheader、automake、autoconf、configure、make

 

1.autoscan (autoconf): 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是configure.ac的一个雏形。

your source files --> [autoscan*] --> [configure.scan] --> configure.ac


2.aclocal (automake):根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到文件 aclocal.m4中。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”


user input files   optional input     process          output files================   ==============     =======          ============                    acinclude.m4 - - - - -.                                          V                                      .-------,configure.ac ------------------------>|aclocal|                 {user macro files} ->|       |------> aclocal.m4                                      `-------'3.autoheader(autoconf): 根据configure.ac中的某些宏,比如cpp宏定义,运行m4,声称config.h.inuser input files    optional input     process          output files================    ==============     =======          ============                    aclocal.m4 - - - - - - - .                                             |                                             V                                     .----------,configure.ac ----------------------->|autoheader|----> autoconfig.h.in                                     `----------'

4.automake: automake将Makefile.am中定义的结构建立Makefile.in,然后configure脚本将生成的Makefile.in文件转换为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,否则它会自己产生config.guess和config.sub

user input files   optional input   processes          output files================   ==============   =========          ============                                     .--------,                                     |        | - - -> COPYING                                     |        | - - -> INSTALL                                     |        |------> install-sh                                     |        |------> missing                                     |automake|------> mkinstalldirsconfigure.ac ----------------------->|        |Makefile.am  ----------------------->|        |------> Makefile.in                                     |        |------> stamp-h.in                                 .---+        | - - -> config.guess                                 |   |        | - - -> config.sub                                 |   `------+-'                                 |          | - - - -> config.guess                                 |libtoolize| - - - -> config.sub                                 |          |--------> ltmain.sh                                 |          |--------> ltconfig                                 `----------'

5.autoconf:将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。

user input files   optional input   processes          output files================   ==============   =========          ============aclocal.m4 ,autoconfig.h.in - - - - - - -.                                         V                                     .--------,configure.ac ----------------------->|autoconf|------> configure
 
6. ./configure的过程
                                           .-------------> [config.cache]     configure* --------------------------+-------------> config.log                                          |              [config.h.in] -.            v            .--> [autoconfig.h]                             +-------> config.status* -+                                 Makefile.in ---'                         `-->   Makefile
 
7. make过程
 
     [autoconfig.h] -.                     +--> make* --->  程序       Makefile   ---'
 
                                     .---------,                   config.site - - ->|         |                  config.cache - - ->|         | - - -> config.cache                                     |         +-,                                     `-+-------' |                                       |         |----> config.status                   config.h.in ------->|config-  |----> config.h                   Makefile.in ------->|  .status|----> Makefile                                       |         |----> stamp-h                                       |         +--,                                     .-+         |  |                                     | `------+--'  |                   ltmain.sh ------->|ltconfig|-------> libtool                                     |        |     |                                     `-+------'     |                                       |config.guess|                                       | config.sub |                                       `------------'

                                   .--------,                   Makefile ------>|        |                   config.h ------>|  |{project sources} ---------------->|        |--------> {project targets}                                 .-+        +--,                                 | `--------'  |                                 |   libtool   |                                 |   missing   |                                 |  install-sh |                                 |mkinstalldirs|                                 `-------------'

 



过程图解 

 


 

 

    


 

 

 


 

参考文章:


http://zhan.renren.com/justlearn?checked=true

http://baike.baidu.com/view/974566.htm

http://www.cnblogs.com/itech/archive/2010/11/28/1890220.html

http://nixdoc.net/man-pages/Linux/aclocal.1.html#NAME

http://zh.wikipedia.org/wiki/Autoconf