Automake 使用帮助
首先准备 make 编译工具链 autoscan
、aclocal
、autoheader
、autoconf
和 automake
。
然后准备源码:头文件 main.h 和源文件 main.cpp,放在同一目录 src 下。
头文件 main.h:
#include <iostream>
源文件 main.cpp(configure.ac 默认只识别 main 关键字命名的文件):
#include "main.h"
int main(void)
{
std::cout << "This program generated by Automake." << std::endl;
return 0;
}
1. 修改 configure.ac(旧版格式为 .in,已过时,新版不兼容)
进入 src 目录,使用 autoscan 扫描源码结构。
$ cd src
$ autoscan
这里会生成 autoscan.log 和 configure.scan 两个文件,修改 configure.scan 作为 configure.ac。
$ mv configure.scan configure.ac
$ vim configure.ac
打开 configure.ac 做以下修改。
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
-AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
+AC_INIT([main], [1.0], [email address]) # 初始化包名,版本号,bug 报告邮箱
AC_CONFIG_SRCDIR([main.cpp])
AC_CONFIG_HEADERS([config.h])
+AM_INIT_AUTOMAKE # 表示使用 Automake 生成 Makefile
# 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
+AC_OUTPUT(Makefile) # 指定输出文件名为 Makefile
2. 生成 configure, config.h.in, autom4te.cache 等文件
运行以下命令,顺序不固定。
$ aclocal # 生成 autom4te.cache 目录
$ autoheader # 生成 config.h.in 文件
$ autoconf # 生成 configure 文件
3. 编写 Makefile.am
$ vim Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main # 指定生成的 ELF 文件名
main_SOURCES=main.cpp # 指定 main 函数所在的源文件
4. 生成 Makefile.in
使用 automake 生成 Makefile.in 文件。
$ automake --add-missing
注:–add-missing 选项会添加丢失的必备的文件。
5. 生成 Makefile
执行上面生成的 configure 文件,得到 Makefile。
$ ./configure
6. make 构建源码
使用 make 命令得到可执行程序 main。
$ make