小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

[轉載]使用Automake,Autoconf生成Makefile

 todaytomo 2007-01-17
使用Automake,Autoconf生成Makefile
在Unix上寫過程序的人尤其是用 C 來開發(fā)程序的人一般都遇到過 Makefile,用 make 來開發(fā)和編譯程序的確很方便,可是要寫出一個Makefile就不那么簡單了。GNU Make 那份幾百頁的文件,讓許多人害怕。當然,現(xiàn)在關于make的文檔比較多,不過寫一個Makefile總是一件很煩人的事情,GNU Autoconf 及 Automake 這兩個軟件就是幫助程序開發(fā)者輕松產生Makefile 文件的?,F(xiàn)在的GNU軟件如Apache, MySQL Minigui等都是利用Autoconf,Automake實現(xiàn)自動編譯的。用戶只要使用 “./configure”, “make”, “make install” 就可以把程序安裝到系統(tǒng)中。

簡介
Makefile 基本上就是『目標』(target), 『關聯(lián)』(dependencies) 和『動作』三者所組成的一系列規(guī)則。而 make 就是根據 Makefile 的規(guī)則決定如何編譯 (compile) 和連接 (link) 程序或者其它動作。當然,make 可做的不只是編譯和連接程序,例如 FreeBSD 的 port collection 中,Makefile還可以做到自動下載遠程程序,解壓縮 (extract) , 打補丁 (patch),設定,然后編譯,安裝到系統(tǒng)中。

Makefile 基本結構雖然很簡單,但是妥善運用這些規(guī)則就可以變換出許多不同的花樣。卻也因為這樣,許多人剛開始學寫Makefile 時會覺得沒有規(guī)范可以遵循,每個人寫出來的Makefile都不大一樣,不知道從哪里下手,而且常常會受到開發(fā)環(huán)境的限制,只要環(huán)境參數不同或者路徑更改,可能 Makefile 就得跟著修改。雖然有GNU Makefile Conventions (GNU Makefile慣例)制訂出一些在進行 GNU 程序設計時寫 Makefile 的一些標準和規(guī)范,但是其內容很長而且很復雜,并且經常作一些調整,為了減輕程序開發(fā)人員維護Makefile 的負擔,就出現(xiàn)了Automake。

利用Automake,編程者只需要寫一些預先定義好的宏 (macro),提交給Automake處理,就會產生一個可以供 Autoconf 使用的 Makefile.in文件。再配合使用 Autoconf產生的自動配置文件 configure 即可產生一份符合 GNU Makefile 慣例的 Makeifle 了。 

需要的軟件
在開始使用 Automake 之前,首先確認你的系統(tǒng)安裝有如下軟件:

1. GNU Automake
2. GNU Autoconf
3. GNU m4
4. perl
5. GNU Libtool (如果你需要產生 shared library)

最好也使用 GNU C/C++ 編譯器 、GNU Make 以及其它 GNU 的工具程序來作為開發(fā)的環(huán)境,這些工具都是屬于 Open Source Software 不但免費而且功能強大。如果你是使用 Red Hat Linux 可以找到所有上述軟件的 rpm 文件。 

一個簡單的例子
Automake 所產生的 Makefile 除了可以做到程序的編譯和連接,也可以用來生成文檔(如 manual page, info 文件等),還可以有把源碼文件包裝起來以供發(fā)布,所以程序源代碼所存放的目錄結構最好符合GNU 的標準慣例,接下來就用一個hello.c 來做為例子。

在工作目錄下建立一個新的子目錄devel,再在 devel 下建立一個"hello"‘ 的子目錄,這個目錄將
作為存放 hello這個程序及其相關文件的地方:

% mkdir devel;cd devel;mkdir hello;cd hello

用編輯器寫一個hello.c文件,

#include <stdio.h>
int main(int argc, char** argv)
{
printf(“Hello, GNU!n”);
return 0;
}

接下來就要用 Autoconf 及 Automake 來產生 Makefile 文件了,

1. 用 autoscan 產生一個 configure.in 的原型,執(zhí)行autoscan 后會產生一個configure.scan 的文件,可以用它作為 configure.in文件的藍本。
 
% autoscan
% ls
configure.scan hello.c

2. 編輯 configure.scan文件,如下所示,並且改名為configure.in

dnl Process this file with Autoconf to produce a configure script.
AC_INIT(hello.c)
AM_INIT_AUTOMAKE(hello, 1.0)
dnl Checks for programs.
AC_PROG_CC
dnl Checks for libraries.
dnl Checks for header files.
dnl Checks for typedefs, structures, and compiler characteristics.
dnl Checks for library functions.
AC_OUTPUT(Makefile)

3. 執(zhí)行 aclocal 和 Autoconf ,分別會產生 aclocal.m4 及 configure 兩個文件

% aclocal
% Autoconf
% ls
aclocal.m4 configure configure.in hello.c

4. 編輯 Makefile.am 文件,內容如下

AUTOMAKE_OPTIONS= foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c

5. 執(zhí)行 Automake --add-missing ,Automake 會根據Makefile.am 文件產生一些文件,包含最重要的Makefile.in

% Automake --add-missing
Automake: configure.in: installing `./install-sh‘
Automake: configure.in: installing `./mkinstalldirs‘
Automake: configure.in: installing `./missing‘

6. 最后執(zhí)行 ./configure:

% ./configure
creating cache ./config.cache
checking for a BSD compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking whether make sets ${MAKE}... yes
checking for working aclocal... found
checking for working Autoconf... found
checking for working Automake... found
checking for working autoheader... found
checking for working makeinfo... found
checking for gcc... gcc
checking whether the C compiler (gcc ) works... yes
checking whether the C compiler (gcc ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
updating cache ./config.cache
creating ./config.status
creating Makefile

$ ls
Makefile aclocal.m4 config.status hello.c mkinstalldirs
Makefile.am config.cache configure install-sh
Makefile.in config.log configure.in missing

現(xiàn)在你的目錄下已經產生了一個 Makefile 文件,輸入make指令就可以編譯 hello.c 了!

% make
gcc -DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c hello.c
gcc -g -O2 -o hello hello.o

你還可以試試 “make clean“,”make install“,”make dist“:
[root@localhost hello]# make clean
test -z "hello " || rm -f hello
rm -f *.o core *.core
[root@localhost hello]# make install
gcc -DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c hello.c
gcc -g -O2 -o hello hello.o
make[1]: Entering directory `/home/joe/devel/hello‘
/bin/sh ./mkinstalldirs /usr/local/bin
/usr/bin/install -c hello /usr/local/bin/hello
make[1]: Nothing to be done for `install-data-am‘.
make[1]: Leaving directory `/home/joe/devel/hello‘
[root@localhost hello]# make dist
rm -rf hello-1.0
mkdir hello-1.0
chmod 777 hello-1.0
here=`cd . && pwd`;
top_distdir=`cd hello-1.0 && pwd`;
distdir=`cd hello-1.0 && pwd`;
cd .
&& Automake --include-deps --build-dir=$here --srcdir-name=. --output-dir=$top_distdir --foreign Makefile
chmod -R a+r hello-1.0
GZIP=--best gtar chozf hello-1.0.tar.gz hello-1.0
rm -rf hello-1.0
一切工作得很好! 當然,在make install時由于需要向系統(tǒng)目錄拷貝文件,您需要有root權限。

更進一步
上述產生Makefile 的過程和以往自行編寫的方式非常不一樣,使用 Automake 只需用到一些已經定義好的宏就可以了。我們把宏及目標 (target)寫在Makefile.am 文件內,Automake 讀入 Makefile.am 文件后會把這一串已經定義好的宏展開并產生相對應的
Makefile.in 文件,然后再由configure這個 shell script 根據 Makefile.in 產生合適的Makefile。
具體流程如下所示:
代碼 --> [autoscan*] --> [configure.scan] --> configure.in
configure.in --. .------> Autoconf* -----> configure
+---+
[aclocal.m4] --+ `---.
[acsite.m4] ---‘ |
+--> [autoheader*] -> [config.h.in]
[acconfig.h] ----. |
+-----‘
[config.h.top] --+
[config.h.bot] --‘

Makefile.am -- [Autoconf*] -------> Makefile.in

.-------------> config.cache
configure* ------------+-------------> config.log
|
[config.h.in] -. v .-> [config.h] -.
+--> config.status* -+ +--> make*
Makefile.in ---‘ `-> Makefile ---‘

上圖表示在整個過程中要使用的文件及產生出來的文件,有星號 (*) 代表可執(zhí)行文件。在此示例中可由 Autoconf 及 Automake 工具所產生的額外文件有 configure.scan、aclocal.m4、configure、Makefile.in,需要加入設置的有configure.in 及 Makefile.am。 開發(fā)者要書寫的文件集中為confiugre.in和Makefile.am,在minigui項目中,我們把一系列的命令集中到一個批處理文件中:autogen.sh:

#!/bin/sh
aclocal
autoheader
Automake --add-missing
Autoconf

只要執(zhí)行該批處理文件,結合configure.in和Makefile.am,就可以生成需要的Makefile了。

編輯 configure.in 文件
Autoconf 是用來產生 ‘configure‘文件的工具?!甤onfigure‘ 是一個 shell script,它可以自動設定一些編譯參數使程序能夠條件編譯以符合各種不同平臺的Unix 系統(tǒng)。Autoconf會讀取configure.in 文件然后產生‘configure‘ 這個 shell script。

configure.in 文件內容是一系列GNU m4 的宏,這些宏經Autoconf處理后會變成檢查系統(tǒng)特性的shell scripts。 configure.in文件中宏的順序并沒有特別的規(guī)定,但是每一個configure.in 文件必須在所有其它宏前加入 AC_INIT 宏,然后在所有其它宏的最后加上 AC_OUTPUT宏。一般可先用 autoscan 掃描原始文件以產生一個 configure.scan 文件,再對 configure.scan 做些修改成 configure.in 文件。在例子中所用到的宏如下:

dnl
這個宏后面的內容不會被處理,可以視為注釋

AC_INIT(FILE)
該宏用來檢查源代碼所在路徑,autoscan 會自動產生,一般無須修改它。

AM_INIT_AUTOMAKE(PACKAGE,VERSION)
這個是使用 Automake 所必備的宏,PACKAGE 是所要產生軟件的名稱,VERSION 是版本編號。

AC_PROG_CC
檢查系統(tǒng)可用的C編譯器,若源代碼是用C寫的就需要這個宏。

AC_OUTPUT(FILE)
設置 configure 所要產生的文件,若是Makefile ,configure 便會把它檢查出來的結果填充到Makefile.in 文件后產生合適的 Makefile。

實際上,在使用 Automake 時,還需要一些其他的宏,這些額外的宏我們用 aclocal來幫助產生。執(zhí)行 aclocal會產生aclocal.m4 文件,如果沒有特別的用途,不需要修改它,用 aclocal 所產生的宏會告訴 Automake如何動作。

有了 configure.in 及 aclocal.m4兩個文件以后,便可以執(zhí)行 Autoconf來產生 configure 文件了。

編輯Makefile.am 文件
接下來要編輯Makefile.am 文件,Automake 會根據 configure.in 中的宏并在perl的幫助下把Makefile.am 轉成 Makefile.in 文件。 Makefile.am 文件定義所要產生的目標:

AUTOMAKE_OPTIONS
設置 Automake 的選項。Automake 主要是幫助開發(fā) GNU 軟件的人員來維護軟件,所以在執(zhí)行Automake 時,會檢查目錄下是否存在標準 GNU 軟件中應具備的文件,例如 ‘NEWS‘、‘AUTHOR‘、
‘ChangeLog‘ 等文件。設置為foreign 時,Automake 會改用一般軟件的標準來檢查。

bin_PROGRAMS
定義要產生的執(zhí)行文件名。如果要產生多個執(zhí)行文件,每個文件名用空白符隔開。

hello_SOURCES
定義 ‘hello‘ 這個執(zhí)行程序所需要的原始文件。如果 ‘hello‘這個程序是由多個原始文件所產生,
必須把它所用到的所有原始文件都列出來,以空白符隔開。假設 ‘hello‘ 還需要 ‘hello.c‘、‘main.c‘、‘hello.h‘ 三個文件的話,則定義
hello_SOURCES= hello.c main.c hello.h
如果定義多個執(zhí)行文件,則對每個執(zhí)行程序都要定義相對的filename_SOURCES。

編輯好 Makefile.am 文件,就可以用 Automake --add-missing來產生 Makefile.in。加上 --add-missing 選項來告訴 Automake順便加入包裝一個軟件所必須的文件,如果你不使用該選項,Automake可能會抱怨缺少了什么文件。Automake產生出來的 Makefile.in 文件是完全符合 GNU Makefile 慣例的,只要執(zhí)行 configure這個shell
script 便可以產生合適的 Makefile 文件了。

使用 Makefile
利用 configure 所產生的 Makefile文件有幾個預先設定的目標可供使用,這里只用幾個簡述如下:

make all
產生設定的目標,既范例中的可執(zhí)行文件。只敲入make 也可以,此時會開始編譯源代碼,然后連接并產生執(zhí)行文件。

make clean
清除之前所編譯的可執(zhí)行文件及目標文件(object file, *.o)。

make distclean
除了清除可執(zhí)行文件和目標文件以外,也把 configure 所產生的 Makefile 清除掉。 通常在發(fā)布軟件前執(zhí)行該命令。

make install
將程序安裝到系統(tǒng)中,若源碼編譯成功,且執(zhí)行結果正確,便可以把程序安裝到系統(tǒng)預先設定的執(zhí)行文件存放路徑中,若用 bin_PROGRAMS 宏的話,程序會被安裝到 /usr/local/bin下。

make dist
將程序和相關的文檔包裝為一個壓縮文檔以供發(fā)布 (distribution) 。執(zhí)行完在目錄下會產生一個以
PACKAGE-VERSION.tar.gz 為名稱的文件。PACKAGE 和 VERSION 這兩個參數是根據 configure.in 文中
AM_INIT_AUTOMAKE(PACKAGE, VERSION) 的定義。在我們的例子中會產生 ‘hello-1.0.tar.gz‘ 的文件。

make distcheck
和 make dist 類似,但是加入檢查包裝以后的壓縮文件是否正常,這個目標除了把程序和相關文檔包裝成 tar.gz 文件外,還會自動把這個壓縮文件解開,執(zhí)行 configure,并執(zhí)行 make all ,確認編譯無錯誤以后,方顯示這個 tar.gz 文件已經準備好并可以發(fā)布了。當你看到:
==========================================
hello-1.0.tar.gz is ready for distribution
==========================================

就可以放心地發(fā)布您的軟件了,檢查過關的套件,基本上可以給任何具備 GNU 開發(fā)環(huán)境的人去重新編譯成功。
要注意的是,利用 Autoconf 及 Automake 所產生出來的軟件套件是可以在沒有安裝 Autoconf 及 Automake 的環(huán)境使用的,因為 configure 是一個 shell script,它己被設計為可以在一般 Unix 的 sh 這個 shell 下執(zhí)行。但是如果要修改 configure.in 及 Makefile.am 文件再產生新的 configure 及 Makefile.in 文件時就一定要有 Autoconf 及 Automake 了。

相關資料
通常我們掌握了一些入門知識就可以開始實踐了,在有新的需求時,參照相關的文檔和別人的例子解決問題,在實踐中不斷提高。
Autoconf 和 Automake 功能十分強大,可以從它們附帶的 info 文檔中找到詳細的使用說明?;蛘吣矚ghtml,可以從gun站點上下載hmtl版本。你也可以從許多現(xiàn)有的GNU 軟件或 Open Source 軟件如Minigui中找到相關的 configure.in 或 Makefile.am 文件,他們是學習 Autoconf 及 Automake 更多技巧的最佳范例。

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多