Linux下使用automake、autoconf產生configure檔案__Linux

來源:互聯網
上載者:User
一、產生configure過程中各檔案之間的關係圖

二、詳細介紹

autoscan: 掃描原始碼以搜尋普通的可移植性問題,比如檢查編譯器,庫,標頭檔等,組建檔案configure.scan,它是configure.ac的一個雛形。

aclocal:根據已經安裝的宏,使用者定義宏和acinclude.m4檔案中的宏將configure.ac檔案所需要的宏集中定義到檔案 aclocal.m4中。aclocal是一個perl 指令碼程式,它的定義是:“aclocal - create aclocal.m4 by scanning configure.ac”

automake:將Makefile.am中定義的結構建立Makefile.in,然後configure指令碼將產生的Makefile.in檔案轉換 為Makefile。如果在configure.ac中定義了一些特殊的宏,比如AC_PROG_LIBTOOL,它會調用libtoolize,否則它 會自己產生config.guess和config.sub

autoconf:將configure.ac中的宏展開,產生configure指令碼。這個過程可能要用到aclocal.m4中定義的宏。 三、執行個體 1.測試代碼(定義兩個檔案hello.h和hello.c)

/*hello.c*/#include <iostream>#include "hello.h"using namespace std;int main(){    CHello a;    return 0;}
/*hello.h*/#ifndef __HELLO_H__#define __HELLO_H__#include<iostream>using namespace std;class CHello{public:    CHello(){ cout<<"Hello!"<<endl;}    ~CHello(){ cout<<"Bye!"<<endl;}};#endif
2.操作步驟 (1)安裝依賴的包
[root@bogon autoconfig]# yum -y install automake autoconf

automake包括:aclocal、automake等

autoconf包括:autoscan、autoconf等 (2)autoscan

[root@bogon autoconfig]# ll-rw-r--r-- 1 root root 105 Jun  4 hello.cpp-rw-r--r-- 1 root root 189 Jun  4 hello.h[root@bogon autoconfig]# autoscan[root@bogon autoconfig]# lltotal 12-rw-r--r-- 1 root root   0 Jun  4 autoscan.log-rw-r--r-- 1 root root 481 Jun  4 configure.scan-rw-r--r-- 1 root root 105 Jun  4 hello.cpp-rw-r--r-- 1 root root 189 Jun  4 hello.h
(3)aclocal
[root@bogon autoconfig]# mv configure.scan configure.ac[root@bogon autoconfig]# vim configure.ac  /*將下面紅色加粗的部分修改掉*/#                                               -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_PREREQ([2.69])AC_INIT(hello, 1.0, admin@163.com)AM_INIT_AUTOMAKE(hello, 1.0)AC_CONFIG_SRCDIR([hello.cpp])AC_CONFIG_HEADERS([config.h])# Checks for programs.AC_PROG_CXXAC_PROG_CC# Checks for libraries.# Checks for header files.# Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.AC_OUTPUT(Makefile)[root@bogon autoconfig]# aclocal[root@bogon autoconfig]# lltotal 52-rw-r--r-- 1 root root 37794 Jun  4 aclocal.m4drwxr-xr-x 2 root root    51 Jun  4 autom4te.cache-rw-r--r-- 1 root root     0 Jun  4 autoscan.log-rw-r--r-- 1 root root   492 Jun  4 configure.ac-rw-r--r-- 1 root root   105 Jun  4 hello.cpp-rw-r--r-- 1 root root   189 Jun  4 hello.h

下面給出本檔案的簡要說明(所有以”#”號開始的行為注釋):
· AC_PREREQ 宏聲明本檔案要求的autoconf版本,本例使用的版本為2.59。
· AC_INIT 宏用來定義軟體的名稱和版本等資訊,”FULL-PACKAGE-NAME”為軟體包名稱,”VERSION”為軟體版本號碼,”BUG-REPORT-ADDRESS”為BUG報告地址(一般為軟體作者郵件地址)。
·AC_CONFIG_SRCDIR 宏用來偵測所指定的源碼檔案是否存在,來確定源碼目錄的有效性。此處為目前的目錄下的hello.c。
·AC_CONFIG_HEADER 宏用於產生config.h檔案,以便autoheader使用。
·AC_PROG_CC 用來指定編譯器,如果不指定,選用預設gcc。
·AC_OUTPUT 用來設定 configure 所要產生的檔案,如果是makefile,configure會把它檢查出來的結果帶入makefile.in檔案產生合適的makefile。使用Automake時,還需要一些其他的參數,這些額外的宏用aclocal工具產生。 (3)autoconf

[root@bogon autoconfig]# autoconf[root@bogon autoconfig]# lltotal 204-rw-r--r-- 1 root root  37794 Jun  4 aclocal.m4drwxr-xr-x 2 root root     81 Jun  4 autom4te.cache-rw-r--r-- 1 root root      0 Jun  4 autoscan.log-rwxr-xr-x 1 root root 154727 Jun  4 configure-rw-r--r-- 1 root root    492 Jun  4 configure.ac-rw-r--r-- 1 root root    105 Jun  4 hello.cpp-rw-r--r-- 1 root root    189 Jun  4 hello.h

此時可以看到已經產生了configure (4)autoheader

[root@bogon autoconfig]# autoheader[root@bogon autoconfig]# lltotal 208-rw-r--r-- 1 root root  37794 Jun  4 aclocal.m4drwxr-xr-x 2 root root     81 Jun  4 autom4te.cache-rw-r--r-- 1 root root      0 Jun  4 autoscan.log-rw-r--r-- 1 root root    625 Jun  4 config.h.in-rwxr-xr-x 1 root root 154727 Jun  4 configure-rw-r--r-- 1 root root    492 Jun  4 configure.ac-rw-r--r-- 1 root root    105 Jun  4 hello.cpp-rw-r--r-- 1 root root    189 Jun  4 hello.h

autoheader產生了configure.h.in如果在configure.ac中定義了AC_CONFIG_HEADER,那麼此檔案就需要; (5)Makefile.am

[root@bogon autoconfig]# vim Makefile.am[root@bogon autoconfig]# cat Makefile.am AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=hello hello_SOURCES=hello.cpp hello.h

· AUTOMAKE_OPTIONS 為設定Automake的選項。由於GNU對自己發布的軟體有嚴格的規範,比如必須附帶許可證聲明檔案COPYING等,否則Automake執行時會報錯。Automake提供了3種軟體等級:foreign、gnu和gnits,供使用者選擇,預設等級為gnu。本例使需用foreign等級,它只檢測必須的檔案。
· bin_PROGRAMS 定義要產生的執行檔案名稱。如果要產生多個執行檔案,每個檔案名稱用空格隔開。
· hello_SOURCES 定義”hello”這個執行程式所需要的原始檔案。如果”hello”這個程式是由多個原始檔案所產生的,則必須把它所用到的所有原始檔案都列出來,並用空格隔開。例如:若目標體”hello”需要”hello.c”、”hello.h”兩個依賴檔案,則定義hello_SOURCES=hello.c hello.h。 (6)automake

[root@bogon autoconfig]# automake --add-missingconfigure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocationconfigure.ac:6: installing './install-sh'configure.ac:6: installing './missing'[root@bogon autoconfig]# lltotal 236-rw-r--r-- 1 root root  37794 Jun  4  aclocal.m4drwxr-xr-x 2 root root     81 Jun  4  autom4te.cache-rw-r--r-- 1 root root      0 Jun  4  autoscan.log-rw-r--r-- 1 root root    625 Jun  4  config.h.in-rwxr-xr-x 1 root root 154727 Jun  4  configure-rw-r--r-- 1 root root    492 Jun  4  configure.ac-rw-r--r-- 1 root root    105 Jun  4  hello.cpp-rw-r--r-- 1 root root    189 Jun  4  hello.hlrwxrwxrwx 1 root root     35 Jun  4  install-sh -> /usr/share/automake-1.13/install-sh-rw-r--r-- 1 root root     79 Jun  4  Makefile.am-rw-r--r-- 1 root root  22227 Jun  4  Makefile.inlrwxrwxrwx 1 root root     32 Jun  4  missing -> /usr/share/automake-1.13/missing

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.