Linux自動(dòng)化構(gòu)建工具(Autotools)

我們需要編寫的文件

  1. Makefile.am
    Makefile.am 是定義編譯規(guī)則,需要編譯的源代碼等。
  2. configure.ac/configure.in
    autoscan命令可以生成configure.scan文件,然后將它改名字為configure.ac或者configure.in。并且可以手動(dòng)修改之。

流程

  1. autoscan生成configure.ac
    掃描源代碼以搜尋普通的可移植性問(wèn)題,比如檢查編譯器,庫(kù),頭文件等,生成文件configure.scan,它是configure.ac的一個(gè)雛形
your source files --> [autoscan*] --> [configure.scan] --> configure.ac/configure.in
  1. aclocal生成aclocal.m4文件
    根據(jù)已經(jīng)安裝的宏,用戶定義宏和acinclude.m4文件中的宏將configure.ac文件所需要的宏集中定義到文件 aclocal.m4中
acinclude.m4 -----------------------------.
                                          V
                                      .-------,
configure.ac ------------------------>|aclocal|
                 {user macro files} ->|       |------> aclocal.m4
                                      `-------'
  1. autoheader生成autoconfig.h.in[可選]
    autoheader(autoconf): 根據(jù)configure.ac中的某些宏,比如cpp宏定義,運(yùn)行m4,生成config.h.in
aclocal.m4 -------------------------------- .
                                             |
                                             V
                                        .----------,
configure.ac -------------------------->|autoheader|----> autoconfig.h.in
                                        `----------'
  1. automake將Makefile.am(需要手動(dòng)編寫)中定義的結(jié)構(gòu),生成Makefile.in,然后configure腳本將生成的Makefile.in文件轉(zhuǎn)換 為Makefile。如果在configure.ac中定義了一些特殊的宏,比如AC_PROG_LIBTOOL,它會(huì)調(diào)用libtoolize,否則它 會(huì)自己產(chǎn)生config.guess和config.sub
                                   .--------,
                                   |        | - - -> COPYING
                                   |        | - - -> INSTALL
                                   |        |------> install-sh
                                   |        |------> missing
                                   |automake|------> mkinstalldirs
configure.ac --------------------->|        |
Makefile.am  --------------------->|        |------> Makefile.in
                                   |        |------> stamp-h.in
                               .---+        | - - -> config.guess
                               |   |        | - - -> config.sub
                               |   `------+-'
                               |          | - - - -> config.guess
                               |libtoolize| - - - -> config.sub
                               |          |--------> ltmain.sh
                               |          |--------> ltconfig
                               `----------'
  1. autoconf生成configure腳本
    將configure.ac中的宏,aclocal.m4中定義的宏和第3步的autoconfig.h.in展開,生成configure腳本。
aclocal.m4 ,autoconfig.h.in - - - - - - -.
                                         V
                                     .--------,
configure.ac ----------------------->|autoconf|------> configure
                                    `----------'

  1. 運(yùn)行./configure生成Makefile
    運(yùn)行./configure,可以加入?yún)?shù)
                                          .-------------> [config.cache]
     configure* --------------------------+-------------> config.log
                                          |
           [config.h.in]-----.            v              .--> [autoconfig.h]
                             +-------> config.status* ---+                   
              Makefile.in ---'                           `-->   Makefile

例子

目標(biāo):在/hello/目錄下創(chuàng)建一個(gè)hello.c文件,并編譯運(yùn)行它

  1. 編寫源文件
#include<stdio.h> 
int main(int argc, char** argv)
{
    printf("Hello, GNU!n");
    return 0;
}
  1. 生成configure.scan

[xxx]$ autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1

生成了configure.scan,autoscan.log文件,將configure.scan 修改為 configure.in,最后修改的內(nèi)容如下:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([hello.c])
#AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(hello, 1.0)
# 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(Makefile)
  1. aclocal生成aclocal.m4
    運(yùn)行aclocal ,生成 aclocal.m4 和 autom4te.cache (生成aclocal.m4的過(guò)程中涉及到configure.in)
  2. 編寫Makefiel.am
AUTOMAKE_OPTIONS= foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c
  1. 生成Makefile.in
    生成 Makefile.in, depcomp, install-sh, 和 missing (根據(jù) Makefile.am, 和 aclocal.m4)
  2. autoconf生成configure
    生成 configure (根據(jù) configure.in, 和 aclocal.m4)
  3. configure
    生成 Makefile, config.log, 和 config.status

總結(jié)

命令流如下

autoscan==>mv configure.scan configure.in==>aclocal
  ==>create Makefile.am==>automake==>autoconf==>configure

參考資料

比較雜,可以看中間部分https://www.cnblogs.com/gaoshanxiaolu/p/4128146.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容