自動生成 Makefile 的全過程詳解

automake/autoconf  入門
做爲 Linux  下的程序開發人員,你們必定都遇到過 Makefile  ,用 make  命令來編譯本身寫的程序確實是很方便。通常狀況下,你們都是手工寫一個簡單 Makefile  ,若是要想寫出一個符合自由軟件慣例的 Makefile  就不那麼容易了。

在本文中,將給你們介紹如何使用 autoconf  和 automake  兩個工具來幫助咱們自動地生成符合自由軟件慣例的 Makefile  ,這樣就能夠象常 見的 GNU  程序同樣,只要使用 「./configure」  , 「make」 「make instal」  就能夠把程序安裝到 Linux  系統中去了。這將特別適合想作開放源代碼軟件的程序開發人員,又或若是你只是本身寫些小的 Toy  程序,那麼這 個文章對你也會有很大的幫助。

1、 Makefile  介紹

Makefile
 是用於自動編譯和連接的,一個工程有不少文件組成,每個文件的改變都會致使工程的從新連接,可是不是全部的文件都須要從新編譯, Makefile  中紀錄有文件的信息,在 make  時會決定在連接的時候須要從新編譯哪些文件。

Makefile
 的宗旨就是:讓編譯器知道要編譯一個文件須要依賴其餘的哪些文件。當那些依賴文件有了改變,編譯器會自動的發現最終的生成文件已通過時,而從新編譯相應的模塊。

Makefile
 的基本結構不是很複雜,但當一個程序開發人員開始寫 Makefile  時,常常會懷疑本身寫的是否符合慣例,並且本身寫的  Makefile  常常和本身的開發環境相關聯,當系統環境變量或路徑發生了變化後, Makefile  可能還要跟着修改。這樣就形成了手工書寫  Makefile  的諸多問題, automake  剛好能很好地幫助咱們解決這些問題。

使用 automake  ,程序開發人員只須要寫一些 簡單的含有預約義宏的文件,由 autoconf  根據一個宏文件生成 configure  ,由 automake  根據另外一個宏文件生成 Makefile.in  , 再使用 configure  依據 Makefile.in  來生成一個符合慣例的 Makefile  。下面咱們將詳細介紹 Makefile  的 automake  生成 方法。

2、使用的環境

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

3、從 helloworld  入手

咱們從你們最常使用的例子程序 helloworld  開始。

下面的過程若是簡單地說來就是:

新建三個文件:

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 header files.

# Checks for typedefs, 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  內容是一些宏定義,這些宏經 autoconf  處理後會變成檢查系統特性、環境變量、軟件必須的參數的 shell  腳本。

autoconf 
是用來生成自動配置軟件源代碼腳本( configure  )的工具。 configure  腳本能獨立於 autoconf  運行,且在運行的過程當中,不須要用戶的干預。

要生成 configure  文件,你必須告訴 autoconf  如何找到你所用的宏。方式是使用 aclocal  程序來生成你的 aclocal.m4  。

aclocal
 根據 configure.in  文件的內容,自動生成 aclocal.m4  文件。 aclocal  是一個 perl  腳本程序,它的定義是: 「aclocal - create aclocal.m4 by scanning configure.ac」  。

autoconf
 從 configure.in  這個列舉編譯軟件時所須要各類參數的模板文件中建立 configure  。

autoconf
 須要 GNU m4  宏處理器來處理 aclocal.m4  ,生成 configure  腳本。

m4
 是一個宏處理器。將輸入拷貝到輸出,同時將宏展開。宏能夠是內嵌的,也能夠是用戶定義的。除了能夠展開宏, 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 `./depcomp'


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 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: executing depfiles commands
$ ls -l Makefile
-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile


你能夠看到,此時 Makefile  已經產生出來了。
相關文章
相關標籤/搜索