autotools使用
??
autotools使用流程
???
?? 正如前面所言,autotools是系列工具,讀者首先要確認系統是否裝了以下工具(可以用which命令進行查看)。
·? aclocal
·? autoscan
·? autoconf
·? autoheader
·? automake
使用autotools主要就是利用各個工具的腳本文件以生成最后的Makefile。其總體流程是這樣的:
·? 使用aclocal生成一個“aclocal.m4”文件,該文件主要處理本地的宏定義;
·? 改寫“configure.scan”文件,并將其重命名為“configure.in”,并使用autoconf文件生成configure文件。
??? 接下來,將通過一個簡單的hello.c例子來熟悉autotools生成makefile的過程.
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”如下所示:
?
#?????????????????????????????????????????????? -*- 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是所要產生軟件套件的名稱,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
在這一步中,通過運行自動配置設置文件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時收集了系統的信息,用戶可以在configure命令中對其進行方便地配置.
在./configure的自定義參數有兩種,一種是開關式(--enable-XXX或--disable-XXX),另一種是開放式,即后面要填入一串字符(--with-XXX=yyyy)參數。讀者可以自行嘗試其使用方法。另外,讀者可以查看同一目錄下的”config.log”文件,以方便調試之用。
到此為止,makefile就可以自動生成了。回憶整個步驟,用戶不再需要定制不同的規則,而只需要輸入簡單的文件及目錄名即可,這樣就大大方便了用戶的使用。下面的圖3.9總結了上述過程:
圖3.9? autotools生成Makefile流程圖
????autotools生成的Makefile除具有普通的編譯功能外,還具有以下主要功能:
1.make
鍵入make默認執行”make all”命令,即目標體為all,其執行情況如下所示:
?
[root@localhost automake]# make
if Gcc -D PACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"hello\" -DVERSION=\"1.0\"? -I. -I.???? -g -O2 -MT hello.o -MD -MP -MF ".deps/hello.Tpo" -c -o hello.o hello.c; \
then mv -f ".deps/hello.Tpo" ".deps/hello.Po"; else rm -f ".deps/hello.Tpo"; exit 1; fi
Gcc? -g -O2?? -o hello? hello.o
此時在本目錄下就生成了可執行文件“hello”,運行“./hello”能出現正常結果,如下所示:
?
[root@localhost automake]# ./hello
Hello everyone!
2.make install
此時,會把該程序安裝到系統目錄中去,如下所示:
?
[root@localhost automake]# make install
if Gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"hello\" -DVERSION=\"1.0\"? -I. -I.???? -g -O2 -MT hello.o -MD -MP -MF ".deps/hello.Tpo" -c -o hello.o hello.c; \
then mv -f ".deps/hello.Tpo" ".deps/hello.Po"; else rm -f ".deps/hello.Tpo"; exit 1; fi
Gcc? -g -O2?? -o hello? hello.o
make[1]: Entering directory '/root/workplace/automake'
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/workplace/automake'
?
此時,若直接運行hello,也能出現正確結果,如下所示:
?
[root@localhost automake]# hello
Hello everyone!
3.make clean
此時,make會清除之前所編譯的可執行文件及目標文件(object file, *.o),如下所示:
?
[root@localhost automake]# make clean
test -z "hello" || rm -f hello
rm -f *.o
4.make dist
此時,make將程序和相關的文檔打包為一個壓縮文檔以供發布,如下所示:
?
[root@localhost automake]# make dist
[root@localhost automake]# ls hello-1.0-tar.gz
hello-1.0-tar.gz
?
可見該命令生成了一個hello-1.0-tar.gz的壓縮文件。
????由上面的講述不難看出,autotools確實是軟件維護與發布的必備工具,也鑒于此,如今GUN的軟件一般都是由automake來制作的。
總結
以上是生活随笔為你收集整理的autotools使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三星高层洗牌 2大内存专家执掌代工业务:
- 下一篇: SIP结构