autoconf和automake的使用 这个资料是非常完整的,本文大部分内容都是从这篇资料借鉴过来的 感谢作者YuChen。
1:准备源文件hello.c
代码语言:javascript复制➜ project git:(master) ✗ ls
hello.c
➜ project git:(master) ✗ cat hello.c
#include <stdio.h>
int main(int argc, const char *argv[]) {
printf("Hello World!n");
return 0;
}
➜ project git:(master) ✗
2:使用autoscan工具生成configure.scan并重命名
代码语言:javascript复制➜ project git:(master) ✗ autoscan
➜ project git:(master) ✗ mv configure.scan configure.ac
➜ project git:(master) ✗ ls
autoscan.log configure.ac hello.c
3:修改configure.ac
初始内容如下
代码语言:javascript复制➜ project git:(master) ✗
# -*- 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_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])
# 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
修改后内容
代码语言:javascript复制# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AM_INIT_AUTOMAKE #1 :是automake所必备的宏。新版本中该宏不需要任何参数
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])
# 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) #这里输出为Makefile
4:使用aclocal工具生成aclocal.m4
代码语言:javascript复制➜ project git:(master) ✗ aclocal
➜ project git:(master) ✗ ls
aclocal.m4 autom4te.cache autoscan.log configure.ac hello.c
➜ project git:(master) ✗
5:使用autoconf工具生成configure文件
代码语言:javascript复制➜ project git:(master) ✗ autoconf
➜ project git:(master) ✗ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac hello.c
➜ project git:(master) ✗
6:使用autoheader工具生成config.h.in
代码语言:javascript复制➜ project git:(master) ✗ autoheader
➜ project git:(master) ✗ ls
aclocal.m4 autom4te.cache autoscan.log config.h.in configure configure.ac hello.c
➜ project git:(master) ✗
7:创建Makefile.am文件
automake工具会根据config.in中的参量把Makefile.am转换成Makefile.in文件。在使用Automake之前,要先手动建立Makefile.am文件。 Makefile.am的内容如下:
代码语言:javascript复制AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.c
Makefile.am的内容解释:
AUTOMAKE_OPTIONS
为设置的Automake选项。它有三种等级提供给用户选择:foreign,gnu,gnits,默认等级为gnu.在此使用foreign,它只检测必须的文件。bin_PROGRAMS
定义要产生的执行文件名。如果要产生多个可执行文件,则每个文件名用空格隔开。hello_SOURCES
定义为产生hello这个可执行程序所需要的原始文件。如果其是由多个文件组成的,则必须用空格进行隔开。
8:使用automake工具生成Makefile.in文件
要使用选项“—add-missing”可以让Automake自动添加一些必要的脚本文件。
代码语言:javascript复制➜ project git:(master) ✗ automake --add-missing
configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './depcomp'
➜ project git:(master) ✗ ls
Makefile.am aclocal.m4 autoscan.log config.h.in configure.ac hello.c missing
Makefile.in autom4te.cache compile configure depcomp install-sh
➜ project git:(master) ✗
9:运行自动配置设置文件configure,根据Makefile.in生成最终的Makefile
代码语言:javascript复制➜ project git:(master) ✗ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
➜ project git:(master) ✗
10:运行make命令进行编译、测试
代码语言:javascript复制➜ project git:(master) ✗ make
/Library/Developer/CommandLineTools/usr/bin/make all-am
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc -g -O2 -o hello hello.o
➜ project git:(master) ✗ ./hello
Hello World!
➜ project git:(master) ✗
参考资料
- autoconf和automake的使用 这个资料是非常完整的,本文大部分内容都是从这边资料拷贝过来的 感谢作者YuChen
- 例解 autoconf 和 automake 生成 Makefile 文件
- automake 和 autoconf 使用简明教程