by 鄭治中 in Linux驅動程式開發
Create Makefile
在 Linux 上寫程式的人大概都要使用 Makefile執行編譯程式的工作,可是要寫出一個 Makefile就不簡單了。網路上介紹 Makefile 的資源很多,可是要變成高手並不容易,難怪許多人聞 Linux 色變。本文將介紹如兩套軟體來協助我們『自動』產生 Makefile 檔: GNU Autoconf 及 Automake
讓開發出來的軟體可以像一般常見的 GNU 軟體一樣,只要用 ``./configure'', ``make all'',``make install'' 就可以完成compile和install的動作。
希望這份簡介能幫助您輕鬆地進入 Linux Programming 的殿堂。
1. 上路之前
程 式設計師只需寫一些預先定義好的巨集 (macro),交給 Automake 處理後會產生一個可供 Autoconf 使用的 Makefile.in 檔。再配合利用Autoconf 產生的自動設定檔 configure 即可產生一份符合 GNU Makefile慣例的 Makeifle 了。
在開始試著用 Automake 之前,請先確認你的系統已經安裝以下的軟體:
l GNU Automake
l GNU Autoconf
l GNU m4
l perl
l GNU Libtool (如果你需要產生 shared library)
我會建議你最好也使用 GNU C/C++ 編譯器、GNU Make 以及其他 GNU 的工具程式來做為開發的環境,這些工具都是屬於 Open Source Software不僅免費而且功能強大。
2. 一個簡單的例子
Automake 所產生的 Makefile 除了可以做到程式的編譯和連結,還把產生相關資源檔 (如 manual page及 info 檔) 都考慮進去。所以原始程式所存放的目錄架構最好符合 GNU 的標準慣例,接下來我拿 hello.c 來做為例子。
在工作目錄下建立一個新的子目錄 ``devel'',再在 devel 下建立一個``hello'' 的子目錄,我們的程式及其相關檔案存放在 ./devel/hello 目錄下:
# mkdir devel
# cd devel
# mkdir hello
# cd hello
用編輯器寫好 hello.c 檔
#include int main(int argc, char** argv) { printf(``Hello world!\n''); return 0; } |
接下來就要用 Autoconf 及 Automake 來幫我們產生 Makefile 檔了,
2.1. 產生一個 configure.scan 的檔案
執行 autoscan 後會產生一個configure.scan 的檔案,可以用它做為 configure.in檔的原型。
# autoscan
# ls
configure.scan hello.c
2.2. 產生一個 configure.in 的檔案
編輯 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) |
2.3. 產生 aclocal.m4檔案
執行 aclocal會產生GNU m4 的巨集到aclocal.m4
# aclocal
2.4. 產生 configure檔案
執行 autoconf,Autoconf 會讀取 configure.in 檔然後產生 'configure' 這個shell script。
# autoconf
2.5. 產生 Makefile.in檔案
接下來我們要編輯 Makefile.am 檔,Automake 會根據 configure.in 中的巨集把Makefile.am 轉成 Makefile.in 檔。
Makefile.am內容
AUTOMAKE_OPTIONS= foreign bin_PROGRAMS= hello hello_SOURCES= hello.c |
這個程式需要 'hello.c'、'main.c'、'hello.h' 三個檔案的話,則定義 hello_SOURCES= hello.c main.c hello.h 如果我們定義多個執行檔,則對每個執行檔都要定義相對的filename_SOURCES。
Automake 會根據 Makefile.am 檔產生一些檔案,包含最重要的 Makefile.in
# automake --add-missing
automake: configure.in: installing `./install-sh'
automake: configure.in: installing `./mkinstalldirs'
automake: configure.in: installing `./missing'
2.6. 最後執行 ./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
現在你的目錄下已經產生了一個 Makefile 檔,下個 ``make'' 指令就可以開始編譯 hello.c 成執行檔。你還可以試試 ``make clean'',''make install'',''make dist'' 看看會有什麽結果。
# make
gcc -DPACKAGE=\"hello\" -DVERSION=\"1.0\" -I. -I. -g -O2 -c hello.c
gcc -g -O2 -o hello hello.o
# ./hello
Hello! GNU!
沒有留言:
張貼留言