Makefile 生成工具之autotools

一、安装autotools工具

  1. sudo apt-get install automake
  2. sudo apt-get install autoconf

二、autotools是系列工具, 它主要由autoconf、automake、perl语言环境和m4等组成;所包含的命令有五个 : 

  •  aclocal
  • autoscan
  • autoconf
  • autoheader
  • automake

  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.autoconf

  configure.in是autoconf的脚本配置文件,它的原型文件“configure.scan”如下所示:

1 #                                               -*- Autoconf -*-
2 3 # Process this file with autoconf to produce a configure script. 4 5 AC_PREREQ(2.59) 6 7 #The next one is modified by sunq 8 9 #AC_INIT(FULL-PACKAGE-NAME,VERSION,BUG-REPORT-ADDRESS) 10 11 AC_INIT(hello,1.0) 12 13 # The next one is added by sunq 14 15 AM_INIT_AUTOMAKE(hello,1.0) 16 17 AC_CONFIG_SRCDIR([hello.c]) 18 19 AC_CONFIG_HEADER([config.h]) 20 21 # Checks for programs. 22 23 AC_PROG_CC 24 25 # Checks for libraries. 26 27 # Checks for header files. 28 29 # Checks for typedefs, structures, and compiler characteristics. 30 31 # Checks for library functions. 32 33 AC_CONFIG_FILES([Makefile]) 34 35 AC_OUTPUT

  下面对这个脚本文件进行解释:

  • 以“#”号开始的行为注释。
  • AC_PREREQ宏声明本文件要求的autoconf版本,如本例使用的版本2.59。
  • AC_INIT宏用来定义软件的名称和版本等信息,在本例中省略了BUG-REPORT-ADDRESS,一般为作者的e-mail。
  • AM_INIT_AUTOMAKE是笔者另加的,它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号。
  • AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。在此处为当前目录下的hello.c 。
  • AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。
  • AC_CONFIG_FILES宏用于生成相应的Makefile文件。
  • 中间的注释间可以添加分别用户测试程序、测试函数库、测试头文件等宏定义。

  接下来首先运行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 hello.h

 

  下面对该脚本文件的对应项进行解释。

  • 其中的AUTOMAKE_OPTIONS为设置automake的选项。由于GNU(在第1章中已经有所介绍)对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING等,否则automake执行时会报错。automake提供了三种软件等级:foreign、gnu和gnits,让用户选择采用,默认等级为gnu。在本例使用foreign等级,它只检测必须的文件。
  • bin_PROGRAMS定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。
  • hello_SOURCES定义“hello”这个执行程序所需要的原始文件。如果”hello”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用空格隔开。例如:若目标体“hello”需要“hello.c”、“sunq.c”、“hello.h”三个依赖文件,则定义hello_SOURCES=hello.c sunq.c hello.h。

 

  接下来可以使用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 

 

原文地址:https://www.cnblogs.com/naray/p/4160089.html