Linux自动化构建工具(Autotools)

我们需要编写的文件

  1. Makefile.am
    Makefile.am 是定义编译规则,需要编译的源代码等。
  2. configure.ac/configure.in
    autoscan命令可以生成configure.scan文件,然后将它改名字为configure.ac或者configure.in。并且可以手动修改之。

流程

  1. 用autoscan生成configure.ac
    扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是configure.ac的一个雏形
your source files --> [autoscan*] --> [configure.scan] --> configure.ac/configure.in
  1. 用aclocal生成aclocal.m4文件
    根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到文件 aclocal.m4中
acinclude.m4 -----------------------------.
                                          V
                                      .-------,
configure.ac ------------------------>|aclocal|
                 {user macro files} ->|       |------> aclocal.m4
                                      `-------'
  1. 用autoheader生成autoconfig.h.in[可选]
    autoheader(autoconf): 根据configure.ac中的某些宏,比如cpp宏定义,运行m4,生成config.h.in
aclocal.m4 -------------------------------- .
                                             |
                                             V
                                        .----------,
configure.ac -------------------------->|autoheader|----> autoconfig.h.in
                                        `----------'
  1. 用automake将Makefile.am(需要手动编写)中定义的结构,生成Makefile.in,然后configure脚本将生成的Makefile.in文件转换 为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,否则它 会自己产生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. 运行./configure生成Makefile
    运行./configure,可以加入参数
                                          .-------------> [config.cache]
     configure* --------------------------+-------------> config.log
                                          |
           [config.h.in]-----.            v              .--> [autoconfig.h]
                             +-------> config.status* ---+                   
              Makefile.in ---'                           `-->   Makefile

例子

目标:在/hello/目录下创建一个hello.c文件,并编译运行它

  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,最后修改的内容如下:

#                                               -*- 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
    运行aclocal ,生成 aclocal.m4 和 autom4te.cache (生成aclocal.m4的过程中涉及到configure.in)
  2. 编写Makefiel.am
AUTOMAKE_OPTIONS= foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c
  1. 生成Makefile.in
    生成 Makefile.in, depcomp, install-sh, 和 missing (根据 Makefile.am, 和 aclocal.m4)
  2. autoconf生成configure
    生成 configure (根据 configure.in, 和 aclocal.m4)
  3. configure
    生成 Makefile, config.log, 和 config.status

总结

命令流如下

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

参考资料

比较杂,可以看中间部分https://www.cnblogs.com/gaoshanxiaolu/p/4128146.html

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容