自動生成 Makefile 的全過程詳解

automake/autoconf 入門
作爲Linux 下的程序開發人員,大家一定都遇到過Makefile ,用make 命令來編譯自己寫的程序確實是很方便。一般情況下,大家都是手工寫一個簡單Makefile ,如果要想寫出一個符合自由軟件慣例的Makefile 就不那麼容易了。

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

一、Makefile 介紹

Makefile
 是用於自動編譯和鏈接的,一個工程有很多文件組成,每一個文件的改變都會導致工程的重新鏈接,但是不是所有的文件都需要重新編譯,Makefile 中紀錄有文件的信息,在make 時會決定在鏈接的時候需要重新編譯哪些文件。

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

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

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

二、使用的環境

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

三、從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 已經產生出來了。


轉載自:http://www.linuxsky.org/doc/dev/201102/423.html

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