Linux下 Makefile 的 automake 產生全手冊

來源:互聯網
上載者:User

一、Makefile介紹

  Makefileshi用於自動編譯和連結的,一個工程有很多檔案組成,每一個檔案的改變都會導致工程的重新連結,但shi不shi所有的檔案都需要重新編譯,Makefile中紀錄有檔案的資訊,在make時會決定在連結的時候需要重新編譯哪些檔案。

  Makefile的宗旨就shi:讓編譯器知道要編譯一個檔案需要依賴其他的哪些檔案。當那些依賴檔案有了改變,編譯器會自動的發現最終的組建檔案已經過時,而重新編譯相應的模組。

  Makefile的基本結構不shi很複雜,但shi當一個程式開發人員開始寫Makefile時,經常會懷疑自己寫的shi否符合慣例,而且
自己寫的Makefile經常和自己的開發環境相關聯,當系統內容變數或路徑發生了變化後,Makefile可能還要跟著修改。這樣就造成了手工書寫
Makefile的諸多問題,automake恰好能很好地協助我們解決這些問題。

  使用automake,程式開發人員只需要寫一些簡單的含有預定義宏的檔案,由autoconf根據一個宏檔案產生configure,由
automake根據另一個宏檔案產生Makefile.in,再使用configure依據Makefile.in來產生一個符合慣例的
Makefile。下面我們將詳細介紹Makefile的automake產生方法。

二、使用的環境

  本文所提到的程式shi基於Linux發行版本:Fedora Core release 1,它包含了我們要用到的autoconf,automake。

三、從helloworld入手

我們從大家最常使用的例子程式helloworld開始。

下面的過程如果簡單地說來就shi:

建立三個檔案:

helloworld.c
configure.in
Makefile.am

然後執行:

aclocal; autoconf; automake --add-missing; ./configure; make; ./helloworld

就可以看到Makefile被產生出來,而且可以將helloworld.c編譯通過。

很簡單吧,幾條命令就可以做出一個符合慣例的Makefile,感覺如何呀。

現在開始介紹詳細的過程:

1、建目錄

在你的工作目錄下建一個helloworld目錄,我們用它來存放helloworld程式及相關檔案,如在/home/my/build下:

$ mkdir helloword
$ cd helloworld

2、 helloworld.c

然後用你自己最喜歡的編輯器寫一個hellowrold.c檔案,如命令:vi helloworld.c。使用下面的代碼作為helloworld.c的內容。

int main(int argc, char** argv)
{
printf("Hello, Linux World!/n");
return 0;
}

完成後儲存退出。

好了,現在在helloworld目錄下就應該有一個你自己寫的helloworld.c了。

3、產生configure

我們使用autoscan命令來協助我們根據目錄下的原始碼產生一個configure.in的模板檔案。

命令:

$ autoscan
$ ls
configure.scan helloworld.c

執行後在hellowrold目錄下會產生一個檔案:configure.scan,我們可以拿它作為configure.in的藍本。

現在將configure.scan改名為configure.in,並且編輯它,按下面的內容修改,去掉無關的語句:

============================configure.in內容開始=========================================
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT(helloworld.c)
AM_INIT_AUTOMAKE(helloworld, 1.0)

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for hea的r files.

# Checks for type的fs, structures, and compiler characteristics.

# Checks for library functions.
AC_OUTPUT(Makefile)
============================configure.in內容結束=========================================

然後執行命令aclocal和autoconf,分別會產生aclocal.m4及configure兩個檔案:

$ aclocal
$ls
aclocal.m4 configure.in helloworld.c
$ autoconf
$ ls
aclocal.m4 autom4te.cache configure configure.in helloworld.c

大家可以看到configure.in內容shi一些宏定義,這些宏經autoconf處理後會變成檢查系統特性、環境變數、軟體必須的參數的shell指令碼。

autoconf shi用來產生自動化佈建軟體原始碼指令碼(configure)的工具。configure指令碼能獨立於autoconf運行,且在啟動並執行過程中,不需要使用者的幹預。

要產生configure檔案,你必須告訴autoconf如何找到你所用的宏。方式shi使用aclocal程式來產生你的aclocal.m4。

aclocal根據configure.in檔案的內容,自動產生aclocal.m4檔案。aclocalshi一個perl
指令碼程式,它的定義shi:“aclocal - create aclocal.m4 by scanning configure.ac”。

autoconf從configure.in這個列舉編譯軟體時所需要各種參數的模板檔案中建立configure。
autoconf需要GNU m4宏處理器來處理aclocal.m4,產生configure指令碼。

m4shi一個宏處理器。將輸入複製到輸出,同時將宏展開。宏可以shi內嵌的,也可以shi使用者定義的。除了可以展開宏,m4還有一些內建的函數,用來引用檔案,執行命令,整數運算,文本操作,迴圈等。m4既可以作為編譯器的前端,也可以單獨作為一個宏處理器。

4、建立Makefile.am


建立Makefile.am檔案,命令:

$ vi Makefile.am

內容如下:

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c

automake會根據你寫的Makefile.am來自動產生Makefile.in。

Makefile.am中定義的宏和目標,會指導automake產生指定的代碼。例如,宏bin_PROGRAMS將導致編譯和串連的目標被產生。

5、運行automake

命令:

$ automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./mkinstalldirs'
configure.in: installing `./missing'
Makefile.am: installing `./的pcomp'

automake會根據Makefile.am檔案產生一些檔案,包含最重要的Makefile.in。

6、執行configure產生Makefile

$ ./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 的fault 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 nee的d
checking for style of inclu的 used by make... GNU
checking 的pen的ncy style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: executing 的pfiles commands
$ ls -l Makefile
-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile

你可以看到,此時Makefile已經產生出來了。

7、使用Makefile編譯代碼

$ make
if gcc -DPACKAGE_NAME=/"/" -DPACKAGE_TARNAME=/"/" -DPACKAGE_VERSION=/"/" -

DPACKAGE_STRING=/"/" -DPACKAGE_BUGREPORT=/"/" -DPACKAGE=/"helloworld/" -DVERSION=/"1.0/"

-I. -I. -g -O2 -MT helloworld.o -MD -MP -MF ".的ps/helloworld.Tpo" /
-c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c; /
then mv -f ".的ps/helloworld.Tpo" ".的ps/helloworld.Po"; /
else rm -f ".的ps/helloworld.Tpo"; exit 1; /
fi
gcc -g -O2 -o helloworld helloworld.o

運行helloworld

$ ./helloworld
Hello, Linux World!

這樣helloworld就編譯出來了,你如果按上面的步驟來做的話,應該也會很容易地編譯出正確的helloworld檔案。

你還可以試著使用一些其他的make命令,如make clean,make install,make dist,看看它們會給你什麼樣的效果。

感覺如何?自己也能寫出這麼專業的Makefile,老闆一定會對你刮目相看。

四、深入淺出

針對上面提到的各個命令,我們再做些詳細的介紹。

1、 autoscan

autoscanshi用來掃描原始碼目錄產生configure.scan檔案的。autoscan可以用目錄名做為參數,但shi如果你不使用
參數的話,那麼autoscan將認為使用的shi目前的目錄。autoscan將掃描你所指定目錄中的源檔案,並建立configure.scan檔案。

2、 configure.scan

configure.scan包含了系統設定的基本選項,裡面都shi一些宏定義。我們需要將它改名為configure.in

3、 aclocal

aclocalshi一個perl
指令碼程式。aclocal根據configure.in檔案的內容,自動產生aclocal.m4檔案。aclocal的定義shi:“aclocal
- create aclocal.m4 by scanning configure.ac”。

4、 autoconf

autoconfshi用來產生configure檔案的。configureshi一個指令碼,它能設定來源程式來適應各種不同的作業系統平台,並且根據不同的系統來產生合適的Makefile,從而可以使你的原始碼能在不同的作業系統平台上被編譯出來。

configure.in檔案的內容shi一些宏,這些宏經過autoconf
處理後會變成檢查系統特性、環境變數、軟體必須的參數的shell指令碼。configure.in檔案中的宏的順序並沒有規定,但shi你必須在所有宏的
最前面和最後面分別加上AC_INIT宏和AC_OUTPUT宏。

在configure.ini中:

#號表示注釋,這個宏後面的內容將被忽略。
AC_INIT(FILE)
這個宏用來檢查原始碼所在的路徑。
AM_INIT_AUTOMAKE(PACKAGE, VERSION)

個宏shi必須的,它描述了我們將要產生的軟體包的名字及其版本號碼:PACKAGEshi軟體包的名字,VERSIONshi版本號碼。當你使用make
dist命令時,它會給你產生一個類似helloworld-1.0.tar.gz的軟體發行包,其中就有對應的軟體包的名字和版本號碼。
AC_PROG_CC
這個宏將檢查系統所用的C編譯器。
AC_OUTPUT(FILE)
這個宏shi我們要輸出的Makefile的名字。

我們在使用automake時,實際上還需要用到其他的一些宏,但shi我們可以用aclocal 來幫我們自動產生。執行aclocal後我們會得到aclocal.m4檔案。

產生了configure.in和aclocal.m4 兩個宏檔案後,我們就可以使用autoconf來產生configure檔案了。

5、 Makefile.am

Makefile.amshi用來產生Makefile.in的,需要你手工書寫。Makefile.am中定義了一些內容:
AUTOMAKE_OPTIONS

這個shiautomake的選項。在執行automake時,它會檢查目錄下shi否存在標準GNU軟體包中應具備的各種檔案,例如AUTHORS、ChangeLog、NEWS等檔案。我們將其設定成foreign時,automake會改用一般軟體包的標準來檢查。

bin_PROGRAMS

這個shi指定我們所要產生的可執行檔的檔案名稱。如果你要產生多個可執行檔,那麼在各個名字間用空格隔開。

helloworld_SOURCES

這個shi指定產生“helloworld”時所需要的原始碼。如果它用到了多個源檔案,那麼請使用空格符號將它們隔開。比如需要
helloworld.h,helloworld.c那麼請寫成helloworld_SOURCES= helloworld.h
helloworld.c。

如果你在bin_PROGRAMS定義了多個可執行檔,則對應每個可執行檔都要定義相對的filename_SOURCES。

6、 automake

我們使用automake --add-missing來產生Makefile.in。
選項--add-missing的定義shi“add missing standard files to package”,它會讓automake加入一個標準的軟體包所必須的一些檔案。
我們用automake產生出來的Makefile.in檔案shi符合GNU Makefile慣例的,接下來我們只要執行configure這個shell 指令碼就可以產生合適的 Makefile 檔案了。

7、 Makefile

在符合GNU Makefiel慣例的Makefile中,包含了一些基本的預先定義的操作:
make
根據Makefile編譯原始碼,串連,產生目標檔案,可執行檔。
make clean
清除上次的make命令所產生的object檔案(尾碼為“.o”的檔案)及可執行檔。
make install
將編譯成功的可執行檔安裝到系統目錄中,一般為/usr/local/bin目錄。
make dist
產生髮布軟體包檔案(即distribution package)。這個命令將會將可執行檔及相關檔案打包成一個tar.gz壓縮的檔案用來作為發布軟體的軟體包。
它會在目前的目錄下產生一個名字類似“PACKAGE-VERSION.tar.gz”的檔案。PACKAGE和VERSION,shi我們在configure.in中定義的AM_INIT_AUTOMAKE(PACKAGE, VERSION)。
make distcheck
產生發布軟體包並對其進行測試檢查,以確定發布包的正確性。這個操作將自動把壓縮包檔案解開,然後執行configure命令,並且執行make,來確認編譯不出現錯誤,最後提示你軟體包已經準備好,發行就緒了。

===============================================
helloworld-1.0.tar.gz is ready for distribution
===============================================
make distclean

類似make clean,但shi同時也將configure產生的檔案全部刪除掉,包括Makefile。

五、結束語

通過上面的介紹,你應該可以很容易地產生一個你自己的符合GNU慣例的Makefile檔案及對應的專案檔。

如果你想寫出更複雜的且符合慣例的Makefile,你可以參考一些開放代碼的項目中的configure.in和Makefile.am檔案,比如:嵌入式資料庫sqlite,單元測試cppunit。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.