使用autotools建立Makefile簡單實例解析

轉自: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

None.gif[armlinux@lqm autotools-easy]$ tree
None.gif.
None.gif
|-- main.c
None.gif
|-- mytool1.c
None.gif
|-- mytool1.h
None.gif
|-- mytool2.c
None.gif`
-- mytool2.h
None.gif
None.gif
0 directories, 5 files
None.gif[armlinux@lqm autotools
-easy]$ autoscan
None.gif[armlinux@lqm autotools
-easy]$ tree
None.gif.
None.gif
|-- autoscan.log
None.gif
|-- configure.scan
None.gif
|-- main.c
None.gif
|-- mytool1.c
None.gif
|-- mytool1.h
None.gif
|-- mytool2.c
None.gif`
-- mytool2.h
None.gif
None.gif
0 directories, 7 files

其中,configure.scan是configure.in的原型文件。而configure.in是autoconf的腳本配置文件。所以下一步的工作就是要對configure.scan進行修改,將其轉化爲configure.in。

第二步   autoconf

configure.scan文件內容如下:

None.gif#                                               -*- Autoconf -*-
None.gif# Process
this file with autoconf to produce a configure script.
None.gif
None.gifAC_PREREQ(
2.57)
None.gifAC_INIT(FULL
-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
None.gifAC_CONFIG_SRCDIR([main.c])
None.gifAC_CONFIG_HEADER([config.h])
None.gif
None.gif# Checks
for programs.
None.gifAC_PROG_CC
None.gif
None.gif# Checks
for libraries.
None.gif
None.gif# Checks
for header files.
None.gif
None.gif# Checks
for typedefs, structures, and compiler characteristics.
None.gif
None.gif# Checks
for library functions.
None.gifAC_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)

具體如下:

None.gif#                                               -*- Autoconf -*-
None.gif# Process
this file with autoconf to produce a configure script.
None.gif
None.gifAC_PREREQ(
2.57)
None.gif#AC_INIT(FULL
-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
None.gifAC_INIT(main,
1.0)
None.gifAM_INIT_AUTOMAKE(main,
1.0)
None.gifAC_CONFIG_SRCDIR([main.c])
None.gifAM_CONFIG_HEADER([config.h])
None.gif
None.gif# Checks
for programs.
None.gifAC_PROG_CC
None.gif
None.gif
None.gif# Checks
for libraries.
None.gif
None.gif# Checks
for header files.
None.gif
None.gif# Checks
for typedefs, structures, and compiler characteristics.
None.gif
None.gif# Checks
for library functions.
None.gifAC_OUTPUT(Makefile)

第三步  autoheader

首先把configure.scan更改名稱爲configure.in。然後執行aclocal,autoconf,autoheader。

None.gif[armlinux@lqm autotools-easy]$ mv configure.scan configure.in
None.gif[armlinux@lqm autotools
-easy]$ ls
None.gifautoscan.log  configure.
in  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
None.gif[armlinux@lqm autotools
-easy]$ aclocal
None.gif[armlinux@lqm autotools
-easy]$ ls
None.gifaclocal.m4  autoscan.log  configure.
in  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
None.gif[armlinux@lqm autotools
-easy]$ autoconf
None.gif[armlinux@lqm autotools
-easy]$ ls
None.gifaclocal.m4  autom4te.cache  autoscan.log  configure  configure.
in  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
None.gif[armlinux@lqm autotools
-easy]$ autoheader


第四步  automake

這是很重要的一步。automake需要的腳本配置文件是Makefile.am,這個文件需要自己建立。

None.gifAUTOMAKE_OPTIONS=foreign
None.gifbin_PROGRAMS
=main
None.gifmain_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

None.gif[armlinux@lqm autotools-easy]$ automake --add-missing
None.gif[armlinux@lqm autotools
-easy]$ ./configure
None.gifchecking
for a BSD-compatible install... /usr/bin/install -c
None.gifchecking whether build environment
is sane... yes
None.gifchecking
for gawk... gawk
None.gifchecking whether make sets $(MAKE)... yes
None.gifchecking
for gcc... gcc
None.gifchecking
for C compiler default output... a.out
None.gifchecking whether the C compiler works... yes
None.gifchecking whether we are cross compiling... no
None.gifchecking
for suffix of executables...
None.gifchecking
for suffix of object files... o
None.gifchecking whether we are
using the GNU C compiler... yes
None.gifchecking whether gcc accepts
-g... yes
None.gifchecking
for gcc option to accept ANSI C... none needed
None.gifchecking
for style of include used by make... GNU
None.gifchecking dependency style of gcc... gcc3
None.gifconfigure: creating .
/config.status
None.gifconfig.status: creating Makefile
None.gifconfig.status: creating config.h
None.gifconfig.status: config.h
is unchanged
None.gifconfig.status: executing depfiles commands

這樣就完成了Makefile的製作。這是具有的功能:make、make install、make uninstall、make clean、make distclean、make dist。

1、鍵入make默認執行make all。其目標體爲all。

None.gif[armlinux@lqm autotools-easy]$ make
None.gifmake  all
-am
None.gifmake[
1]: Entering directory `/home/armlinux/program/autotools-easy'
None.gif
source='main.c'object='main.o' libtool=no
None.gifdepfile
='.deps/main.Po' tmpdepfile='.deps/main.TPo'
None.gifdepmode
=gcc3 /bin/sh ./depcomp
None.gifgcc
-DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -c `test -f 'main.c'|| echo './'`main.c
None.gifsource
='mytool1.c'object='mytool1.o' libtool=no
None.gifdepfile
='.deps/mytool1.Po' tmpdepfile='.deps/mytool1.TPo'
None.gifdepmode
=gcc3 /bin/sh ./depcomp
None.gifgcc
-DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -c `test -f 'mytool1.c'|| echo './'`mytool1.c
None.gifsource
='mytool2.c'object='mytool2.o' libtool=no
None.gifdepfile
='.deps/mytool2.Po' tmpdepfile='.deps/mytool2.TPo'
None.gifdepmode
=gcc3 /bin/sh ./depcomp
None.gifgcc
-DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -c `test -f 'mytool2.c'|| echo './'`mytool2.c
None.gifgcc  
-g -O2   -o main  main.o mytool1.o mytool2.o  
None.gifmake[
1]: Leaving directory `/home/armlinux/program/autotools-easy'

2、make install(uninstall)

3、make clean (distclean)

make clean僅僅是清除之前編譯的可執行文件及配置文件。而make distclean要清除所有生成的文件。

None.gif[armlinux@lqm autotools-easy]$ make clean
None.giftest
-z "main"|| rm -f main
None.gifrm
-f *.o core *.core
None.gif[armlinux@lqm autotools
-easy]$ make distclean
None.giftest
-z "main"|| rm -f main
None.gifrm
-f *.o core *.core
None.gifrm
-f *.tab.c
None.gifrm
-rf ./.deps
None.gifrm
-f Makefile
None.gifrm
-f config.h stamp-h1
None.gifrm
-f TAGS ID GTAGS GRTAGS GSYMS GPATH
None.gifrm
-f config.status config.cache config.log configure.lineno

4、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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章