轉自:http://blog.chinaunix.net/uid-9105154-id-2009272.html
本文中使用的實例仍爲《手動建立makefile簡單實例解析》中的5文件工程實例。
對於一個較大的項目而言,完全手動建立Makefile是一件費力而又容易出錯的工作。autotools系列工具只需用戶輸入簡單的目標文件、依賴文件、文件目錄等就可以比較輕鬆地生成Makefile了。現在Linux上的軟件開發一般都是用autotools來製作Makefile。
autotools工具主要有:aclocal、autoscan、autoconf、autoheader、automake。使用autotools主要就是利用各個工具的腳本文件來生成最後的Makefile。下面結合實例來介紹具體的流程。
第一步 autoscan
使用autoscan在給定目錄及其子目錄樹中檢查源文件,如果沒有給出目錄,就在當前目錄及其子目錄樹中進行檢查。最終生成兩個文件:configure.scan、autoscan.log
[armlinux@lqm autotools-easy]$ tree
.
|-- main.c
|-- mytool1.c
|-- mytool1.h
|-- mytool2.c
`-- mytool2.h
![None.gif]()
0 directories, 5 files
[armlinux@lqm autotools-easy]$ autoscan
[armlinux@lqm autotools-easy]$ tree
.
|-- autoscan.log
|-- configure.scan
|-- main.c
|-- mytool1.c
|-- mytool1.h
|-- mytool2.c
`-- mytool2.h
![None.gif]()
0 directories, 7 files 其中,configure.scan是configure.in的原型文件。而configure.in是autoconf的腳本配置文件。所以下一步的工作就是要對configure.scan進行修改,將其轉化爲configure.in。
第二步 autoconf
configure.scan文件內容如下:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
![None.gif]()
AC_PREREQ(2.57)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([config.h])
![None.gif]()
# Checks for programs.
AC_PROG_CC
![None.gif]()
# Checks for libraries.
![None.gif]()
# Checks for header files.
![None.gif]()
# Checks for typedefs, structures, and compiler characteristics.
![None.gif]()
# Checks for library functions.
AC_OUTPUT說明:
1、以“#”號開始的是行爲註釋。
2、AC_PREREQ宏聲明本文件要求的autoconf版本。
3、AC_INIT宏用來定義軟件的名稱和版本等信息,這裏的BUG-REPORT-ADDRESS可以省略。
4、AC_CONFIG_SRCDIR宏用來偵測所指定的源碼文件是否存在,來確定源碼目錄的有效性。這個參數一般不需要修改。
5、AC_CONFIG_HEADER宏用於生成config.h文件,以便autoheader使用。
修改時需要增加一個宏AM_INIT_AUTOMAKE(PACKAGE,VERSION),還要把AC_CONFIG_HEADER更改爲AM_CONFIG_HEADER。
AC_OUTPUT是輸出文件,如果有多個目錄需要產生Makefile如AC_OUTPUT(Makefile src/Makefile)
具體如下:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
![None.gif]()
AC_PREREQ(2.57)
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_INIT(main,1.0)
AM_INIT_AUTOMAKE(main,1.0)
AC_CONFIG_SRCDIR([main.c])
AM_CONFIG_HEADER([config.h])
![None.gif]()
# Checks for programs.
AC_PROG_CC
![None.gif]()
![None.gif]()
# Checks for libraries.
![None.gif]()
# Checks for header files.
![None.gif]()
# Checks for typedefs, structures, and compiler characteristics.
![None.gif]()
# Checks for library functions.
AC_OUTPUT(Makefile)第三步 autoheader
首先把configure.scan更改名稱爲configure.in。然後執行aclocal,autoconf,autoheader。
[armlinux@lqm autotools-easy]$ mv configure.scan configure.in
[armlinux@lqm autotools-easy]$ ls
autoscan.log configure.in main.c mytool1.c mytool1.h mytool2.c mytool2.h
[armlinux@lqm autotools-easy]$ aclocal
[armlinux@lqm autotools-easy]$ ls
aclocal.m4 autoscan.log configure.in main.c mytool1.c mytool1.h mytool2.c mytool2.h
[armlinux@lqm autotools-easy]$ autoconf
[armlinux@lqm autotools-easy]$ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.in main.c mytool1.c mytool1.h mytool2.c mytool2.h
[armlinux@lqm autotools-easy]$ autoheader
第四步 automake
這是很重要的一步。automake需要的腳本配置文件是Makefile.am,這個文件需要自己建立。
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=main.c mytool1.c mytool1.h mytool2.c mytool2.h AUTOMAKE_OPTIONS爲設置automake的選項。automake提供了3種軟件等級:foreign、gnu、gnits,讓用戶選擇使用,默認等級是gnu。現在使用的foreign只是檢測必要的文件。
bin_PROGRAMS定義了要產生的執行文件名。如果產生多個可執行文件,每個文件名用空格隔開。
file_SOURCES定義file這個執行程序的依賴文件。同樣的,對於多個執行文件,那就要定義相應的file_SOURCES。
接下來就是使用automake對其生成configure.in文件。這裏可以使用選項--adding-missing讓automake自動添加一些必要的腳本文件。
第五步 運行configure
[armlinux@lqm autotools-easy]$ automake --add-missing
[armlinux@lqm autotools-easy]$ ./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... 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: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands這樣就完成了Makefile的製作。這是具有的功能:make、make install、make uninstall、make clean、make distclean、make dist。
1、鍵入make默認執行make all。其目標體爲all。
[armlinux@lqm autotools-easy]$ make
make all-am
make[1]: Entering directory `/home/armlinux/program/autotools-easy'
source='main.c'object='main.o' libtool=no
depfile='.deps/main.Po' tmpdepfile='.deps/main.TPo'
depmode=gcc3 /bin/sh ./depcomp
gcc -DHAVE_CONFIG_H -I. -I. -I. -g -O2 -c `test -f 'main.c'|| echo './'`main.c
source='mytool1.c'object='mytool1.o' libtool=no
depfile='.deps/mytool1.Po' tmpdepfile='.deps/mytool1.TPo'
depmode=gcc3 /bin/sh ./depcomp
gcc -DHAVE_CONFIG_H -I. -I. -I. -g -O2 -c `test -f 'mytool1.c'|| echo './'`mytool1.c
source='mytool2.c'object='mytool2.o' libtool=no
depfile='.deps/mytool2.Po' tmpdepfile='.deps/mytool2.TPo'
depmode=gcc3 /bin/sh ./depcomp
gcc -DHAVE_CONFIG_H -I. -I. -I. -g -O2 -c `test -f 'mytool2.c'|| echo './'`mytool2.c
gcc -g -O2 -o main main.o mytool1.o mytool2.o
make[1]: Leaving directory `/home/armlinux/program/autotools-easy'2、make install(uninstall)
3、make clean (distclean)
make clean僅僅是清除之前編譯的可執行文件及配置文件。而make distclean要清除所有生成的文件。
[armlinux@lqm autotools-easy]$ make clean
test -z "main"|| rm -f main
rm -f *.o core *.core
[armlinux@lqm autotools-easy]$ make distclean
test -z "main"|| rm -f main
rm -f *.o core *.core
rm -f *.tab.c
rm -rf ./.deps
rm -f Makefile
rm -f config.h stamp-h1
rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH
rm -f config.status config.cache config.log configure.lineno4、make dist
將所有的程序和相關的文檔打包爲一個壓縮文件以供發佈。
[armlinux@lqm autotools-easy]$ make dist
{ test !-d main-1.0|| { find main-1.0-type d !-perm -200-exec chmod u+w {} ';'&& rm -fr main-1.0; }; }
mkdir main-1.0
find main-1.0-type d !-perm -777-exec chmod a+rwx {} ; -o
!-type d !-perm -444-links 1-exec chmod a+r {} ; -o
!-type d !-perm -400-exec chmod a+r {} ; -o
!-type d !-perm -444-exec /bin/sh /home/armlinux/program/autotools-easy/install-sh -c -m a+r {} {} ;
|| chmod -R a+r main-1.0
/bin/sh /home/armlinux/program/autotools-easy/missing --run tar chof - main-1.0| GZIP=--best gzip -c >main-1.0.tar.gz
{ test !-d main-1.0|| { find main-1.0-type d !-perm -200-exec chmod u+w {} ';'&& rm -fr main-1.0; }; }
[armlinux@lqm autotools-easy]$ ls
aclocal.m4 config.h config.status depcomp main-1.0.tar.gz Makefile missing mytool1.h mytool2.h
autom4te.cache config.h.in configure install-sh main.c Makefile.am mkinstalldirs mytool1.o mytool2.o
autoscan.log config.log configure.in main main.o Makefile.in mytool1.c mytool2.c stamp-h1