歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 自由軟件Makefile自動生成方法

自由軟件Makefile自動生成方法

日期:2017/3/1 11:15:28   编辑:Linux編程

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

在本文中,將給大家介紹如何使用autoconf和automake兩個工具來幫助我們自動地生成符合自由軟件慣例的Makefile,這樣就可以象常見的GNU程序一樣,只要使用“./configure”,“make”,“make instal”就可以把程序安裝到Linux系統中去了。

一、Makefile介紹

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

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

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

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

二、使用的環境

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,並且編輯它,
需要增加一個宏AM_INIT_AUTOMAKE(PACKAGE,VERSION)。
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AM_INIT_AUTOMAKE(mahjong, 0.10)


然後執行命令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已經產生出來了。

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 ".deps/helloworld.Tpo"
-c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c;
then mv -f ".deps/helloworld.Tpo" ".deps/helloworld.Po";
else rm -f ".deps/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,老板一定會對你刮目相看。

Copyright © Linux教程網 All Rights Reserved