Autoconf和Automake工具組簡(jiǎn)介
來(lái)源:ChinaUnix博客 日期:2009.04.20 16:24 (共有
0條評論)
我要評論 本文摘自L(fǎng)inux高級程序設計(楊宗德 鄧玉春 曾慶華)
Autoconf/Automake工具用于自動(dòng)創(chuàng )建功能完善的makefile文件。當前大多數軟件包都是用這一工具生成makefile文件的。本節首先介紹Autoconf/Automake工具的功能以及makefile創(chuàng )建過(guò)程中所涉及的文件和命令。最后以一個(gè)實(shí)例介紹如何使用Autoconf/Automake工具自動(dòng)創(chuàng )建makefile文件。
Autoconf/Automake工具組簡(jiǎn)介Autoconf/Automake工具組主要包括autoconf、automake、perl語(yǔ)言環(huán)境和m4。其中FC4默認安裝的autoconf和automake軟件包信息如下:
[root@localhost hello]# rpm -qa |grep autoconf //查看是否安裝autoconf
autoconf-2.59-5
[root@localhost hello]# rpm -qa |grep automake //查看是否安裝automake
automake14-1.4p6-12
automake-1.9.5-1
automake17-1.7.9-6
automake15-1.5-13
automake16-1.6.3-5
默認安裝的perl語(yǔ)言環(huán)境如下:
[root@localhost ~]# rpm -qa |grep perl //
查看perl的安裝情況,已經(jīng)安裝后才有以下信息
perl-Filter-1.30-7
perl-URI-1.35-2
perl-HTML-Tagset-3.04-1
perl-libwww-perl-5.803-2
perl-XML-Encoding-1.01-27
perl-XML-NamespaceSupport-1.08-7
perl-Crypt-SSLeay-0.51-6
perl-XML-Grove-0.46alpha-27
perl-5.8.6-15
perl-DateManip-5.42a-4
perl-HTML-Parser-3.45-1
perl-Compress-Zlib-1.34-2
perl-XML-Parser-2.34-6
perl-XML-Dumper-0.71-4
perl-libxml-enno-1.02-31
perl-Convert-ASN1-0.19-1
perl-XML-SAX-0.12-7
perl-LDAP-0.33-1
perl-XML-LibXML-1.58-2
perl-XML-Twig-3.17-1
perl-Parse-Yapp-1.05-33
perl-libxml-perl-0.08-1
perl-XML-LibXML-Common-0.13-8
默認安裝的m4軟件包如下:
[root@localhost ~]# rpm -qa |grep m4 //查看是否安裝m4工具
m4-1.4.3-1
如果讀者沒(méi)有獲得以上任何一個(gè)軟件包的完全安裝,請直接插入FC4安裝盤(pán),使用"system-config-packages"命令更新,在開(kāi)發(fā)工具中選中以上選項即可。
以下命令用來(lái)查看本節所使用的Autoconf/Automake命令所在位置:
[root@localhost hello]# whereis aclocal //查看aclocal命令所在位置
aclocal: /usr/bin/aclocal /usr/share/aclocal
[root@localhost hello]# whereis autoscan //查看autoscan命令所在位置
autoscan: /usr/bin/autoscan /usr/share/man/man1/autoscan.1.gz
[root@localhost hello]# whereis autoconf //查看autoconf命令所在位置
autoconf: /usr/bin/autoconf /usr/share/autoconf /usr/share/man/man1/autoconf.1.gz
[root@localhost hello]# whereis autoheader //查看autoheader命令所在位置
autoheader: /usr/bin/autoheader /usr/share/man/man1/autoheader.1.gz
[root@localhost hello]# whereis automake //查看automake命令所在位置
automake: /usr/bin/automake /usr/local/automake
使用Autoconf/Automake工具自動(dòng)生成Makefile文件的流程圖如圖2-5所示。在此過(guò)程中使用的命令主要有aclocal、autoscan、autoconf、autoheader和automake。由圖可知運行步驟如下:
(1)創(chuàng )建源代碼文件,使用"autoscan"生成configure.scan文件,將其重命名為configure.ac,并做適當修改,然后使用"aclocal"命令生成aclocal.m4文件,使用"autoconf"命令由configure.ac和aclocal.m4文件生成configure文件。
(2)手工編輯Makefile.am文件,使用"automake"命令生成configure.in文件。
(3)手工編輯或由系統給定acconfig.h文件,使用"autoheader"命令生成config.h.in文件。
(4)使用"configure"命令由configure、configure.in和config.h.in文件生成Makefile文件。從而完成Makefile文件的創(chuàng )建過(guò)程。
下面以自動(dòng)編譯hello.c程序為例介紹如何使用這組工具生成makefile文件。
1.使用Vi編輯器編輯源程序在Linux操作Shell提示符使用Vi編輯器下創(chuàng )建hello.c源程序。
[root@localhost ch0206]# mkdir hello //創(chuàng )建文件夾
[root@localhost ch0206]# cd hello //切換文件
[root@localhost hello]# ls //已經(jīng)創(chuàng )建的hello.c文件
hello.c
[root@localhost hello]# cat hello.c //C源程序代碼
int main(int argc,char** argv)
{
printf("hello!GNU\n");
return 0;
}
2.使用Autoscan工具生成configure.ac文件Autoscan工具用來(lái)掃描源代碼以搜尋一般的可移植性問(wèn)題,比如檢查編譯器、庫和頭文件等,并創(chuàng )建configure.scan文件,它會(huì )在給定目錄及其子目錄樹(shù)中檢查源文件,若沒(méi)有給出目錄,就在當前目錄及其子目錄樹(shù)中進(jìn)行檢查。如下所示:
[root@localhost hello]# autoscan .///在當前文件夾中搜索
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[root@localhost hello]# ls //生成configure.scan文件,它是configure.ac文件原型
autoscan.log configure.scan hello.c
[root@localhost hello]# cat configure.scan //configure.scan文件內容
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59) //autoconf版本號
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS) //軟件的名稱(chēng)和版本等信息
AC_CONFIG_SRCDIR([hello.c]) //偵測源碼文件
AC_CONFIG_HEADER([config.h]) //用于生成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 //輸入文件名
下面給出本文件的簡(jiǎn)要說(shuō)明(所有以"#"號開(kāi)始的行為注釋?zhuān)?br>(1)AC_PREREQ宏聲明本文件要求的autoconf版本,本例使用的版本為2.59。
(2)AC_INIT宏用來(lái)定義軟件的名稱(chēng)和版本等信息,"FULL-PACKAGE-NAME"為軟件包名稱(chēng),"VERSION"為軟件版本號,"BUG-REPORT-ADDRESS"為BUG報告地址(一般為軟件作者郵件地址)。
(3)AC_CONFIG_SRCDIR宏用來(lái)偵測所指定的源碼文件是否存在,來(lái)確定源碼目錄的有效性。此處為當前目錄下的hello.c。
(4)AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。
(5)AC_PROG_CC用來(lái)指定編譯器,如果不指定,選用默認gcc。
(6)AC_OUTPUT用來(lái)設定 configure所要產(chǎn)生的文件,如果是makefile,configure會(huì )把它檢查出來(lái)的結果帶入makefile.in文件產(chǎn)生合適的makefile。使用Automake時(shí),還需要一些其他的參數,這些額外的宏用aclocal工具產(chǎn)生。
中間的注釋可以分別添加用戶(hù)測試程序、測試函數庫和測試頭文件等宏定義。
此文件只是下面要使用的configure.ac文件的原型,要使用此文件,還需要根據情況修改相關(guān)內容。
[root@localhost hello]# cp configure.scan configure.ac //復制文件
[root@localhost hello]# ls
autoscan.log configure.ac configure.scan hello.c
[root@localhost hello]# cat configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
AC_INIT(hello,1.0,yangzongde@163.com) //在此行內容中設置當前軟件包信息
AM_INIT_AUTOMAKE(hello,1.0) //automake所必備的宏,必須添加
AC_CONFIG_SRCDIR([hello.c]) //源文件名
AC_CONFIG_HEADER([config.h]) //config文件
# 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) //輸出文件名為makefile
此文件的相關(guān)內容需要根據當前軟件和系統環(huán)境進(jìn)行配置,但是,AM_INIT_ AUTOMAKE宏需要自己添加,它是automake所必備的宏,同前面一樣,PACKAGE是要產(chǎn)生的軟件套件的名稱(chēng),VERSION是版本編號。其他設置請參閱注釋內容。
3.使用aclocal工具生成aclocal.m4aclocal工具用于掃描configure.ac文件生成aclocal.m4。此工具根據已經(jīng)安裝的宏、用戶(hù)定義宏和acinclude.m4文件中的宏將configure.ac文件需要的宏集中定義到文件aclocal.m4中。
[root@localhost hello]# aclocal //執行aclocal生成aclocal.m4文件
[root@localhost hello]# ls
aclocal.m4 autom4te.cache autoscan.log configure.ac configure.scan hello.c
4.使用autoconf工具生成configure文件將configure.ac中的宏展開(kāi),生成configure腳本。這個(gè)過(guò)程可能要用到aclocal.m4中定義的宏。
[root@localhost hello]# autoconf //執行autoconf生成configure文件
[root@localhost hello]# ls
aclocal.m4 autoscan.log configure.ac hello.c
autom4te.cache configure configure.scan
5.使用autoheader工具生成config.h.in文件autoheader工具負責生成config.h.in文件。該工具會(huì )從"acconfig.h"文件中復制用戶(hù)附加的符號定義。此步驟可以在第3或第4步之前完成。
[root@localhost hello]# find / -name acconfig.h //系統acconfig.h文件位置
/usr/src/kernels/2.6.11-1.1369_FC4-i686/include/acpi/acconfig.h
[root@localhost hello]# autoheader
[root@localhost hello]# ls //查看生成的config.h.in文件
aclocal.m4 autoscan.log configure configure.scan
autom4te.cache config.h.in configure.ac hello.c
6.創(chuàng )建Makefile.am文件Automake工具會(huì )根據configure.in中的參量把Makefile.am轉換成Makefile.in文件。在使用Automake工具前,讀者需要手工創(chuàng )建腳本配置文件Makefile.am。本例中,作者創(chuàng )建的文件如下所示:
[root@localhost hello]# ls Makefile.am
Makefile.am
[root@localhost hello]# cat Makefile.am // Makefile.am范例
AUTOMAKE_OPTIONS = foreign //軟件等級
bin_PROGRAMS = hello //可執行文件名
hello_SOURCES = hello.c //源文件名
其中:(1)AUTOMAKE_OPTIONS為設置Automake的選項。由于GNU對自己發(fā)布的軟件有嚴格的規范,比如必須附帶許可證聲明文件COPYING等,否則Automake執行時(shí)會(huì )報錯。Automake提供了3種軟件等級:foreign、gnu和gnits,供用戶(hù)選擇,默認等級為gnu。本例使需用foreign等級,它只檢測必須的文件。
(2)bin_PROGRAMS定義要產(chǎn)生的執行文件名。如果要產(chǎn)生多個(gè)執行文件,每個(gè)文件名用空格隔開(kāi)。
(3)hello_SOURCES定義"hello"這個(gè)執行程序所需要的原始文件。如果"hello"這個(gè)程序是由多個(gè)原始文件所產(chǎn)生的,則必須把它所用到的所有原始文件都列出來(lái),并用空格隔開(kāi)。例如:若目標體"hello"需要"hello.c"、"hello.h"兩個(gè)依賴(lài)文件,則定義hello_SOURCES=hello.c hello.h。
7.使用Automake生成Makefile.in文件下面使用Automake生成"Makefile.in"文件,使用選項"--add-missing"可以讓Automake自動(dòng)添加一些必需的腳本文件。如下所示:
[root@localhost hello]# automake --add-missing
configure.ac: installing './install-sh' //創(chuàng )建install-sh文件
configure.ac: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: required file './NEWS' not found
Makefile.am: required file './README' not found
Makefile.am: required file './AUTHORS' not found
Makefile.am: required file './ChangeLog' not found
Makefile.am: installing './COPYING'
Makefile.am: installing './depcomp'
[root@localhost hello]# automake --add-missing //再運行一次,可以輔助生成幾個(gè)必要的文件
Makefile.am: required file './NEWS' not found //沒(méi)有找到NEWS文件
Makefile.am: required file './README' not found
Makefile.am: required file './AUTHORS' not found
Makefile.am: required file './ChangeLog' not found
[root@localhost hello]# touch NEWS //創(chuàng )建NEWS文件,如果沒(méi)有自動(dòng)生成,手工創(chuàng )建
[root@localhost hello]# touch README //創(chuàng )建README文件
[root@localhost hello]# touch AUTHORS //創(chuàng )建AUTHORS文件
[root@localhost hello]# touch ChangeLog //創(chuàng )建ChangeLog文件
[root@localhost hello]# automake --add-missing //再運行一次
[root@localhost hello]# ls //生成必要的文件
aclocal.m4 ChangeLog configure.scan INSTALL missing
AUTHORS config.h.in COPYING install-sh NEWS
autom4te.cache configure depcomp Makefile.am README
autoscan.log configure.ac hello.c Makefile.in
[root@localhost hello]# ls configure.in -l
-rw-r--r-- 1 root root 536 Dec 27 04:29 configure.in
8.配置運行自動(dòng)配置設置文件configure,把Makefile.in變成最終的Makefile。
[root@localhost hello]# ./configure //配置,生成Makefile文件
……
config.status: creating Makefile
config.status: executing depfiles commands
[root@localhost hello]# ls
aclocal.m4 ChangeLog config.status COPYING install-sh missing
AUTHORS config.h configure depcomp Makefile NEWS
autom4te.cache config.h.in configure.ac hello.c Makefile.am README
autoscan.log config.log configure.scan INSTALL Makefile.in stamp-h1
[root@localhost hello]# ls -l Makefile*
-rw-r--r-- 1 root root 16876 Dec 27 04:51 Makefile
-rw-r--r-- 1 root root 68 Dec 27 04:46 Makefile.am
-rw-r--r-- 1 root root 17180 Dec 27 04:50 Makefile.in
9.測試運行make命令進(jìn)行編譯,下面的示例中Make的主要編譯命令為"gcc -g -O2 -o hello hello.o",此句對應本節前面介紹的手工編輯的Makefile文件內容。
[root@localhost hello]# cd ../hello
[root@localhost hello]# make //執行make命令
make all-am
……
gcc -g -O2 -o hello hello.o //編譯指令
……
編譯成功后,將在當前目錄下生成并運行可執行程序hello。測試源代碼是否正確:
[root@localhost hello_2]# ./hello
hello!GNU
此方法生成的makefile文件很全面。使用"make install"命令把目標文件安裝在系統中。
[root@localhost hello]# make install //安裝
make[1]: Entering directory '/root/book/ch02/ch0206/hello'
test -z "/usr/local/bin" || mkdir -p -- "/usr/local/bin"
/usr/bin/install -c 'hello' '/usr/local/bin/hello' //安裝目標文件
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory '/root/book/ch02/ch0206/hello'
使用"make uninstall"命令把目標文件從系統中卸載。
[root@localhost hello]# make uninstall //卸載命令
rm -f '/usr/local/bin/hello' //從系統中卸載
使用"make clean"命令清除編譯時(shí)的obj文件。
[root@localhost hello]# make clean
test -z "hello" || rm -f hello
rm -f *.o //刪除obj文件
使用"make dist"命令將程序和相關(guān)的文檔打包為一個(gè)壓縮文檔以供發(fā)布。
[root@localhost hello]# make dist
……
[root@localhost hello]# ls //查看生成的文件
aclocal.m4 config.h.in configure.scan install-sh README
AUTHORS config.h.in~ COPYING Makefile stamp-h1
autom4te.cache config.log depcomp Makefile.am
Changelog config.status hello-1.0.tar.gz Makefile.in
ChangeLog configure hello.c missing
config.h configure.ac INSTALL NEWS
[root@localhost hello]# ls hello-1.0.tar.gz -l //打包文件
-rw-r--r-- 1 root root 67699 Dec 27 06:33 hello-1.0.tar.gz