Linux下使用automake、autoconf生成configure文件


一、生成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]# ll
total 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_CXX AC_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]# ll total 52 -rw-r--r-- 1 root root 37794 Jun 4 aclocal.m4 drwxr-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]# ll
total 204
-rw-r--r-- 1 root root  37794 Jun  4 aclocal.m4
drwxr-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]# ll
total 208
-rw-r--r-- 1 root root  37794 Jun  4 aclocal.m4
drwxr-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-missing
configure.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-invocation
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
[root@bogon autoconfig]# ll
total 236
-rw-r--r-- 1 root root  37794 Jun  4  aclocal.m4
drwxr-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
lrwxrwxrwx 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.in
lrwxrwxrwx 1 root root     32 Jun  4  missing -> /usr/share/automake-1.13/missing

此步主要是為了生成Makefile.in,加上--add-missing參數后,會補全缺少的腳本;

(6)測試

[root@bogon autoconfig]# ./configure
[root@bogon autoconfig]# make
[root@bogon autoconfig]# ./hello
Hello!
Bye!

和平時安裝許多開源軟件一樣操作

(7)打包

[root@bogon autoconfig]# make dist
[root@bogon autoconfig]# ll
total 436
-rw-r--r-- 1 root root  37794 Jun  4 aclocal.m4
drwxr-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    758 Jun  4 config.h
-rw-r--r-- 1 root root    625 Jun  4 config.h.in
-rw-r--r-- 1 root root  11031 Jun  4 config.log
-rwxr-xr-x 1 root root  32557 Jun  4 config.status
-rwxr-xr-x 1 root root 154727 Jun  4 configure
-rw-r--r-- 1 root root    492 Jun  4 configure.ac
lrwxrwxrwx 1 root root     32 Jun  4 depcomp -> /usr/share/automake-1.13/depcomp
-rwxr-xr-x 1 root root  22250 Jun  4 hello
-rw-r--r-- 1 root root  72021 Jun  4 hello-1.0.tar.gz -rw-r--r-- 1 root root    105 Jun  4 hello.cpp
-rw-r--r-- 1 root root    189 Jun  4 hello.h
-rw-r--r-- 1 root root  26008 Jun  4 hello.o
lrwxrwxrwx 1 root root     35 Jun  4 install-sh -> /usr/share/automake-1.13/install-sh
-rw-r--r-- 1 root root  23564 Jun  4 Makefile
-rw-r--r-- 1 root root     79 Jun  4 Makefile.am
-rw-r--r-- 1 root root  23869 Jun  4 Makefile.in
lrwxrwxrwx 1 root root     32 Jun  4 missing -> /usr/share/automake-1.13/missing
-rw-r--r-- 1 root root     23 Jun  4 stamp-h1

如果細心的話可以發現,人群中已經出現了hello-1.0.tar.gz就是將已經編譯好的文件進行了打包。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM