1. 程式人生 > >通過configure.ac檔案生成copnfigure檔案

通過configure.ac檔案生成copnfigure檔案

一、生成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)安裝依賴的包

[[email protected] autoconfig]# yum -y install automake autoconf

automake包括:aclocal、automake等

autoconf包括:autoscan、autoconf等

(2)autoscan

複製程式碼
[[email protected] autoconfig]# ll
-rw-r--r-- 1 root root 105 Jun  4 hello.cpp
-rw-r--r-- 1 root root 189 Jun  4 hello.h
[[email protected] autoconfig]# autoscan
[[email protected] 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

複製程式碼
[[email protected] autoconfig]# mv configure.scan configure.ac
[[email protected] autoconfig]# vim configure.ac  /*將下面紅色加粗的部分修改掉*/
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT(hello, 1.0, [email protected])
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)
[[email protected] autoconfig]# aclocal
[[email protected] 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

複製程式碼
[[email protected] autoconfig]# autoconf
[[email protected] 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

複製程式碼
[[email protected] autoconfig]# autoheader
[[email protected] 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

[[email protected] autoconfig]# vim Makefile.am
[[email protected] 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

複製程式碼
[[email protected] 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'
[[email protected] 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)測試

[[email protected] autoconfig]# ./configure
[[email protected] autoconfig]# make
[[email protected] autoconfig]# ./hello
Hello!
Bye!

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

(7)打包

複製程式碼
[[email protected] autoconfig]# make dist
[[email protected] 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
複製程式碼