在Linux系統下開發一個較大的項目,完全手動建立Makefile是一件費力而又容易出錯的工作。autotools系列工具只需用戶輸入簡單的目標文件、依賴文件、文件目錄等就可以比較輕松地生成Makefile了。
這極大地簡化了Makefile的編寫和維護,作者也是剛體驗到其威力,所以將其過程記錄下來。
本文以一個簡單的hello.c文件進行驗證。
准備工作
首先需要安裝autotools系列工具,包括aclocal、autoscan、automake、autoheader、autoconf等。
如果在centos或redhat系統下可以通過rpm –qa | grep auto來查看各個應用程序,如果沒有,直接yum install automake即可安裝。
大致流程
- 1. 編寫源代碼以及手動書寫Makefile.am文件(稍后介紹);
- 2. 依次使用各種autotools系列工具配置編譯屬性;
autoscan #掃面當前目錄、源文件,生成configure.scan文件 aclocal #根據configure.in生成aclocal.m4文件以及autom4te.cache文件夾 autoconf #根據configure.in和aclocal.m4來產生configure文件 autoheader #生成配置頭文件的模板config.h.in touch README NEWS AUTHORS ChangeLog #生成一些聲明性文件 automake --add-missing #生成Makefiel.in和所需要的腳本
- 3. 編譯、生成、部署:
make make install make clean make dist #制作發布的軟件包 make distcheck #檢驗完整性並制作軟件包
詳細步驟
1. autoscan
將生成configure.scan和autoscan.log文件,它會在給定目錄及其子目錄樹中檢查源文件,若沒有給定目錄,就在當前目錄及其子目錄樹中進行檢查。它會搜索源文件以尋找一般的移植性問題並且創建一個文件configure.scan,通過這個文件我們可以創建autoconf需要的模版文件。
[root@lzv6 hello]# ls hello.c [root@lzv6 hello]# autoscan [root@lzv6 hello]# ls autoscan.log configure.scan hello.c
2. 重命名configure.scan,並修改configure.in
mv configure.scan configure.in #將上步生成的configure.scan更改為autoconf需要的文件模版configure.in
在configure.in中增加以下內容:
AC_INIT([hello], [0.1], [lizhenghn@gmail.com]) AM_INIT_AUTOMAKE(hello,1.0) AC_PROG_CXX AC_PROG_LIBTOOL AC_CONFIG_FILES(Makefile)
修改后的configure.in內容如下:
1 # -*- Autoconf -*- 2 # Process this file with autoconf to produce a configure script. 3 4 AC_PREREQ([2.63]) 5 AC_INIT([hello], [0.1], [lizhenghn@gmail.com]) #用來定義軟件的名字、版本,以及郵箱地址信息 6 #AC_INIT([hello], [0.1]) 7 AM_INIT_AUTOMAKE(hello,0.1) #必須添加此行,描述了要生成的軟件包的名字及其版本號 8 AC_CONFIG_SRCDIR([hello.c]) #用來偵測所指定的源碼文件是否存在 9 AC_CONFIG_HEADERS([config.h]) 10 11 # Checks for programs. 12 AC_PROG_CC #檢測系統所用的C編譯器 13 AC_PROG_CXX 14 15 # Checks for libraries. 16 #AC_PROG_RANLIB #需要需要靜態庫 17 AC_PROG_LIBTOOL #如果需要動態庫 18 19 # Checks for header files. 20 21 # Checks for typedefs, structures, and compiler characteristics. 22 23 # Checks for library functions. 24 AC_CONFIG_FILES(Makefile) 25 AC_OUTPUT
3. aclocal :復制所有的宏命令
configure.in 里面包含了一系列的宏命令,運行aclocal的目的是把工程需要的宏命令展開。aclocal.m4 就是configure.in中用到的宏定義。本步會生成aclocal.m4文件和autom4te.cache文件夾。
[root@lzv6 hello]# aclocal [root@lzv6 hello]# ls aclocal.m4 autom4te.cache autoscan.log configure.in hello.c
4. autoheader :生成config.h.in文件
生成配置頭文件的模板config.h.in文件。
[root@lzv6 hello]# autoheader [root@lzv6 hello]# ls aclocal.m4 autom4te.cache autoscan.log config.h.in configure.in hello.c
5. 創建一些聲明性文件
此處創建一些聲明性,但非必須的文件,文件內容可以暫時都為空。
[root@lzv6 hello]# touch README NEWS AUTHORS ChangeLog
6. autoconf :生成configure腳本
[root@lzv6 hello]# autoconf [root@lzv6 hello]# ls aclocal.m4 AUTHORS autom4te.cache autoscan.log ChangeLog config.h.in configure configure.in hello.c NEWS README
7. 編寫Makefile.am
如下是一個簡單的Makefile.am
AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=hello hello_SOURCES=hello.c
說明:AUTOMAKE_OPTIONS為設置automake的選項。automake提供了3種軟件等級:foreign、gnu、gnits,默認等級是gnu。此處使用的foreign表示只是檢測必要的文件。
bin_PROGRAMS定義了要產生的執行文件名。如果產生多個可執行文件,每個文件名用空格隔開。
file_SOURCES定義file這個執行程序的依賴文件。同樣的,對於多個執行文件,那就要定義相應的file_SOURCES。
8. automake --add-missing
生成Makefiel.in和所需要的腳本,其中add-missing選項會讓automake自動添加一些必須的腳本文件。
[root@lzv6 hello]# automake --add-missing configure.in:16: installing `./config.guess' configure.in:16: installing `./config.sub' configure.in:7: installing `./install-sh' configure.in:16: required file `./ltmain.sh' not found configure.in:7: installing `./missing' Makefile.am: installing `./depcomp' [root@lzv6 hello]# libtoolize --automake --copy --debug --force #注意此處步驟!!! [root@lzv6 hello]# automake --add-missing [root@lzv6 hello]# ls aclocal.m4 autom4te.cache ChangeLog config.h.in configure depcomp install-sh Makefile.am missing README AUTHORS autoscan.log config.guess config.sub configure.in hello.c ltmain.sh Makefile.in NEWS
說明:第一次運行時報錯,此時運行命令libtoolize --automake --copy --debug --force即可解決。
至此該工程的Makefile就配置完成了。
在以后的使用中進行編譯時可以直接運行命令:
[root@lzv6 hello]# ./configure [root@lzv6 hello]# make [root@lzv6 hello]# make install #默認安裝在/usr/local/bin/
[root@lzv6 hello]# make dist #打包,會在當前目前下生成hello-0.1.tar.gz
當你增加代碼文件時,其實主需要修改Makefile.am即可,這里就不再詳述了。
這里附上一張整個步驟的流程圖:
參考:
http://www.ibm.com/developerworks/cn/linux/l-makefile/