〉GNU Automake
〉GNU Autoconf
〉GNU m4
〉perl
〉GNU Libtool
因此您的操作系統必須安裝以上軟件,否則就不能夠自動生成Makefile文件或者在生成的過程中出現各種各樣的問題。用 autoconf/automake/autoheader工具來處理各種移植性的問題,用這些工具完成系統配置信息的收集,制作Makefile文件。然后在打算編譯源碼時只需要通過 "./configure; make"這樣簡單的命令就可以得到干凈利落的編譯。
1.autoscan (autoconf): 掃描源代碼以搜尋普通的可移植性問題,比如檢查編譯器,庫,頭文件等,生成文件configure.scan,它是configure.ac的一個雛形。
your source files --> [autoscan*] --> [configure.scan] --> configure.ac
2.aclocal (automake):根據已經安裝的宏,用戶定義宏和acinclude.m4文件中的宏將configure.ac文件所需要的宏集中定義到文件 aclocal.m4中。aclocal是一個perl 腳本程序,它的定義是:“aclocal - create aclocal.m4 by scanning configure.ac”user input files optional input process output files
================ ============== ======= ============
acinclude.m4 - - - - -.
V
.-------,
configure.ac ------------------------>|aclocal|
{user macro files} ->| |------> aclocal.m4
`-------'
3.autoheader(autoconf): 根據configure.ac中的某些宏,比如cpp宏定義,運行m4,聲稱config.h.in
user input files optional input process output files
================ ============== ======= ============
aclocal.m4 - - - - - - - .
|
V
.----------,
configure.ac ----------------------->|autoheader|----> autoconfig.h.in
`----------'
4.automake: automake將Makefile.am中定義的結構建立Makefile.in,然后configure腳本將生成的Makefile.in文件轉換 為Makefile。如果在configure.ac中定義了一些特殊的宏,比如AC_PROG_LIBTOOL,它會調用libtoolize,否則它 會自己產生config.guess和config.sub
user input files optional input processes output files
================ ============== ========= ============
.--------,
| | - - -> COPYING
| | - - -> INSTALL
| |------> install-sh
| |------> missing
|automake|------> mkinstalldirs
configure.ac ----------------------->| |
Makefile.am ----------------------->| |------> Makefile.in
| |------> stamp-h.in
.---+ | - - -> config.guess
| | | - - -> config.sub
| `------+-'
| | - - - -> config.guess
|libtoolize| - - - -> config.sub
| |--------> ltmain.sh
| |--------> ltconfig
`----------'
5.autoconf:將configure.ac中的宏展開,生成configure腳本。這個過程可能要用到aclocal.m4中定義的宏。
user input files optional input processes output files
================ ============== ========= ============
aclocal.m4 ,autoconfig.h.in - - - - - - -.
V
.--------,
configure.ac ----------------------->|autoconf|------> configure
6. ./configure的過程
.-------------> [config.cache]
configure* --------------------------+-------------> config.log
|
[config.h.in] -. v .--> [autoconfig.h]
+-------> config.status* -+
Makefile.in ---' `--> Makefile
7. make過程
[autoconfig.h] -.
+--> make* ---> 程序
Makefile ---'
.---------,
config.site - - ->| |
config.cache - - ->|configure| - - -> config.cache
| +-,
`-+-------' |
| |----> config.status
config.h.in ------->|config- |----> config.h
Makefile.in ------->| .status|----> Makefile
| |----> stamp-h
| +--,
.-+ | |
| `------+--' |
ltmain.sh ------->|ltconfig|-------> libtool
| | |
`-+------' |
|config.guess|
| config.sub |
`------------'
.--------,
Makefile ------>| |
config.h ------>| make |
{project sources} ---------------->| |--------> {project targets}
.-+ +--,
| `--------' |
| libtool |
| missing |
| install-sh |
|mkinstalldirs|
`-------------'
實例 :
在/hello/目錄下創建一個hello.c文件,並編譯運行它:
#cd /hello/
(1) 編寫源文件hello.c:
#include<stdio.h>
int main(int argc, char** argv)
{
printf("Hello, GNU!n");
return 0;
}
[litao@vm0000131 hello]$ ll
total 4
-rw-rw-r-- 1 litao litao 68 Aug 12 12:02 hello.c
一、autoscan
[litao@vm0000131 hello]$ autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[litao@vm0000131 hello]$ ll
total 8
-rw-rw-r-- 1 litao litao 0 Aug 12 12:03 autoscan.log
-rw-rw-r-- 1 litao litao 457 Aug 12 12:03 configure.scan
-rw-rw-r-- 1 litao litao 68 Aug 12 12:02 hello.c
已經生成了configure.scan,autoscan.log文件
將configure.scan 修改為 configure.in,最后修改的內容如下:
[litao@vm0000131 hello]$ mv configure.scan configure.in
[litao@vm0000131 hello]$ vim configure.in
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([hello.c])
#AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(hello, 1.0)
# 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)
二、acloacl
[litao@vm0000131 hello]$ aclocal
生成 aclocal.m4 和 autom4te.cache (生成aclocal.m4的過程中涉及到configure.in)
[litao@vm0000131 hello]$ ll
total 44
-rw-rw-r-- 1 litao litao 31120 Aug 12 12:08 aclocal.m4
drwxr-xr-x 2 litao litao 4096 Aug 12 12:08 autom4te.cache
-rw-rw-r-- 1 litao litao 0 Aug 12 12:03 autoscan.log
-rw-rw-r-- 1 litao litao 496 Aug 12 12:08 configure.in
-rw-rw-r-- 1 litao litao 68 Aug 12 12:02 hello.c
三、antoconf
[litao@vm0000131 hello]$ autoconf
生成 configure (根據 configure.in, 和 aclocal.m4)
[litao@vm0000131 hello]$ ll
total 168
-rw-rw-r-- 1 litao litao 31120 Aug 12 12:08 aclocal.m4
drwxr-xr-x 2 litao litao 4096 Aug 12 12:11 autom4te.cache
-rw-rw-r-- 1 litao litao 0 Aug 12 12:03 autoscan.log
-rwxrwxr-x 1 litao litao 122297 Aug 12 12:11 configure
-rw-rw-r-- 1 litao litao 496 Aug 12 12:08 configure.in
-rw-rw-r-- 1 litao litao 68 Aug 12 12:02 hello.c
四、編寫Makefile.am:
AUTOMAKE_OPTIONS= foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c
五、automake
生成 Makefile.in, depcomp, install-sh, 和 missing (根據 Makefile.am, 和 aclocal.m4)
[litao@vm0000131 hello]$ automake
configure.in: required file `./install-sh' not found
configure.in: required file `./missing' not found
Makefile.am: required file `./depcomp' not found
[litao@vm0000131 hello]$ automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'
[litao@vm0000131 hello]$ ll
total 192
-rw-rw-r-- 1 litao litao 31120 Aug 12 12:08 aclocal.m4
drwxr-xr-x 2 litao litao 4096 Aug 12 12:14 autom4te.cache
-rw-rw-r-- 1 litao litao 0 Aug 12 12:03 autoscan.log
-rwxrwxr-x 1 litao litao 122297 Aug 12 12:11 configure
-rw-rw-r-- 1 litao litao 496 Aug 12 12:08 configure.in
lrwxrwxrwx 1 litao litao 31 Aug 12 12:16 depcomp -> /usr/share/automake-1.9/depcomp
-rw-rw-r-- 1 litao litao 68 Aug 12 12:02 hello.c
lrwxrwxrwx 1 litao litao 34 Aug 12 12:16 install-sh -> /usr/share/automake-1.9/install-sh
-rw-rw-r-- 1 litao litao 69 Aug 12 12:15 Makefile.am
-rw-rw-r-- 1 litao litao 16561 Aug 12 12:16 Makefile.in
lrwxrwxrwx 1 litao litao 31 Aug 12 12:16 missing -> /usr/share/automake-1.9/missing
六、configure
生成 Makefile, config.log, 和 config.status
生成makefile整個過程
configure.in :
configure.in文件內容是一系列GNU m4 的宏,這些宏經autoconf處理后會變成檢查系統特性的shell scripts。 configure.in 內宏的順序並沒有特別的規定,但是每一個configure.in 文件必須在所有宏前加入 AC_INIT 宏,然后在所有宏的最后加上 AC_OUTPUT宏。可先用 autoscan 掃描原始文件以產生一個 configure.scan 文件,再對 configure.scan 做些修改成 configure.in 文件。在范例中所用到的宏如下:
dnl
這個宏后面的字不會被處理,可以視為注釋
AC_INIT(FILE)
該宏用來檢查源代碼所在路徑,autoscan 會自動產生,一般無須修改它。
AM_INIT_AUTOMAKE(PACKAGE,VERSION)
這個是使用 Automake 所必備的宏,PACKAGE 是所要產生軟件套件的名稱,VERSION 是版本編號。
AC_PROG_CC
檢查系統可用的C編譯器,若源代碼是用C寫的就需要這個宏。
AC_OUTPUT(FILE)
設置 configure 所要產生的文件,若是Makefile ,configure 便會把它檢查出來的結果帶入 Makefile.in 文件后產生合適的 Makefile。
實際上,這里使用 Automake 時,還需要一些其他的宏,這些額外的宏我們用 aclocal來幫助產生。執行 aclocal會產生aclocal.m4 文件,如果無特別的用途,可以不需要修改它,用 aclocal 所產生的宏會告訴 Automake如何動作。
有了 configure.in 及 aclocal.m4兩個文件以后,便可以執行 autoconf來產生 configure 文件了。
Makefile.am
Automake 會根據 configure.in 中的宏把Makefile.am 轉成 Makefile.in 文件。 Makefile.am 文件定義所要產生的目標:
AUTOMAKE_OPTIONS
設置 automake 的選項。
Automake 主要是幫助開發 GNU 軟件的人員來維護軟件,所以在執行 automake 時,會檢查目錄下是否存在標准 GNU 軟件中應具備的文件,例如 'NEWS'、'AUTHOR'、'ChangeLog' 等文件。設置 foreign 時,automake 會改用一般軟件的標准來檢查。
bin_PROGRAMS
定義要產生的執行文件名。如果要產生多個執行文件,每個文件名用空白符隔開。
hello_SOURCES
定義 'hello' 這個執行程序所需要的原始文件。如果 'hello'這個程序是由多個原始文件所產生,必須把它所用到的所有原始文件都列出來,以空白符隔開。假設 'hello' 還需要 'hello.c'、'main.c'、'hello.h' 三個文件的話,則定義
hello_SOURCES= hello.c main.c hello.h
如果定義多個執行文件,則對每個執行程序都要定義相對的filename_SOURCES。
編輯好 Makefile.am 文件,就可以用 automake --add-missing來產生 Makefile.in。加上 --add-missing 選項來告訴 automake順便假如包裝一個軟件所必須的文件。Automake產生生出來的 Makefile.in 文件是完全符合 GNU Makefile 的慣例,只要執行 configure這個shell script 便可以產生合適的 Makefile 文件了。
四、深入淺出
針對上面提到的各個命令,我們再做些詳細的介紹。
1 、 autoscan
autoscan 是用來掃描源代碼目錄生成configure.scan 文件的。autoscan 可以用目錄名做為參數,但如果你不使用參數的話,那么 autoscan 將認為使用的是當前目錄。autoscan 將掃描你所指定目錄中的源文件,並創建configure.scan 文件。
2 、 configure.scan
configure.scan 包含了系統配置的基本選項,里面都是一些宏定義。我們需要將它改名為configure.in
3 、 aclocal
aclocal 是一個perl 腳本程序。aclocal 根據configure.in 文件的內容,自動生成aclocal.m4 文件。aclocal 的定義是:“aclocal - create aclocal.m4 by scanning configure.ac” 。
4 、 autoconf
autoconf 是用來產生configure 文件的。configure 是一個腳本,它能設置源程序來適應各種不同的操作系統平台,並且根據不同的系統來產生合適的Makefile ,從而可以使你的源代碼能在不同的操作系統平台上被編譯出來。
configure.in 文件的內容是一些宏,這些宏經過autoconf 處理后會變成檢查系統特性、環境變量、軟件必須的參數的shell 腳本。configure.in 文件中的宏的順序並沒有規定,但是你必須在所有宏的最前面和最后面分別加上AC_INIT 宏和AC_OUTPUT 宏。
在configure.ini 中:
# 號表示注釋,這個宏后面的內容將被忽略。
AC_INIT(FILE)
這個宏用來檢查源代碼所在的路徑。
AM_INIT_AUTOMAKE(PACKAGE, VERSION)
這個宏是必須的,它描述了我們將要生成的軟件包的名字及其版本號:PACKAGE 是軟件包的名字,VERSION 是版本號。當你使用make dist 命令時,它會給你生成一個類似helloworld-1.0.tar.gz 的軟件發行包,其中就有對應的軟件包的名字和版本號。
AC_PROG_CC
這個宏將檢查系統所用的C 編譯器。
AC_OUTPUT(FILE)
這個宏是我們要輸出的Makefile 的名字。
我們在使用automake 時,實際上還需要用到其他的一些宏,但我們可以用aclocal 來幫我們自動產生。執行aclocal 后我們會得到aclocal.m4 文件。
產生了configure.in 和aclocal.m4 兩個宏文件后,我們就可以使用autoconf 來產生configure 文件了。
5 、 Makefile.am
Makefile.am 是用來生成Makefile.in 的,需要你手工書寫。Makefile.am 中定義了一些內容:
AUTOMAKE_OPTIONS
這個是automake 的選項。在執行automake 時,它會檢查目錄下是否存在標准GNU 軟件包中應具備的各種文件,例如AUTHORS 、ChangeLog 、NEWS 等文件。我們將其設置成foreign 時,automake 會改用一般軟件包的標准來檢查。
bin_PROGRAMS
這個是指定我們所要產生的可執行文件的文件名。如果你要產生多個可執行文件,那么在各個名字間用空格隔開。
helloworld_SOURCES
這個是指定產生“helloworld” 時所需要的源代碼。如果它用到了多個源文件,那么請使用空格符號將它們隔開。比如需要 helloworld.h ,helloworld.c 那么請寫成helloworld_SOURCES= helloworld.h helloworld.c 。
如果你在bin_PROGRAMS 定義了多個可執行文件,則對應每個可執行文件都要定義相對的filename_SOURCES 。
6 、 automake
我們使用automake --add-missing 來產生Makefile.in 。
選項--add-missing 的定義是“add missing standard files to package” ,它會讓automake 加入一個標准的軟件包所必須的一些文件。
我們用automake 產生出來的Makefile.in 文件是符合GNU Makefile 慣例的,接下來我們只要執行configure 這個shell 腳本就可以產生合適的 Makefile文件了。
7 、 Makefile
在符合GNU Makefiel 慣例的Makefile 中,包含了一些基本的預先定義的操作:
make
根據Makefile 編譯源代碼,連接,生成目標文件,可執行文件。
make clean
清除上次的make 命令所產生的object 文件(后綴為“.o” 的文件)及可執行文件。
make install
將編譯成功的可執行文件安裝到系統目錄中,一般為/usr/local/bin 目錄。
make dist
產生發布軟件包文件(即distribution package )。這個命令將會將可執行文件及相關文件打包成一個tar.gz 壓縮的文件用來作為發布軟件的軟件包。
它會在當前目錄下生成一個名字類似“PACKAGE-VERSION.tar.gz” 的文件。PACKAGE 和VERSION ,是我們在configure.in 中定義的AM_INIT_AUTOMAKE(PACKAGE, VERSION) 。
make distcheck
生成發布軟件包並對其進行測試檢查,以確定發布包的正確性。這個操作將自動把壓縮包文件解開,然后執行configure 命令,並且執行make ,來確認編譯不出現錯誤,最后提示你軟件包已經准備好,可以發布了。
===============================================
helloworld-1.0.tar.gz is ready for distribution
===============================================
make distclean
類似make clean ,但同時也將configure 生成的文件全部刪除掉,包括Makefile 。
