深入淺出學Makefile<二:如何自動生成Makefile>

 (第二部分:龐大的makefile是怎麼生成的 )

四:自動生成makefile

4.1 以helloworld.c爲源文件

root@parson-desktop:/home/parson/tmp/automk# ls

hellolinux.c

4.2.使用Autoscan工具生成configure.ac文件

root@parson-desktop:/home/parson/tmp/automk# autoscan

程序“autoscan”尚未安裝。  您可以使用以下命令安裝:

apt-get install autoconf

安裝autoconf,輸入如下命令

root@parson-desktop:/home/parson/tmp/automk# apt-get install autoconf

正在讀取軟件包列表... 完成

正在分析軟件包的依賴關係樹      

正在讀取狀態信息... 完成      

將會安裝下列額外的軟件包:

  automake autotools-dev

建議安裝的軟件包:

  autoconf2.13 autoconf-archive gnu-standards autoconf-doc libtool gettext

下列【新】軟件包將被安裝:

  autoconf automake autotools-dev

升級了 0 個軟件包,新安裝了 3 個軟件包,要卸載 0 個軟件包,有 273 個軟件包未被升級。

需要下載 1,444kB 的軟件包。

解壓縮後會消耗掉 4,325kB 的額外空間。

您希望繼續執行嗎?[Y/n]y

獲取:1 http://ftp.sjtu.edu.cn/ubuntu/ lucid/main autoconf 2.65-3ubuntu1 [772kB]

獲取:2 http://ftp.sjtu.edu.cn/ubuntu/ lucid/main autotools-dev 20090611.1 [64.1kB]                               

獲取:3 http://ftp.sjtu.edu.cn/ubuntu/ lucid/main automake 1:1.11.1-1 [608kB]                                     

下載 1,444kB,耗時 2分 59秒 (8,044B/s)                                                                             

選中了曾被取消選擇的軟件包 autoconf。

(正在讀取數據庫 ... 系統當前總共安裝有 149752 個文件和目錄。)

正在解壓縮 autoconf (從 .../autoconf_2.65-3ubuntu1_all.deb) ...

選中了曾被取消選擇的軟件包 autotools-dev。

正在解壓縮 autotools-dev (從 .../autotools-dev_20090611.1_all.deb) ...

選中了曾被取消選擇的軟件包 automake。

正在解壓縮 automake (從 .../automake_1%3a1.11.1-1_all.deb) ...

正在處理用於 man-db 的觸發器...

正在處理用於 doc-base 的觸發器...

Processing 39 changed 1 added doc-base file(s)...

Registering documents with scrollkeeper...

正在處理用於 install-info 的觸發器...

正在設置 autoconf (2.65-3ubuntu1) ...

正在設置 autotools-dev (20090611.1) ...

正在設置 automake (1:1.11.1-1) ...

update-alternatives: 使用 /usr/bin/automake-1.11 來提供 /usr/bin/automake (automake),於 自動模式 中。

root@parson-desktop:/home/parson/tmp/automk# ls

hellolinux.c

再一次執行autoscan

root@parson-desktop:/home/parson/tmp/automk# autoscan

root@parson-desktop:/home/parson/tmp/automk# ls

autoscan.log  configure.scan  hellolinux.c

生成了autoscan.log和configure.scan

我們看看生成了兩個文件是什麼東西

root@parson-desktop:/home/parson/tmp/automk# cat autoscan.log

autoscan.log沒有內容

root@parson-desktop:/home/parson/tmp/automk# cat configure.scan

#                                               -*- Autoconf -*-

# Process this file with autoconf to produce a configure script. 

AC_PREREQ([2.65])

AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])

AC_CONFIG_SRCDIR([helloworld.c])

AC_CONFIG_HEADERS([config.h]) 

# 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

(1)AC_PREREQ宏聲明本文件要求的autoconf版本,本例使用的版本爲2.65。

(2)AC_INIT宏用來定義軟件的名稱和版本等信息,”FULL-PACKAGE-NAME”爲軟件包名稱,”VERSION”爲軟件版本號,”BUG-REPORT-ADDRESS”爲BUG報告地址(一般爲軟件作者郵件地址)。

(3)AC_CONFIG_SRCDIR宏用來偵測所指定的源碼文件是否存在,來確定源碼目錄的有效性。此處爲當前目錄下的helloworld.c。

(4)AC_CONFIG_HEADER宏用於生成config.h文件,以便autoheader使用。

(5)AC_PROG_CC用來指定編譯器,如果不指定,選用默認gcc。

(6)AC_OUTPUT用來設定 configure 所要產生的文件,如果是makefile,configure會把它檢查出來的結果帶入makefile.in文件產生合適的makefile。使用 Automake時,還需要一些其他的參數,這些額外的宏用aclocal工具產生。

root@parson-desktop:/home/parson/tmp/automk# mv configure.scan configure.ac

root@parson-desktop:/home/parson/tmp/automk# vim configure.ac

#                                               -*- Autoconf -*-

# Process this file with autoconf to produce a configure script. 

AC_PREREQ([2.65])

AC_INIT(hellolinux,1.0,[email protected])

AM_INIT_AUTOMAKE(hellolinux,1.0)

AC_CONFIG_SRCDIR([hellolinux.c])

#AC_CONFIG_HEADERS([config.h]) 

# 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.ac" 21L, 498C 已寫入                                                            

root@parson-desktop:/home/parson/tmp/automk# ls

autoscan.log  configure.ac  hellolinux.c

4.3 利用aclocal工具製作aclocal.m4文件

root@parson-desktop:/home/parson/tmp/automk# aclocal

root@parson-desktop:/home/parson/tmp/automk# ls

aclocal.m4  autom4te.cache  autoscan.log  configure.ac  hellolinux.c

4.4 利用autoconf製作configure工具

root@parson-desktop:/home/parson/tmp/automk# autoconf

root@parson-desktop:/home/parson/tmp/automk# ls

aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  hellolinux.c

4.5編寫Makefile.am文件

root@parson-desktop:/home/parson/tmp/automk# vim Makefile.am 

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=hellolinux

hellolinux_SOURCES=hellolinux.c~

"Makefile.am" [新] 3L, 81C 已寫入       

4.6 通過automake產生Makefile.in文件                                                  

root@parson-desktop:/home/parson/tmp/automk# ./configure

configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."

root@parson-desktop:/home/parson/tmp/automk# automake --add-missing

configure.ac:6: installing `./install-sh'

configure.ac:6: installing `./missing'

Makefile.am: installing `./depcomp'

root@parson-desktop:/home/parson/tmp/automk# ls

aclocal.m4      autoscan.log  configure     depcomp       install-sh   Makefile.in

autom4te.cache  config.log    configure.ac  hellolinux.c  Makefile.am  missing

4.7 通過運行./configure產生Makefile文件

root@parson-desktop:/home/parson/tmp/automk# ./configure

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking for gcc... gcc

checking whether the C compiler works... yes

checking for C compiler default output file name... a.out

checking for suffix of executables...

checking whether we are cross compiling... no

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 ISO C89... 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

root@parson-desktop:/home/parson/tmp/automk# ls

aclocal.m4      autoscan.log  config.status  configure.ac  hellolinux.c  Makefile     Makefile.in

autom4te.cache  config.log    configure      depcomp       install-sh    Makefile.am  missing 

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