《linux高級程序設計》第2章Linux下C語言開發工具,這一章主要介紹Linux下進行C語言程序開發所必備的工具。本節為Autoconf/Automake工具組簡介。
Autoconf/Automake工具用于自動創建功能完善的makefile文件。當前大多數軟件包都是用這一工具生成makefile文件的。本節首先介紹Autoconf/Automake工具的功能以及makefile創建過程中所涉及的文件和命令。最后以一個實例介紹如何使用Autoconf/Automake工具自動創建makefile文件。
2.6.1 autoconf安裝automake工具組簡介(1)
Autoconf/Automake工具組主要包括autoconf、automake、perl語言環境和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語言環境如下:
[root@localhost ~]# rpm -qa |grep perl // 查看perl的安裝情況,已經安裝后才有以下信息 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
|
autoconf安裝默認安裝的m4軟件包如下:
[root@localhost ~]# rpm -qa |grep m4 //查看是否安裝m4工具 m4-1.4.3-1
|
如果讀者沒有獲得以上任何一個軟件包的完全安裝,請直接插入FC4安裝盤,使用"system-config-packages"命令更新,在開發工具中選中以上選項即可。
以下命令用來查看本節所使用的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工具自動生成Makefile文件的流程圖如圖2-5所示。在此過程中使用的命令主要有aclocal、autoscan、autoconf、autoheader和automake。由圖可知運行步驟如下:
|
(點擊查看大圖)圖2-5 自動創建Makefile文件流程 |
(1)創建源代碼文件,使用"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"命令由configuer、configure.in和config.h.in文件生成Makefile文件。從而完成Makefile文件的創建過程。
下面以自動編譯hello.c程序為例介紹如何使用這組工具生成makefile文件。
1.使用Vi編輯器編輯源程序
在Linux操作Shell提示符使用Vi編輯器下創建hello.c源程序。
[root@localhost ch0206]# mkdir hello //創建文件夾 [root@localhost ch0206]# cd hello //切換文件 [root@localhost hello]# ls //已經創建的hello.c文件 hello.c [root@localhost hello]# cat hello.c //C源程序代碼 int main(int argc,char** argv) { printf("hello!GNU\n"); return 0; }
|
2.使用autoconf安裝工具生成configure.ac文件
Autoscan工具用來掃描源代碼以搜尋一般的可移植性問題,比如檢查編譯器、庫和頭文件等,并創建configure.scan文件,它會在給定目錄及其子目錄樹中檢查源文件,若沒有給出目錄,就在當前目錄及其子目錄樹中進行檢查。如下所示:
[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) //軟件的名稱和版本等信息
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 //輸入文件名
|
【編輯推薦】
- Linux 查看磁盤空間實現代碼介紹
- Linux操作系統需要微軟的十大幫助
- 探尋Linux到底需要多低的配置
- Linux測試工具tcpdump監視TCP/IP連接命令介紹
- Linux流量控制實例應用介紹