autoconf和automake的使用 这个资料是非常完整的,本文大部分内容都是从这篇资料借鉴过来的 感谢作者YuChen。
➜ 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) ✗
➜ project git:(master) ✗ autoscan
➜ project git:(master) ✗ mv configure.scan configure.ac
➜ project git:(master) ✗ ls
autoscan.log configure.ac hello.c
初始内容如下
➜ 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
修改后内容
# -*- 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
➜ project git:(master) ✗ aclocal
➜ project git:(master) ✗ ls
aclocal.m4 autom4te.cache autoscan.log configure.ac hello.c
➜ project git:(master) ✗
➜ project git:(master) ✗ autoconf
➜ project git:(master) ✗ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac hello.c
➜ project git:(master) ✗
➜ 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) ✗
automake工具会根据config.in中的参量把Makefile.am转换成Makefile.in文件。在使用Automake之前,要先手动建立Makefile.am文件。 Makefile.am的内容如下:
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这个可执行程序所需要的原始文件。如果其是由多个文件组成的,则必须用空格进行隔开。
要使用选项“—add-missing”可以让Automake自动添加一些必要的脚本文件。
➜ 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) ✗
➜ 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) ✗
➜ 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) ✗