autoconf和automake的使用

使用autoconf和automake两个工具可以自动生成符合自由软件惯例的Makefile,这样就可以象常见的GNU程序一样,只要使用“./configure”,“make”,“make instal”就可以把程序安装到Linux系统中去了。

Makefile
Makefile是一个文件,定义整个工程的编译规则。Makefile是用于自动编译和链接的,纪录有文件的信息,在make时会决定在链接的时候需要重新编译哪些文件,通常是那些发生了修改的文件。它的宗旨就是:让编译器知道要编译一个文件需要依赖其他的哪些文件。当那些依赖文件有了改变,编译器会自动的发现最终的生成文件已经过时,而重新编译相应的模块。Makefile经常关联开发环境,当系统环境变量或路径发生了变化后,Makefile可能还要跟着修改。这样就造成了手工书写Makefile的诸多问题,automake恰好能很好地帮助我们解决这些问题。

automake
使用automake,程序开发人员只需要写一些简单的含有预定义宏的文件,由autoconf根据一个宏文件生成configure,由automake根据另一个宏文件生成Makefile.in,再使用configure依据Makefile.in来生成一个符合惯例的Makefile。

简单的例子:

  1. 建目录

    192:~ lurongming$ mkdir helloword
    192:~ lurongming$ cd helloworld
    
  2. 编写文件
    helloworld.c

    /// @file helloworld.c
    int main(int argc, char** argv)
    {
    	printf("Hello World! ");
    	return 0;
    }
    
  3. 使用autoscan生成configure.scan模版

    192:helloworld lurongming$ autoscan
    192:helloworld lurongming$ ls
    autoscan.log	configure.scan	helloworld.c
    

    每个configure.scan文件都是以AC_INIT开头,以AC_OUTPUT结束。我们不难从文件中看出confiugre.in文件的一般布局:

    AC_INIT
    测试程序
    测试函数库
    测试头文件
    测试类型定义
    测试结构
    测试编译器特性
    测试库函数
    测试系统调用
    AC_OUTPUT

    192:helloworld lurongming$ cat configure.scan
    #                                               -*- Autoconf -*-
    # Process this file with autoconf to produce a configure script.
    
    AC_PREREQ([2.69])
    AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
    AC_CONFIG_SRCDIR([helloworld.c])
    AC_CONFIG_HEADERS([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_OUTPUT
    
  4. configure.scan重命名为configure.ac, 并修改文件内容

    192:helloworld lurongming$ mv configure.scan configure.ac
    192:helloworld lurongming$ vim configure.ac
    

    最后configure.ac文件如下:

    #                                               -*- Autoconf -*-
    # Process this file with autoconf to produce a configure script.
    
    AC_PREREQ([2.69])
    AC_INIT(helloworld.c, 1.0)
    AM_INIT_AUTOMAKE(helloworld, 1.0)
    
    AC_CONFIG_SRCDIR([helloworld.c])
    AC_CONFIG_HEADERS([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
    

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

  5. 执行aclocal,autoconf,autoheader
    autoconf 是用来生成自动配置软件源代码脚本(configure)的工具。configure脚本能独立于autoconf运行,且在运行的过程中,不需要用户的干预。
    要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4。
    aclocal根据configure.ac文件的内容,自动生成aclocal.m4文件。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。
    autoconf从configure.ac这个列举编译软件时所需要各种参数的模板文件中创建configure。
    autoconf需要GNU m4宏处理器来处理aclocal.m4,生成configure脚本。
    m4是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,循环等。m4既可以作为编译器的前端,也可以单独作为一个宏处理器。

    192:helloworld lurongming$ aclocal
    192:helloworld lurongming$ ls
    aclocal.m4	autom4te.cache	autoscan.log	configure.ac	helloworld.c
    192:helloworld lurongming$ autoconf
    192:helloworld lurongming$ autoheader
    
  6. 新建Makefile.am,内容如下:

    AUTOMAKE_OPTIONS=foreign
    bin_PROGRAMS=helloworld
    helloworld_SOURCES=helloworld.c 
    
  7. 运行automake

    192:helloworld lurongming$ touch ./NEWS
    192:helloworld lurongming$ touch ./README
    192:helloworld lurongming$ touch ./AUTHORS
    192:helloworld lurongming$ touch ./ChangeLog
    192:helloworld lurongming$ automake --add-missing
    
  8. 运行./configure

    192:helloworld lurongming$  ./configure
    
  9. 使用Makefile编译代码

    192:helloworld lurongming$  make
    
  10. 使用./helloworld

    192:helloworld lurongming$  ./helloworld
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值