autotools系列工具—-自動生成Makefile

       在較大項目中, 如果手動維護Makefile, 那將是一件複雜並痛苦的事情. 那麼, 有沒有一種輕鬆的手段生成Makefile呢? autotools系列工具正是在這樣的呼聲中誕生的. 它只需用戶輸入簡單的目標文件, 依賴文件, 文件目錄等就可以輕鬆地生成Makefile了. 另外, 這些工具還可以完成系統配置信息的收集, 從而可以方便地處理各種移植性問題.autotools是系列工具, 它含有:
  • aclocal
  • autoscan
  • autoconf
  • autoheader
  • automake

autotools 使用流程

下面用一個簡單的hello.c程序, 演示autotools的使用流程. hello.c如下:
wangsheng@pc01:~/work/train/make/automake$ ls
hello.c
wangsheng@pc01:~/work/train/make/automake$ cat hello.c
#include 
int main()
{
  printf("Hello, autotools!\n");
  return 0;
}
(1) 使用autoscan命令自動生成configure.scan文件它會在給定目錄及其子目錄樹中檢查源文件, 若沒有給出目錄, 就在當前目錄及其子目錄樹中進行檢查.它會搜索源文件以尋找一般的移植性問題並創建一個文件"configure.scan", 該文件就是接下來autoconf要用到的"configure.in"原型.
wangsheng@pc01:~/work/train/make/automake$ autoscan
wangsheng@pc01:~/work/train/make/automake$ ls
autoscan.log  configure.scan  hello.c
(2)將configure.scan重命名爲configure.in, 並做適當修改configure.scan的內容:
wangsheng@pc01:~/work/train/make/automake$ 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([hello.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
將configure.scan重命名爲configure.in
wangsheng@pc01:~/work/train/make/automake$ mv configure.scan configure.in
根據具體情況, 適當修改, 以下加粗部分是修改的內容:
wangsheng@pc01:~/work/train/make/automake$ cat configure.in
#                                               -*- 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_INIT(hello,1.0)
AM_INIT_AUTOMAKE(hello,1.0)
AC_CONFIG_SRCDIR([hello.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_CONFIG_FILES([Makefile])
AC_OUTPUT
說明:
  • 以"#"號開始的行爲註釋
  • AC_PREREQ宏聲明本文要求的autoconf版本, 如本例中的版本 2.65
  • AC_INIT宏用來定義軟件的名稱和版本等信息, 在本例中省略了BUG-REPROT-ADDRESS, 一般爲作者的E-mail
  • AM_INIT_AUTOMAKE是手動添加的, 它是automake所必備的宏, 也同前面一樣, PACKAGE是所要產生軟件套件的名稱,VERSION是版本編號.
  • AC_CONFIG_SCRDIR宏用來偵測所指定的源碼文件是否存在, 來確定源碼目錄的有效性. 在此處指當前目錄下hello.c
  • AC_CONFIG_FILES宏用於生成相應的Makefile文件.
(3) 運行aclocal命令,生成"aclocal.m4"文件, 該文件主要處理本地的宏定義
wangsheng@pc01:~/work/train/make/automake$ aclocal
wangsheng@pc01:~/work/train/make/automake$ ls
aclocal.m4 autom4te.cache  autoscan.log  configure.in  hello.c
(4) 運行autoconf命令生成configure可執行文件
wangsheng@pc01:~/work/train/make/automake$ autoconf
wangsheng@pc01:~/work/train/make/automake$ ls
aclocal.m4 autom4te.cache  autoscan.log  configure  configure.in  hello.c
(5) 運行autoheader命令, 生成config.h.in文件.該工具通常會從"acconfig.h"文件中複製用戶附加的符號定義. 本例中沒有附加的符號定義, 所以不需要創建"acconfig.h"文件.
wangsheng@pc01:~/work/train/make/automake$ autoheader
wangsheng@pc01:~/work/train/make/automake$ ls
aclocal.m4 autom4te.cache  autoscan.log  config.h.in  configure  configure.in  hello.c
(6) 運行automake命令, 生成Makefile.in文件這一步是創建Makefile很重要的一步, automake要用的腳本配置文件是Makefile.am, 用戶需要自己創建相應的文件. 之後, automake工具將自動轉換成Makefile.in 本例中, 創建的文件爲Makefile.am, 內容如下:
wangsheng@pc01:~/work/train/make/automake$ cat Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.c
說明:
  • 其中的AUTOMAKE_OPTIONS爲設置automake的選項. 由於GNU對自己發佈的軟件有嚴格的規範, 比如必須附帶許可證聲明文件COPYING等, 否則automake執行時會報錯. automake提供了3中軟件等級:foreign, gnu和gnits, 供用戶選擇. 默認級別是gnu. 在本例中, 使用了foreign等級, 它只檢測必須的文件.
  • bin_PROGRAMS定義要產生的執行文件名. 如果要產生多個執行文件, 每個文件名用空格隔開
  • hello_SOURCES 定義"hello"這個可執行程序所需的原始文件. 如果"hello"這個程序是由多個源文件所產生的, 則必須把它所用到的所有源文件都列出來, 並用空格隔開. 如果要定義多個可執行程序, 那麼需要對每個可執行程序建立對應的file_SOURCES.
在這裏使用"--add-missiing"選項可以讓automake自動添加一些必須的腳本文件.
wangsheng@pc01:~/work/train/make/automake$ automake --add-missing
configure.in:7: installing `./install-sh'
configure.in:7: installing `./missing'
Makefile.am: installing `./depcomp'
wangsheng@pc01:~/work/train/make/automake$ ls
aclocal.m4      autoscan.log  configure     depcomp  install-sh   Makefile.in
autom4te.cache  config.h.in   configure.in  hello.c  Makefile.am  missing
(7)運行configure, 生成Makfefile文件
wangsheng@pc01:~/work/train/make/automake$ ./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: creating config.h
config.status: executing depfiles commands
wangsheng@pc01:~/work/train/make/automake$ ls
aclocal.m4      config.h     config.status  depcomp     Makefile     missing
autom4te.cache  config.h.in  configure      hello.c     Makefile.am  stamp-h1
autoscan.log    config.log   configure.in   install-sh  Makefile.in

autotools生成Makefile流程圖如下

使用由autotools生成的Makefile

生成的makefile內容很龐大!不知道爲啥會那麼大?????????????


autotools生成的Makefile具有以下主要功能:(1) make編譯源程序, 鍵入make, 默認執行"make all"命令
wangsheng@pc01:~/work/train/make/automake$ make
make  all-am
make[1]: Entering directory `/home/wangsheng/work/train/make/automake'
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc  -g -O2   -o hello hello.o
make[1]: Leaving directory `/home/wangsheng/work/train/make/automake'
此時在本目錄下就生成了可執行文件"hello", 運行"./hello"就能看到程序的執行結果:
wangsheng@pc01:~/work/train/make/automake$ ./hello
Hello, autotools!
(2) make install執行該命令, 可以把程序安裝到系統目錄中
wangsheng@pc01:~/work/train/make/automake$ sudo make install
此時, 直接在console輸入hello, 就可以看到程序的運行結果(3) make clean清除之前所編譯的可執行文件及目標文件
wangsheng@pc01:~/work/train/make/automake$ make clean
test -z "hello" || rm -f hello
rm -f *.o
(4) make dist將程序和相關的文檔打包爲一個壓縮文檔以供發佈
wangsheng@pc01:~/work/train/make/automake$ make dist
wangsheng@pc01:~/work/train/make/automake$ ls -l hello-1.0.tar.gz
hello-1.0.tar.gz

可見該命令生成了一個hello-1.0.tar.gz的壓縮文檔.

 

 

 

 

 

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