Autoconf和Automake使用
一、生成Makefile流程圖
二、具體實例
執行命令順序:autoscan; aclocal; autoconf; autoheader; automake --add-missing; ./configure; make; ./helloworld;
1、建目錄
在你的工作目錄下建一個helloworld目錄,用來存放helloworld程序及相關文件,如在/home/my/build下:
$ mkdir helloword
$ cd helloworld
2、 helloworld.c
編輯文件,完成后保存退出。現在在helloworld目錄下就應該有一個你自己寫的helloworld.c了。
int main(int argc, char** argv) { printf("Hello, Linux World! "); return 0; }
3、生成configure
$ autoscan
$ ls
configure.scan helloworld.c
執行后在hellowrold目錄下會生成一個文件:configure.scan,可以拿它作為configure.ac的藍本。
4、生成configure.ac
# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) #AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) AC_INIT(HelloWorld, 1.0, jiangtengfei007@163.com) AM_INIT_AUTOMAKE(HelloWorld, 1.0) #這句話很重要,否則下面aclocal出錯 AC_CONFIG_SRCDIR([HelloWorld.c]) AC_CONFIG_HEADERS([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
5、執行aclocal和autoconf
然后執行命令aclocal和autoconf,分別會產生aclocal.m4及configure兩個文件:
$ aclocal $ls aclocal.m4 configure.ac helloworld.c $ autoconf $ ls aclocal.m4 autom4te.cache configure configure.ac helloworld.c
可以看到configure.ac內容是一些宏定義,這些宏經autoconf處理后會變成檢查系統特性、環境變量、軟件必須的參數的shell腳本。
autoconf 是用來生成自動配置軟件源代碼腳本(configure)的工具。configure腳本能獨立於autoconf運行,且在運行的過程中,不需要用戶的干預。
要生成configure文件,你必須告訴autoconf如何找到你所用的宏。方式是使用aclocal程序來生成你的aclocal.m4。
aclocal根據configure.ac文件的內容,自動生成aclocal.m4文件。aclocal是一個perl 腳本程序,它的定義是:“aclocal - create aclocal.m4 by scanning configure.ac”。
autoconf從configure.ac這個列舉編譯軟件時所需要各種參數的模板文件中創建configure。
autoconf需要GNU m4宏處理器來處理aclocal.m4,生成configure腳本。
m4是一個宏處理器。將輸入拷貝到輸出,同時將宏展開。宏可以是內嵌的,也可以是用戶定義的。除了可以展開宏,m4還有一些內建的函數,用來引用文件,執行命令,整數運算,文本操作,循環等。m4既可以作為編譯器的前端,也可以單獨作為一個宏處理器.
7、新建Makefile.am
AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=helloworld helloworld_SOURCES=helloworld.c
automake會根據你寫的Makefile.am來自動生成Makefile.in。Makefile.am中定義的宏和目標,會指導automake生成指定的代碼。例如,宏bin_PROGRAMS將導致編譯和連接的目標被生成。
8、運行automake
➜ AutoConf_ automake --add-missing configure.ac:7: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see: configure.ac:7: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation configure.ac:12: installing './compile' configure.ac:7: installing './install-sh' configure.ac:7: installing './missing' Makefile.am: installing './depcomp'
automake會根據Makefile.am文件產生一些文件,包含最重要的Makefile.in。
9、執行configure生成Makefile
➜ ./configure checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... ./install-sh -c -d checking for gawk... no checking for mawk... no checking for nawk... no checking for awk... awk checking whether make sets $(MAKE)... yes checking whether make supports nested variables... yes checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no 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 ISO C89... none needed checking whether gcc understands -c and -o together... yes checking for style of include used by make... GNU checking dependency style of gcc... gcc3 checking that generated files are newer than configure... done configure: creating ./config.status config.status: creating Makefile config.status: creating config.h config.status: executing depfiles commands
➜ ls -l Makefile
-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile
你可以看到,此時Makefile已經產生出來了。
10、使用Makefile編譯代碼
➜ make /Applications/Xcode.app/Contents/Developer/usr/bin/make all-am gcc -DHAVE_CONFIG_H -I. -g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo -c -o helloworld.o helloworld.c mv -f .deps/helloworld.Tpo .deps/helloworld.Po gcc -g -O2 -o helloworld helloworld.o
11、運行helloworld
➜ ./helloworld
Hello World!!!!
這樣helloworld就編譯出來了,你如果按上面的步驟來做的話,應該也會很容易地編譯出正確的helloworld文件。你還可以試着使用一些其他的make命令,如make clean,make install,make dist,看看它們會給你什么樣的效果。感覺如何?自己也能寫出這么專業的Makefile,老板一定會對你刮目相看。
三、命令詳解
1、 autoscan
autoscan是用來掃描源代碼目錄生成configure.scan文件的。autoscan可以用目錄名做為參數,但如果你不使用參數的話,那么autoscan將認為使用的是當前目錄。autoscan將掃描你所指定目錄中的源文件,並創建configure.scan文件。
2、 configure.scan
configure.scan包含了系統配置的基本選項,里面都是一些宏定義。我們需要將它改名為configure.ac
3、 aclocal
aclocal是一個perl 腳本程序。aclocal根據configure.ac文件的內容,自動生成aclocal.m4文件。aclocal的定義是:“aclocal - create aclocal.m4 by scanning configure.ac”。
4、 autoconf
使用autoconf,根據configure.in和aclocal.m4來產生configure文件。configure是一個腳本,它能設置源程序來適應各種不同的操作系統平台,並且根據不同的系統來產生合適的Makefile,從而可以使你的源代碼能在不同的操作系統平台上被編譯出來。
configure.ac文件的內容是一些宏,這些宏經過autoconf 處理后會變成檢查系統特性、環境變量、軟件必須的參數的shell腳本。configure.ac文件中的宏的順序並沒有規定,但是你必須在所有宏的最前面和最后面分別加上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.ac和aclocal.m4 兩個宏文件后,我們就可以使用autoconf來產生configure文件了。
5、 Makefile.am
Makefile.am是用來生成Makefile.in的,需要你手工書寫。Makefile.am中定義了一些內容:
AUTOMAKE_OPTIONS //在執行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,根據configure.in和Makefile.am來產生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 list //產生發布軟件包文件(即distribution package)。這個命令會將可執行文件及相關文件打包成一個tar.gz壓縮的文件用來作為發布軟件的軟件包。它會在當前目錄下生成一個名字類似“PACKAGE-VERSION.tar.gz”的文件。PACKAGE和VERSION,是我們在configure.ac中定義的AM_INIT_AUTOMAKE(PACKAGE, VERSION)。 make distcheck //生成發布軟件包並對其進行測試檢查,以確定發布包的正確性。這個操作將自動把壓縮包文件解開,然后執行configure命令,並且執行make,來確認編譯不出現錯誤,最后提示你軟件包已經准備好,可以發布了。 make distclean //類似make clean,但同時也將configure生成的文件全部刪除掉,包括Makefile。