3.7.1 autotools使用流程
正如前面所言,autotools是系列工具,讀者首先要確認系統(tǒng)是否裝了以下工具(可以用which命令進行查看)。
· aclocal
· autoscan
· autoconf
· autoheader
· automake
使用autotools主要就是利用各個工具的腳本文件以生成最后的Makefile。其總體流程是這樣的:
· 使用aclocal生成一個“aclocal.m4”文件,該文件主要處理本地的宏定義;
· 改寫“configure.scan”文件,并將其重命名為“configure.in”,并使用autoconf文件生成configure文件。
接下來,筆者將通過一個簡單的hello.c例子帶領(lǐng)讀者熟悉autotools生成makefile的過程,由于在這過程中有涉及到較多的腳本文件,為了更清楚地了解相互之間的關(guān)系,強烈建議讀者實際動手操作以體會其整個過程。
1.autoscan
它會在給定目錄及其子目錄樹中檢查源文件,若沒有給出目錄,就在當前目錄及其子目錄樹中進行檢查。它會搜索源文件以尋找一般的移植性問題并創(chuàng)建一個文件“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的配置文件)文件,此時還沒有創(chuàng)建該配置文件,于是它會自動生成一個“configure.in”的原型文件“configure.scan”。
2.autoconf
configure.in是autoconf的腳本配置文件,它的原型文件“configure.scan”如下所示:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
#The next one is modified by sunq
#AC_INIT(FULL-PACKAGE-NAME,VERSION,BUG-REPORT-ADDRESS)
AC_INIT(hello,1.0)
# The next one is added by sunq
AM_INIT_AUTOMAKE(hello,1.0)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADER([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
下面對這個腳本文件進行解釋:
· 以“#”號開始的行為注釋。
· AC_PREREQ宏聲明本文件要求的autoconf版本,如本例使用的版本2.59。
· AC_INIT宏用來定義軟件的名稱和版本等信息,在本例中省略了BUG-REPORT-ADDRESS,一般為作者的e-mail。
· AM_INIT_AUTOMAKE是筆者另加的,它是automake所必備的宏,也同前面一樣,PACKAGE是所要產(chǎn)生軟件套件的名稱,VERSION是版本編號。
· AC_CONFIG_SRCDIR宏用來偵測所指定的源碼文件是否存在,來確定源碼目錄的有
效性。在此處為當前目錄下的hello.c。
· AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。
· AC_CONFIG_FILES宏用于生成相應(yīng)的Makefile文件。
· 中間的注釋間可以添加分別用戶測試程序、測試函數(shù)庫、測試頭文件等宏定義。
接下來首先運行aclocal,生成一個“aclocal.m4”文件,該文件主要處理本地的宏定義。如下所示:
[root@localhost automake]# aclocal
再接著運行autoconf,生成“configure”可執(zhí)行文件。如下所示:
[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”文件中復(fù)制用戶附加的符號定義,因此此處沒有附加符號定義,所以不需要創(chuàng)建“acconfig.h”文件。如下所示:
[root@localhost automake]# autoheader
4.automake
這一步是創(chuàng)建Makefile很重要的一步,automake要用的腳本配置文件是Makefile.am,用戶需要自己創(chuàng)建相應(yīng)的文件。之后,automake工具轉(zhuǎn)換成Makefile.in。在該例中,筆者創(chuàng)建的文件為Makefile.am如下所示:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c
下面對該腳本文件的對應(yīng)項進行解釋。
· 其中的AUTOMAKE_OPTIONS為設(shè)置automake的選項。由于GNU(在第1章中已經(jīng)有所介紹)對自己發(fā)布的軟件有嚴格的規(guī)范,比如必須附帶許可證聲明文件COPYING等,否則automake執(zhí)行時會報錯。automake提供了三種軟件等級:foreign、gnu和gnits,讓用戶選擇采用,默認等級為gnu。在本例使用foreign等級,它只檢測必須的文件。
· bin_PROGRAMS定義要產(chǎn)生的執(zhí)行文件名。如果要產(chǎn)生多個執(zhí)行文件,每個文件名用空格隔開。
· hello_SOURCES定義“hello”這個執(zhí)行程序所需要的原始文件。如果”hello”這個程序是由多個原始文件所產(chǎn)生的,則必須把它所用到的所有原始文件都列出來,并用空格隔開。例如:若目標體“hello”需要“hello.c”、“sunq.c”、“hello.h”三個依賴文件,則定義hello_SOURCES=hello.c sunq.c hello.h。要注意的是,如果要定義多個執(zhí)行文件,則對每個執(zhí)行程序都要定義相應(yīng)的file_SOURCES。
接下來可以使用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
在這一步中,通過運行自動配置設(shè)置文件configure,把Makefile.in變成了最終的Makefile。如下所示:
[root@localhost automake]# ./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: executing depfiles commands
可以看到,在運行configure時收集了系統(tǒng)的信息,用戶可以在configure命令中對其進行方便地配置。在./configure的自定義參數(shù)有兩種,一種是開關(guān)式(--enable-XXX或--disable-XXX),另一種是開放式,即后面要填入一串字符(--with-XXX=yyyy)參數(shù)。讀者可以自行嘗試其使用方法。另外,讀者可以查看同一目錄下的”config.log”文件,以方便調(diào)試之用。
到此為止,makefile就可以自動生成了?;貞浾麄€步驟,用戶不再需要定制不同的規(guī)則,而只需要輸入簡單的文件及目錄名即可,這樣就大大方便了用戶的使用。下面的圖3.9總結(jié)了上述過程:

圖3.9 autotools生成Makefile流程圖