Linux下autoTools工具集使用介紹__Linux

來源:互聯網
上載者:User

Author : iStone
E-mail : liul.stone@gmail.com
Date : 2015-09-19 15:16:38 一 使用autoTools工具集 1.1 什麼是autoTools

我們都瞭解make工程管理器的強大功能。但編寫makefile 確實不是一件輕鬆的事,尤其對於一個較大的項目而言更是如此。那麼,有沒有一種輕鬆的手段產生makefile而同時又能讓使用者享受make 的優越性呢。本節要講的autoTools系列工具正是為此而設的,它只需使用者輸入簡單的目標檔案、依賴檔案、檔案目錄等就可以輕鬆地產生makefile了。另外,這些工具還可以完成系統配置資訊的收集,從而可以方便地處理各種移植性的問題。也正是基於此,現在Linux上的軟體開發一般都用autoTools 來產生makefile。 1.2 autoTools使用流程

正如上文所言,autoTools 是系列工具,它包含了aclocal、autoscan、autoconf、autoheader、和automake這些工具,使用autoTools主要就是利用各個工具的指令檔來產生最後的makefile檔案。其總體流程如下:
圖 1-1 autoTools產生makefile流程圖

接下來用/*sayHello.c*/來做為執行個體: 0、安裝autoTools

安裝autoTools工具集,Centos下可以使用 yum install autoconf automake 線上安裝。 1、建立目錄

建立一個TestautoTools的目錄,這個目錄將作為存放sayHello程式及其相關檔案的地方。 2、建立源碼檔案

用編輯器在TestautoTools目錄下建立sayHello.c源檔案和sayHello.h標頭檔,內容如下:

[root@localhost TestautoTools]# cat sayHello.c#include <stdio.h>#include "sayHello.h"int main(){    printf("Program say : %s\n" ,STR);    return 0;}[root@localhost TestautoTools]# cat sayHello.h#ifndef SAYHELLO_H#define SAYHELLO_H#define STR "hello AutoTools"#endif[root@localhost TestautoTools]# lssayHello.c  sayHello.h

接下來就要用autoTools為我們產生makefile文檔: 3、autoscan

[root@localhost TestautoTools]# autoscan[root@localhost TestautoTools]# lsautoscan.log  configure.scan  sayHello.c  sayHello.h[root@localhost TestautoTools]#

它會在給定目錄及其子目錄樹中檢查源檔案,預設是在目前的目錄及其子目錄樹中進行檢查。執行autoscan後會產生一個configure.scan的檔案,我們可以用它做為configure.in文檔的藍本。但由上述結果可知autoscan會首先去讀入configure.ac檔案,但此時還沒有建立該設定檔,於是它就自動產生了一個configure.scan檔案。

[root@localhost TestautoTools]# lsautoscan.log  configure.scan  sayHello.c  sayHello.h
4、autoconf

configure.in是autoconf的指令碼設定檔,它的原型檔案configure.scan改名為configure.in並把內容更改為如下所示:

# -*- Autoconf -*-                    //以“#”號開始的行為注釋。AC_PREREQ(2.59)                         //本檔案要求的autoconf版本。AC_INIT(sayHello,1.0,liul.stone@gmail.com)                    // AC_INIT宏用來定義軟體的名稱和版本等資訊。AM_INIT_AUTOMAKE(sayHello,1.0)          //是automake所必備的宏,軟體名稱和版本號碼。AC_CONFIG_SRCDIR([sayHello.c])    //用來偵測所指定的源碼檔案是否存在。AC_CONFIG_HEADER([config.h])     //用於產生config.h檔案,以便autoheader 使用。AC_PROG_CCAC_CONFIG_FILES([Makefile])          //用於產生相應的Makefile 檔案。AC_OUTPUT

接下來運行aclocal,產生一個“aclocal.m4”檔案,該檔案是處理本地的宏定義。

[root@localhost hello]# aclocal

再接著運行autoconf,產生configure可執行檔。

[root@localhost hello]# autoconf[root@localhost hello]# lsaclocal.m4  autom4te.cache  autoscan.log  configure  configure.in  sayHello.c  sayHello.h
5、autoheader

使用autoheader命令是為了成config.h.in檔案。該工具通常會從acconfig.h檔案中複製使用者定義的符號,若此處沒有自訂符號就不需建立acconfig.h檔案。

[root@localhost TestautoTools]# autoheader[root@localhost TestautoTools]# lsaclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure  configure.in  sayHello.c  sayHello.h[root@localhost TestautoTools]#
6、automake

這一步是建立makefile很重要的一步,automake用的指令碼設定檔是Makefile.am,需要自己建立相應的檔案。再用automake工具將其轉成Makefile.in檔案。內容如下所示:

[root@localhost TestautoTools]# cat Makefile.amAUTOMAKE_OPTIONS=foreignbin_PROGRAMS= sayHellosayHello_SOURCES= sayHello.c sayHello.h[root@localhost TestautoTools]#

AUTOMAKE_OPTIONS:設定automake的選項。由於GNU對自己發布的軟體有嚴格的規範,如必須附帶許可證聲明檔案COPYING等,否則automake執行時會報錯。automake提供了3 種軟體等級:foreign、gnu、gnits,我們這裡用foreign只檢測必要檔案。
bin_PROGRAMS:定義要產生的可執行檔名。如果要產生多個執行檔案用空格隔開。
sayHello_SOURCES:定義sayHello這個可執行檔所需要的源檔案。如sayHello這個程式是由多個原始檔案所產生的,則須把它所用到的所有源檔案都列出來,並用空格隔開。例如定義:sayHello _SOURCES=sayHello.c sayHello.h。注意:如果要定義多個執行檔案,則對每個執行檔案都要定義相應的_SOURCES。

接下來使用automake產生configure.in檔案,在這裡使用選項—adding-missing可以讓automake自動添加有一些必需的指令檔。如下所示:

[root@localhost TestautoTools]# automake --add-missingconfigure.in:6: installing `./install-sh'configure.in:6: installing `./missing'Makefile.am: installing `./depcomp'[root@localhost TestautoTools]# lsaclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure  configure.in  depcomp  install-sh  Makefile.am  Makefile.in  missing  sayHello.c  sayHello.h[root@localhost TestautoTools]#
7、configure

通過運行自動設定設定檔案configure,把Makefile.in變成了最終的Makefile。

[root@localhost TestautoTools]# ./configurechecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables...checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3configure: creating ./config.statusconfig.status: creating Makefileconfig.status: creating config.hconfig.status: executing depfiles commands[root@localhost TestautoTools]#[root@localhost TestautoTools]# lsaclocal.m4      autoscan.log  config.h.in  config.status  configure.in  install-sh  Makefile.am  missing     sayHello.hautom4te.cache  config.h      config.log   configure      depcomp       Makefile    Makefile.in  sayHello.c  stamp-h1[root@localhost TestautoTools]#

到此步makefile就已經自動產生了。 1.3 使用make命令

產生了makefile後我們就要使用make工程管理器去實現makefile的制定。 1、make

make預設執行make all命令,其執行情況如下所示:

[root@localhost TestautoTools]# makemake  all-ammake[1]: Entering directory `/home/liul/work_dir/TestautoTools'gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT sayHello.o -MD -MP -MF .deps/sayHello.Tpo -c -o sayHello.o sayHello.cmv -f .deps/sayHello.Tpo .deps/sayHello.Pogcc  -g -O2   -o sayHello sayHello.o make[1]: Leaving directory `/home/liul/work_dir/TestautoTools'[root@localhost TestautoTools]#

如果之前makefile操作無誤,那麼執行make命令會出現上面這段執行內容,而且會在目錄下產生hello可執行檔,運行sayHello程式:

[root@localhost TestautoTools]# ./sayHelloProgram say : hello AutoTools[root@localhost TestautoTools]#
2、make install

把sayHello程式安裝到系統目錄中去:

[root@localhost TestautoTools]# make installmake[1]: Entering directory `/home/liul/work_dir/TestautoTools'test -z "/usr/local/bin" || /bin/mkdir -p "/usr/local/bin"  /usr/bin/install -c sayHello '/usr/local/bin'make[1]: Nothing to be done for `install-data-am'.make[1]: Leaving directory `/home/liul/work_dir/TestautoTools'[root@localhost TestautoTools]#

此時,若直接運行sayHello能出現如下結果就說明操作正確:

[root@localhost TestautoTools]# sayHelloProgram say : hello AutoTools[root@localhost TestautoTools]#
3、make clean

此命令是清除之前所編譯的可執行檔及目標檔案(object file, *.o):

[root@localhost TestautoTools]# make cleantest -z "sayHello" || rm -f sayHellorm -f *.o[root@localhost TestautoTools]#
4、make dist

當我們做好了一切工作,最後一步是壓縮文檔以供發布:

[root@localhost TestautoTools]# make distcd . && /bin/sh /root/link2workdir/TestautoTools/missing --run automake-1.11 --foreign Makefilecd . && /bin/sh ./config.status Makefile depfilesconfig.status: creating Makefileconfig.status: executing depfiles commands{ test ! -d "sayHello-1.0" || { find "sayHello-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "sayHello-1.0"; }; }test -d "sayHello-1.0" || mkdir "sayHello-1.0"test -n "" \        || find "sayHello-1.0" -type d ! -perm -755 \                -exec chmod u+rwx,go+rx {} \; -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 /root/link2workdir/TestautoTools/install-sh -c -m a+r {} {} \; \        || chmod -R a+r "sayHello-1.0"tardir=sayHello-1.0 && /bin/sh /root/link2workdir/TestautoTools/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >sayHello-1.0.tar.gz{ test ! -d "sayHello-1.0" || { find "sayHello-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "sayHello-1.0"; }; }[root@localhost TestautoTools]#[root@localhost TestautoTools]# lsaclocal.m4      autoscan.log  config.h.in  config.status  configure.in  install-sh  Makefile.am  missing              sayHello.c  stamp-h1autom4te.cache  config.h      config.log   configure      depcomp       Makefile    Makefile.in  sayHello-1.0.tar.gz  sayHello.h[root@localhost TestautoTools]#[root@localhost TestautoTools]#
5、總結

sayHello-1.0.tar.gz就是我們最終要發布的壓縮檔。autoTools的整個流程都介紹完畢,現實中更多會用到autoTools來為我們產生makefile文檔。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.