Cmake 的意义在于跨越各种操作系统,Autotools 的意义在于跨越各种Unix 。
编译开源软件经常会遇到这种固定安装方式:
./configure
make
make install
这样的软件一般都是使用 GNU 的 Autotools 进行软件编译的,通常我们说的 Autotools 是一套工具集一般包含下面几个 GNU 开源项目:
- Autoconf:
autoscan
autoconf
autoheader
autoreconf
autoupdate
autom4te
ifnames
- Automake:
automake
aclocal
- Libtool:
libtoolize
Debian 系统下安装 autotools 方法为
apt install automake autoconf libtool m4
可以使用apt-file list autoconf |grep "/usr/bin"
来查看每个包都包含哪些应用软件
Dominik Kern 大神的个人主页上有一个经典教程讲述了怎么使用 Autotools 来开发应用,下图为教程中的一个图例:
我自己写了一个 Autotools 的例子放到了 Github 上面 autotools_hello ,使用的方式比较简单,使用 autogen.sh
去生成 configure
config.h.in
Makefile.in
等文件,使用 autoremove.sh
重新初始化文件
下面是 autogen.sh 的文件内容,从中可以看出 Autotools 工具的使用顺序:
#!/bin/sh
# autogen.sh
aclocal && \
autoheader && \
libtoolize && \
autoconf && \
touch NEWS README AUTHORS ChangeLog && \
automake --add-missing --force-missing --copy
Autotools运行流程图:
流程总结:
- 执行
autoscan
命令。这个命令主要用于扫描工作目录,并且生成 configure.scan 文件; - 修改 configure.scan 为 configure.ac 文件,并且修改配置内容,语法参考 Autoconf 文档;
- 执行
aclocal
命令。扫描 configure.ac 文件生成 aclocal.m4 文件; - 执行
autoheader
命令。生成 config.h.in 文件; - 执行
libtoolize
命令。如果生成库文件需要用到这一步骤; - 执行
autoconf
命令。将 configure.ac 文件中的宏展开,生成 configure 脚本; - 新增 Makefile.am 文件,修改配置内容,语法参考 Automake 文档 和 Libtool 文档;
- 执行
automake
命令。生成 Makefile.in 文件;
经过上面的8个步骤在工程目录下就会生成 configure, config.h.in, Makefile.in 三个文件,接下来就是经典的 ./configure -> make -> make install
了。