歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下AutoTools工具集使用教程

Linux下AutoTools工具集使用教程

日期:2017/3/1 10:06:53   编辑:Linux編程

編寫makefile 確實不是一件輕松的事,尤其對於一個較大的項目而言更是如此。那麼,有沒有一種輕松的手段生成makefile而同時又能讓用戶享受make 的優越性呢?本節要講的autoTools系列工具正是為此而設的,它只需用戶輸入簡單的目標文件、依賴文件、文件目錄等就可以輕松地生成makefile了。另外,這些工具還可以完成系統配置信息的收集,從而可以方便地處理各種移植性的問題。也正是基於此,現在Linux上的軟件開發一般都用autoTools 來生成makefile。

autoTools使用流程

autoTools 是系列工具,它包含了aclocal、autoscan、autoconf、autoheader、和automake這些工具,使用autoTools主要就是利用各個工具的腳本文件來生成最後的makefile文件。其總體流程如下:

autoTools生成makefile流程圖

接下來用實例12-4 /*hello.c*/來做為實例:

1、建立一個hello的目錄,這個目錄將作為存放hello程序及其相關檔案的地方。

2、用編輯器在hello目錄下創建hello.c源文件,內容如下:

#include <stdio.h>

int main(int argc,char **argv)

{

printf("Hello,Make!\n");

return 0;

}

接下來就要用autoTools為我們生成makefile文檔:

3、autoscan

[root@localhosthello]# autoscan

autom4te: configure.ac: no such file or directory

autoscan: /usr/bin/autom4te failed with exit status: 1

它會在給定目錄及其子目錄樹中檢查源文件,默認是在當前目錄及其子目錄樹中進行檢查。執行autoscan後會產生一個configure.scan的檔案,我們可以用它做為configure.in文檔的藍本。但由上述結果可知autoscan會首先去讀入configure.ac文件,但此時還沒有創建該配置文件,於是它就自動生成了一個configure.scan文件。

[root@localhost hello]# ls

autoscan.log configure.scan hello.c

4、autoconf

configure.in是autoconf的腳本配置文件,它的原型文件configure.scan改名為configure.in並把內容更改為如下所示:

# -*- Autoconf -*- //以“#”號開始的行為注釋。

AC_PREREQ(2.59) //本文件要求的autoconf版本。

AC_INIT(hello,1.0) // AC_INIT宏用來定義軟件的名稱和版本等信息。

AM_INIT_AUTOMAKE(hello,1.0) //是automake所必備的宏,軟件名稱和版本號。

AC_CONFIG_SRCDIR([hello.c]) //用來偵測所指定的源碼文件是否存在。

AC_CONFIG_HEADER([config.h]) //用於生成config.h文件,以便autoheader使用。

AC_PROG_CC

AC_CONFIG_FILES([Makefile]) //用於生成相應的Makefile 文件。

AC_OUTPUT

接下來運行aclocal,生成一個“aclocal.m4”文件,該文件是處理本地的宏定義。

[root@localhost hello]# aclocal

再接著運行autoconf,生成configure可執行文件。

[root@localhost hello]#autoconf

[root@localhost hello]#ls

aclocal.m4 autom4te.cache autoscan.log configure configure.in hello.c

5、autoheader

使用autoheader命令是為了成config.h.in文件。該工具通常會從acconfig.h文件中復制用戶定義的符號,若此處沒有自定義符號就不需創建acconfig.h文件。

[root@localhost hello]#autoheader

6、automake

這一步是創建makefile很重要的一步,automake用的腳本配置文件是Makefile.am,需要自己創建相應的文件。再用automake工具將其轉成Makefile.in文件。內容如下所示:

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS= hello

hello_SOURCES= hello.c

AUTOMAKE_OPTIONS:設置automake的選項。由於GNU對自己發布的軟件有嚴格的規范,如必須附帶許可證聲明文件COPYING等,否則automake執行時會報錯。automake提供了3 種軟件等級:foreign、gnu、gnits,我們這裡用foreign只檢測必要文件。

bin_PROGRAMS:定義要產生的可執行文件名。如果要產生多個執行文件用空格隔開。

hello_SOURCES:定義hello這個可執行文件所需要的源文件。如hello這個程序是由多個原始文件所產生的,則須把它所用到的所有源文件都列出來,並用空格隔開。例如定義:hello_SOURCES=hello.cstdio.h hello.h。注意:如果要定義多個執行文件,則對每個執行文件都要定義相應的_SOURCES。

接下來使用automake生成configure.in文件,在這裡使用選項—adding-missing可以讓automake自動添加有一些必需的腳本文件。如下所示:

[root@localhost hello]#automake --add-missing

configure.in: installing './install-sh'

configure.in: installing './missing'

Makefile.am: installing 'depcomp'

[root@localhost hello]#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

通過運行自動配置設置文件configure,把Makefile.in變成了最終的Makefile。

[root@localhost hello]#./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 file name... 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: creating config.h

config.status: executing depfiles commands

到此步makefile就已經自動生成了。

Copyright © Linux教程網 All Rights Reserved