GNU Autotools的研究(轉)

最近對Linux下軟件項目的構建過程研究了一番。Linux下的軟件項目一般用Autotools工具集和make工具來構建,咱們一般使用./configure、make、make install這樣的命令來編譯安裝軟件包,則這樣的項目通常就是使用Autotools工具集來構建,再加上GNU make工具來編譯安裝。
   使用Autotools的目的:
  (1)構建可移植的軟件包。在不一樣操做系統上(主要是不一樣的類Unix系統),可能一樣功能函數名稱的不一樣,一樣功能的庫的名字的不一樣,以及頭文件的不一樣,使得軟件包的移植成了大的問題。
  (2)統一構建過程。對軟件包的編譯方式、編譯方法進行統一化,以便於項目的構建。
  (3)快速製做Makefile文件。Linux下的項目通常用make工具來管理,須要編寫Makefile文件。對於一個較大的項目而言,徹底手動創建Makefile是一件費力而又容易出錯的工做。autotools系列工具只需用戶輸入簡單的目標文件、依賴文件、文件目錄等就能夠比較輕鬆地生成Makefile了。如今Linux上的軟件開發通常都是用autotools來製做Makefile。

   Autotools工具主要有:autoscan、aclocal、autoheader、autoconf、automake。使用autotools主要就是利用各個工具的腳本文件來生成最後的Makefile。各個工具的做用:
   (1)autoscan:它會在給定目錄及其子目錄樹中檢查源文件,若沒有給出目錄,就在當前目錄及其子目錄樹中進行檢查。它會掃描源代碼以搜尋普通的可移植性問題,好比檢查編譯器,庫,頭文件等,生成configure.scan文件(並附帶產生一個日誌文件autoscan.log),它是configure.ac(或configure.in)的原型文件,而configure.ac是autoconf的腳本配置文件。 
   過程:源代碼文件-->autoscan-->configure.scan-->configure.ac(或configure.in) 
   (2)aclocal: 根據已經安裝的宏,用戶定義的宏和acinclude.m4文件(若是有的話)中的宏,將configure.in(或configure.ac)文件所須要的宏集中地定義到文件aclocal.m4中。aclocal是一個perl腳本程序。之因此要建立aclocal.m4文件,是由於automake包含一堆的autoconf宏,這些宏可以在你的軟件包中使用,而automake在某些狀況下實際上也須要其中的一些宏。這堆宏必須在你的aclocal.m4中定義,不然他們將不會被autoconf看到。aclocal程序將會基於configure.in的內容自動產生aclocal.m4。
   過程:configure.ac、acinclude.m4(可選)、用戶宏文件(可選)-->aclocal-->aclocal.m4 
   (3)autoheader:經過掃描configure.ac(或configure.in),根據其中的某些宏(好比cpp宏定義)產生宏定義的模板文件config.h.in,configure腳本能夠用這個文件生成一個由"#define"語句構成的宏定義文件config.h。
   當咱們的程序要使用一些與平臺相關的庫時,因爲在不一樣的平臺上一樣功能的庫名字、函數、頭文件的不一樣等,爲了使程序具備可移植性,咱們常常會在程序中使用一大堆的#ifdef或#ifndef預編譯指令,這樣很是地不方便。而autoheader就是解決這個問題的,在它生成的模板文件config.h.in中,爲這些庫、函數、頭文件等定義了#undef宏標誌,configure腳本把它轉換成config.h後,就會變成#define宏標誌,用做這些庫、函數、頭文件的保護符。這樣,你能夠在程序裏包含這個config.h,而後使用其中的某些定義好的常量。
   過程:configure.ac、aclocal.m4(可選)-->autoheader-->config.h.in 
   (4)autoconf:將configure.ac中的宏展開,生成configure腳本文件。這個過程可能要用到aclocal.m4中定義的宏。
   過程:configure.ac、aclocal.m4(可選)、config.h.in(可選)-->autoconf-->configure 
   (5)automake(-a):選要本身寫一個Makefile.am文件,而後automake根據configure.ac和Makefile.am中定義的結構,生成Makefile.in文件。若是在configure.ac中定義了一些特殊的宏,好比AC_PROG_LIBTOOL,它會調用libtoolize併產生config.guess、config.sub、ltmain.sh等文件。
   automake提供了三種軟件等級foreign、gnu和gnits,讓用戶選擇採用,默認等級爲gnu。使用gnu等級時,automake還要求你必須有一些額外的文件,它們是install-sh、missing、depcomp、INSTALL、NEWS、 README、AUTHORS、ChangeLog、COPYING共9個文件,它們是一個符合GNU規範的代碼文件結構所必須的文件。automake能夠經過附加--add-missing參數(或-a)自動生成其中的一些文件(install-sh、missing、depcomp、INSTALL、COPYING),剩下的NEWS、README、AUTHORS、ChangeLog要本身建立。
   config.guess:GNU構建系統區分三類機器,運行構建用的編譯器的「構建」機器、運行構建的軟件包的「主機」機器、編譯器用來產生代碼的「目標」機器。這個腳本用來猜想「構建」機器的類型,並輸出腳本所運行的系統的配置名(configure name)。
   config.sub:使配置名規範化。
   過程:Makefile.am、configure.ac-->automake(-a)-->Makefile.in、install-sh、missing、depcomp、INSTALL、COPYING。若調用了libtoolize,則另外產生的還有config.guess、config.sub、ltmain.sh等。 
   (6)用./configure生成Makefile文件。configure腳本會收集系統的信息,建立config.status(這個文件可用來從新建立configure腳本),使用Makefile.in來建立Makefile文件,使用config.h.in(若是有的話)來建立config.h文件,並生成一個日誌文件config.log(記錄一些建立時的調試信息等),ltmain.sh會產生libtool文件,有時還會產生文件config.cache,stamp-h1(config.h的時間戳文件)等。這樣就完成了Makefile文件的製做,而且經常具備如下的功能:make、make install、make uninstall、make clean、make distclean、make dist。
   使用configure命令時,用戶能夠對其進行方便地配置。./configure的自定義參數有兩種,一種是開關式(--enable-XXX或--disable-XXX),另外一種是開放式,即後面要填入一串字符(--with-XXX=yyyy)參數。
   製做完後,再查看config.h(若是存在的話),若是有必要,須要再改寫源代碼,並從第(1)步中的autoscan從新開始。html

   libtool:提供一種標準的方法來建立靜態庫或共享庫。它把特定於平臺的庫的產生過程的複雜性隱藏在一個統一的接口後面,這個接口經過libtool被全部的平臺支持。
   過程:Makefile.in、config.h.in-->configure-->config.status、Makefile、config.h 
   (7)make編譯。如今就會根據Makefile文件中定義的目標和規則來編譯整個源代碼樹,生成可執行的程序。經常還有文件mkinstalldirs(通常要本身編寫)等。
   (8)make install(uninstall):把該程序安裝到系統目錄中去,make uninstall則卸載程序。
   (9)make dist:將全部的程序和相關的文檔打包爲一個.tar.gz壓縮文件以供發佈。
   (10)make clean(distclean):清除以前編譯目標文件、的可執行文件及配置文件。而make distclean要清除全部生成的文件。   
   另外還有幾個有用的工具在項目的構建過程當中有時也會用到。
   autoupdate:更新configure.in文件,以適應更新版本的Autoconf。原來的configure.in會被備份下來。
   autoreconf:更新產生的配置文件(常在你更新了Autoconf工具的版本,或者安裝了新版本的GNU構建系統時使用)。
   ifname:掃描C源程序文件(若沒給定則使用標準輸入),在標準輸出上輸出那些出如今#if,#elif,#ifdef或#ifndef中的標識符,每一個標識符顯示爲一行,其後跟一空格和所屬的文件名。

   下面用一個具體的實例來介紹整個構建流程。咱們的項目有五個文件mytool1.h,mytool2.h,mytool1.c,mytool2.c,main.c。文件內容以下:python

 

[cpp]  view plain copy
 
  1. /* mytool1.h */  
  2. #ifndef _MYTOOL_1_H  
  3. #define _MYTOOL_1_H  
  4. void mytool1_print(char *print_str);  
  5. #endif  
  6. /* mytool2.h */  
  7. #ifndef _MYTOOL_2_H  
  8. #define _MYTOOL_2_H  
  9. void mytool2_print(char *print_str);  
  10. #endif  
  11. /* mytool1.c */  
  12. #include "mytool1.h"  
  13. #include <stdio.h>  
  14. void mytool_print(char *print_str){  
  15.     printf("This is mytool1 print:%s",print_str);  
  16. }  
  17. /* mytool2.c */  
  18. #include "mytool2.h"  
  19. #include <stdio.h>  
  20. void mytool2_print(char *print_str){  
  21.     printf("This is mytool2 print:%s",print_str);  
  22. }  
  23. /* main.c */  
  24. #include "mytool1.h"  
  25. #include "mytool2.h"  
  26. int main(){  
  27.     mytool1_print("Hello mytool1!");  
  28.     mytool2_print("Hello mytool2!");  
  29.     return 0;  
  30. }  

 

   構建流程以下:
   (1)運行autoscan。生成configure.scan和autoscan.log。操做結果以下:linux

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  2. main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h  
  3. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ autoscan  
  4. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  5. autoscan.log  configure.scan  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h  

 

   其中autoscan.log文件內容爲空(通常常記錄一些建立時的日誌信息),configure.scan文件的內容以下:緩存

 

[python]  view plain copy
 
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3. AC_PREREQ(2.61)  
  4. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)  
  5. AC_CONFIG_SRCDIR([mytool1.h])  
  6. AC_CONFIG_HEADER([config.h])  
  7. # Checks for programs.  
  8. AC_PROG_CC  
  9. # Checks for libraries.  
  10. # Checks for header files.  
  11. # Checks for typedefs, structures, and compiler characteristics.  
  12. # Checks for library functions.  
  13. AC_OUTPUT  

 

   內容分析:
AC_PREREQ:聲明本文件要求的autoconf版本,如本例使用的版本2.61,無需修改。
AC_INIT:用來定義軟件的包名稱、版本號、Bug報告地址,須要你本身填進去(名稱能夠隨意起,只要符合標識符規則就行)。這裏Bug報告地址BUG-REPORT-ADDRESS也能夠省略,不省略時則通常填做者的e-mail。
AC_CONFIG_SRCDIR:用來偵測所指定的源碼文件是否存在,來肯定源碼目錄的有效性,在此處爲當前目錄下的mytool1.h。這個參數通常不須要修改。
AC_CONFIG_HEADER:用於生成config.h文件,以便autoheader使用。
AC_PROG_CC:表示測試程序用的語言,這裏爲C語言。而C++用AC_PROG_CXX,其餘語言參考autoscan的手冊。
AC_OUTPUT:指定要輸出的Makefile文件名,注意是每一個有源代碼(或者有Makefile.am)的地方都要輸出的Makefile文件,不一樣的文件之間用空格隔開,如AC_OUTPUT(Makefile src/Makefile)。也可用AC_CONFIG_FILES宏來指定Makefile文件,功能是同樣的,多個Makefie文件(好比子目錄中也有Makefile文件)時用空格分開。
   其餘的一些這裏沒用到的宏定義主要有AC_PROG_INSTALL、PKG_CHECK_MODULES和AC_SUBST,AC_PROG_RANLIB(用到了庫時要用這個宏)等,具體可參數幫助手冊。中間的註釋間能夠添加分別用於測試程序、測試函數庫、測試頭文件等宏定義。
   (2)修改configure.scan並重命名爲configure.ac(或configure.in也可),而後運行aclocal。爲了使aclocal,autoheader,automake能使用這個文件(他們須要的文件名必須是configure.ac或configure.in),在修改時還要加入一些必要的宏,其中AM_INIT_AUTOMAKE(PACKAGE,VERSION)是automake必備的宏,參數的含義跟上面介紹的同樣。另外當使用到庫時,須要加入AC_PROG_RANLIB等。修改後的文件名爲configure.ac,文件內容以下:bash

 

[python]  view plain copy
 
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3. AC_PREREQ(2.61)  
  4. AC_INIT(mytool, 0.1, zhoudaxia@gmail.com)  
  5. AM_INIT_AUTOMAKE(mytool,0.1)  
  6. AC_CONFIG_SRCDIR([mytool1.h])  
  7. AC_CONFIG_HEADER([config.h])  
  8. # Checks for programs.  
  9. AC_PROG_CC  
  10. # Checks for libraries.  
  11. # Checks for header files.  
  12. # Checks for typedefs, structures, and compiler characteristics.  
  13. # Checks for library functions.  
  14. AC_OUTPUT(Makefile)  

 

   這可能會有個疑問,爲何在(1)中autoscan不直接建立configure.ac,而是要建立爲configure.scan後再讓你來修改並重命名呢?固然,如今你也許不須要本身來寫configure.ac,可是之後你可能會本身寫。你固然不但願一不當心把configure.ac覆蓋掉了。因此autoscan不會直接建立爲configure.ac(或configure.in)。
   如今運行aclocal,根據configure.ac生成aclocal.m4文件。操做以下:函數

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ aclocal  
  2. aclocal: `configure.ac' or `configure.in' is required  
  3. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ mv configure.scan configure.ac  
  4. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ aclocal  
  5. /usr/share/aclocal/snacc.m4:24: warning: underquoted definition of AM_PATH_SNACC  
  6.   run info '(automake)Extending aclocal'  
  7.   or see http://sources.redhat.com/automake/automake.html#Extending-aclocal  
  8. /usr/share/aclocal/libmcrypt.m4:17: warning: underquoted definition of AM_PATH_LIBMCRYPT  
  9. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  10. aclocal.m4  autom4te.cache  autoscan.log  configure.ac  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h  

 

   產生了一個警告,是與acloal程序自己有關的,與咱們的configure.ac無關,無論它了。可見同時也生成了一個緩存文件autom4te.cache。
   (3)運行autoheader。根據configure.ac產生config.h.in文件。注意configure.ac中必需要有AC_CONFIG_HEADER宏才能運行autoheader。操做以下:工具

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ autoheader  
  2. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  3. aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure.ac  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h  

 

   (4)運行autoconf,根據configure.ac、aclocal.m4和config.h.in生成configure腳本文件。操做以下:測試

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ autoconf  
  2. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  3. aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure  configure.ac  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h  

 

   (5)運行automake,根據Makefile.am和configure.ac生成Makefile.in文件。先要編寫Makefile.am文件,這很關鍵。Makefile.am編寫以下:ui

 

[python]  view plain copy
 
  1. AUTOMAKE_OPTIONS=gnu  
  2. bin_PROGRAMS=mytool  
  3. mytool_SOURCES=main.c mytool1.c mytool1.h mytool2.c mytool2.h  

 

   內容分析:
   AUTOMAKE_OPTIONS:設置automake的選項。automake提供了3種軟件等級foreign、gnu、gnits讓用戶選擇使用,默認等級是gnu。如今使用的gnu(因爲是默承認以省略這一行),它會檢測GNU規範須要的文件。
   bin_PROGRAMS:定義要產生的可執行程序名。若是要產生多個執行文件,每一個文件名用空格隔開。
   mytool_SOURCES:定義mytool這個執行程序的依賴文件。_SOURCES前面的要與bin_PROGRAMS所定義的相一致。若是」mytool」這個程序是由多個原始文件所產生的,則必須把它所用到的全部原始文件都列出來,並用空格隔開。例目標體「mytool」要「main.c」、「mytool1.c」、「mytool1.h」、「mytool2.c」、「mytool2.h」五個依賴文件,則須要像上面那樣定義。要注意的是,若是要定義多個執行文件,則對每一個執行程序都要定義相應的XXX_SOURCES。
   其餘的一些這裏沒用到的宏的介紹:
   SUBDIRS=src:若是執行程序依賴的文件不在同一個目錄,有的在子目錄中,這個宏用來指定子目錄,相鄰的子目錄用空格分開。SUBDIRS裏指定的每一個子目錄中都必需要有Makefile.am(相應地在configure.ac裏也要把子目錄中的Makefile文件名加入到AC_OUTPUT中)。要注意一點是bin_PROGRAMS定義的是一個可執行文件,也就是說後面的依賴文件列表中必須有一個是有main函數的。
   noinst_LIBRARIES=lib.a:要是咱們的子目錄中的代碼不是一個獨立的程序(沒有main函數),只是拿來給其餘目錄的程序#include的怎麼辦?要用noinst_LIBRARIES來將它定義爲庫。以後和定義了bin_PROGRAMS同樣定義XXX_SOURCES,例如根據上面noinst_LIBRARIES所定義的,咱們能夠這樣來定義:lib_a_SOURCES=<文件列表>。
注意「.」要改爲「_」,即lib.a在做爲XXX_SOURCES的頭部時變成了lib_a。  
   LDADD=sub/lib.a:最後哪裏引用了這些代碼,要加入相應地語句。比方說,咱們的子目錄名字爲sub,在它的上層目錄要引用到這些代碼,那麼要在它的上層目錄中的Makefile.am中要加入這麼一句來指定調用了sub子目錄下的lib.a庫(其實咱們是把sub子目錄下的代碼編譯成了一個庫,庫文件爲lib.a,LDADD就是指定要調用哪一個庫)。注意別忘了使用庫時,要給configure.ac中加入AC_PROG_RANLIB。
   總結一下Makefile.am是怎麼放的:
   1. 根目錄(configure.ac所在的目錄)必須有一個Makefile.am
   2. 全部有須要編譯的代碼文件的目錄下必須有一個Makefile.am
   3. 若是一個目錄中有Makefile.am,那麼必須在它的父目錄中的Makefile.am裏用SUBDIRS指定它。
   如今運行automake,因爲使用了GNU構建等級,故咱們先要本身建立NEWS、README、AUTHORS、ChangeLog文件,而後用帶--add-missing選項的automake命令,操做以下:this

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  2. aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure  configure.ac  main.c  Makefile.am  mytool1.c  mytool1.h  mytool2.c  mytool2.h  
  3. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ touch NEWS README AUTHORS ChangeLog  
  4. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  5. aclocal.m4  autom4te.cache  ChangeLog    configure     main.c       mytool1.c  mytool2.c  NEWS  
  6. AUTHORS     autoscan.log    config.h.in  configure.ac  Makefile.am  mytool1.h  mytool2.h  README  
  7. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ automake --add-missing  
  8. configure.ac: installing `./install-sh'  
  9. configure.ac: installing `./missing'  
  10. Makefile.am: installing `./INSTALL'  
  11. Makefile.am: installing `./COPYING'  
  12. Makefile.am: installing `./depcomp'  
  13. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  14. aclocal.m4  autom4te.cache  ChangeLog    configure     COPYING  INSTALL     main.c       Makefile.in  mytool1.c  mytool2.c  NEWS  
  15. AUTHORS     autoscan.log    config.h.in  configure.ac  depcomp  install-sh  Makefile.am  missing      mytool1.h  mytool2.h  README  

 

   可見自動生成的另外5個GNU規範文件install-sh、missing、INSTALL、COPYING、depcomp。因爲沒有調用libtoolize,故沒有產生config.guess、config.sub、ltmain.sh等文件。
   (6)運行./configure,根據Makefile.in和config.h.in生成config.status、Makefile、config.h、config.log文件,完成Makefile的製做。操做以下:

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ./configure  
  2. checking for a BSD-compatible install... /usr/bin/install -c  
  3. checking whether build environment is sane... yes  
  4. checking for gawk... no  
  5. checking for mawk... mawk  
  6. checking whether make sets $(MAKE)... yes  
  7. checking for gcc... gcc  
  8. checking for C compiler default output file name... a.out  
  9. checking whether the C compiler works... yes  
  10. checking whether we are cross compiling... no  
  11. checking for suffix of executables...   
  12. checking for suffix of object files... o  
  13. checking whether we are using the GNU C compiler... yes  
  14. checking whether gcc accepts -g... yes  
  15. checking for gcc option to accept ISO C89... none needed  
  16. checking for style of include used by make... GNU  
  17. checking dependency style of gcc... gcc3  
  18. configure: creating ./config.status  
  19. config.status: creating Makefile  
  20. config.status: creating config.h  
  21. config.status: executing depfiles commands  
  22. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  23. aclocal.m4      autoscan.log  config.h.in    configure     depcomp     main.c       Makefile.in  mytool1.h  NEWS  
  24. AUTHORS         ChangeLog     config.log     configure.ac  INSTALL     Makefile     missing      mytool2.c  README  
  25. autom4te.cache  config.h      config.status  COPYING       install-sh  Makefile.am  mytool1.c    mytool2.h  stamp-h1  

 

   可見configure腳本會收集系統的信息,建立config.status、Makefile、config.h、stamp-h1等文件。
   (7)運行make,編譯所有源代碼文件。操做以下:

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ make  
  2. make  all-am  
  3. make[1]: Entering directory `/home/zhouhuansheng/zhou/linux_programming/9/auto'  
  4. if gcc -DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -MT main.o -MD -MP -MF ".deps/main.Tpo" -c -o main.o main.c; /  
  5.         then mv -f ".deps/main.Tpo" ".deps/main.Po"; else rm -f ".deps/main.Tpo"; exit 1; fi  
  6. if gcc -DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -MT mytool1.o -MD -MP -MF ".deps/mytool1.Tpo" -c -o mytool1.o mytool1.c; /  
  7.         then mv -f ".deps/mytool1.Tpo" ".deps/mytool1.Po"; else rm -f ".deps/mytool1.Tpo"; exit 1; fi  
  8. gcc  -g -O2   -o mytool  main.o mytool1.o mytool2.o    
  9. make[1]: Leaving directory `/home/zhouhuansheng/zhou/linux_programming/9/auto'  
  10. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  11. aclocal.m4      autoscan.log  config.h.in    configure     depcomp     main.c    Makefile.am  mytool     mytool1.o  mytool2.o  stamp-h1  
  12. AUTHORS         ChangeLog     config.log     configure.ac  INSTALL     main.o    Makefile.in  mytool1.c  mytool2.c  NEWS  
  13. autom4te.cache  config.h      config.status  COPYING       install-sh  Makefile  missing      mytool1.h  mytool2.h  README  

 

   可見編譯成功,生成了各個.o目標文件和最終的可執行程序mytool。
   (8)運行sudo make install,將程序安裝到系統目錄中,make uninstall卸載程序。以下:

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ sudo make install  
  2. [sudo] password for zhouhuansheng:   
  3. make[1]: Entering directory `/home/zhouhuansheng/zhou/linux_programming/9/auto'  
  4. test -z "/usr/local/bin" || mkdir -p -- "/usr/local/bin"  
  5.   /usr/bin/install -c 'mytool' '/usr/local/bin/mytool'  
  6. make[1]: Nothing to be done for `install-data-am'.  
  7. make[1]: Leaving directory `/home/zhouhuansheng/zhou/linux_programming/9/auto'  
  8. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls -l /usr/local/bin/mytool  
  9. -rwxr-xr-x 1 root root 10483 2009-07-18 23:45 /usr/local/bin/mytool  
  10. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ sudo make uninstall  
  11.  rm -f '/usr/local/bin/mytool'  
  12. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls -l /usr/local/bin/mytool  
  13. ls: cannot access /usr/local/bin/mytool: No such file or directory  

 

   可見程序mytool安裝成功,被安裝在/usr/local/bin目錄下,而後被卸載。
   (9)運行make dist,將程序打包爲.tar.gz包,以便發佈。以下:

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ make dist  
  2. { test ! -d mytool-0.1 || { find mytool-0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr mytool-0.1; }; }  
  3. mkdir mytool-0.1  
  4. find mytool-0.1 -type d ! -perm -777 -exec chmod a+rwx {} /; -o /  
  5.       ! -type d ! -perm -444 -links 1 -exec chmod a+r {} /; -o /  
  6.       ! -type d ! -perm -400 -exec chmod a+r {} /; -o /  
  7.       ! -type d ! -perm -444 -exec /bin/bash /home/zhouhuansheng/zhou/linux_programming/9/auto/install-sh -c -m a+r {} {} /; /  
  8.     || chmod -R a+r mytool-0.1  
  9. tardir=mytool-0.1 && /bin/bash /home/zhouhuansheng/zhou/linux_programming/9/auto/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >mytool-0.1.tar.gz  
  10. { test ! -d mytool-0.1 || { find mytool-0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr mytool-0.1; }; }  
  11. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  12. aclocal.m4      autoscan.log  config.h.in    configure     depcomp     main.c    Makefile.am  mytool             mytool1.h  mytool2.h  README  
  13. AUTHORS         ChangeLog     config.log     configure.ac  INSTALL     main.o    Makefile.in  mytool-0.1.tar.gz  mytool1.o  mytool2.o  stamp-h1  
  14. autom4te.cache  config.h      config.status  COPYING       install-sh  Makefile  missing      mytool1.c          mytool2.c  NEWS  

 

   可見程序被打包爲mytool-0.1.tar.gz,格式爲「包名-版本號.tar.gz」。 
   (10)運行make clean清除以前編譯目標文件、可執行文件及配置文件,make distclean則清除全部生成的文件。以下:

 

[python]  view plain copy
 
  1. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ make clean  
  2. test -z "mytool" || rm -f mytool  
  3. rm -f *.o  
  4. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  5. aclocal.m4      autoscan.log  config.h.in    configure     depcomp     main.c       Makefile.in        mytool1.c  mytool2.h  stamp-h1  
  6. AUTHORS         ChangeLog     config.log     configure.ac  INSTALL     Makefile     missing            mytool1.h  NEWS  
  7. autom4te.cache  config.h      config.status  COPYING       install-sh  Makefile.am  mytool-0.1.tar.gz  mytool2.c  README  
  8. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ make distclean  
  9. test -z "mytool" || rm -f mytool  
  10. rm -f *.o  
  11. rm -f *.tab.c  
  12. test -z "" || rm -f   
  13. rm -f config.h stamp-h1  
  14. rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags  
  15. rm -f config.status config.cache config.log configure.lineno configure.status.lineno  
  16. rm -rf ./.deps  
  17. rm -f Makefile  
  18. zhouhuansheng@laptop-zhou:~/zhou/linux_programming/9/auto$ ls  
  19. aclocal.m4  autom4te.cache  ChangeLog    configure     COPYING  INSTALL     main.c       Makefile.in  mytool-0.1.tar.gz  mytool1.h  mytool2.h  README  
  20. AUTHORS     autoscan.log    config.h.in  configure.ac  depcomp  install-sh  Makefile.am  missing      mytool1.c          mytool2.c  NEWS  

 

   可見make clean清除以前編譯目標文件、可執行文件及配置文件,make distclean還清除了config.status、Makefile、config.h等文件,回到./configure以前的狀態。

   一切大功告成。由上面的敘述不難看出,autotools確實是軟件維護與發佈的便捷工具,也鑑於此,現在GUN的軟件通常都是由automake來製做的。
   總結(項目的整個構建過程): autoscan、修改configure.scan並重命令爲configure.ac、aclocal、autoheader、autoconf、編寫Makefile.am並運行automake(-a)、./configure、make、make install/uninstall、make dist、make clean/distclean

 

 

轉自 http://blog.csdn.net/zhoudaxia/article/details/4361194

相關文章
相關標籤/搜索