Linux下Makefile的automake生成全攻略( 二 )


AC_INIT(helloworld.c)
AM_INIT_AUTOMAKE(helloworld, 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)
============================configure.in内容结束=========================================
然后执行命令aclocal和autoconf,分别会产生aclocal.m4及configure两个文件:
$ aclocal
$ls
aclocal.m4 configure.in helloworld.c
$ autoconf
$ ls
aclocal.m4 autom4te.cache configure configure.in helloworld.c

大家可以看到configure.in内容是一些宏定义,这些宏经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本 。
autoconf 是用来生成自动配置软件源代码脚本(configure)的工具 。configure脚本能独立于autoconf运行,且在运行的过程中,不需要用户的干预 。
要生成configure文件,你必须告诉autoconf如何找到你所用的宏 。方式是使用aclocal程序来生成你的aclocal.m4 。
aclocal根据configure.in文件的内容,自动生成aclocal.m4文件 。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac” 。
autoconf从configure.in这个列举编译软件时所需要各种参数的模板文件中创建configure 。
autoconf需要GNU m4宏处理器来处理aclocal.m4,生成configure脚本 。
m4是一个宏处理器 。将输入拷贝到输出,同时将宏展开 。宏可以是内嵌的,也可以是用户定义的 。除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,循环等 。m4既可以作为编译器的前端,也可以单独作为一个宏处理器 。
4、新建Makefile.am
新建Makefile.am文件,命令:

$ vi Makefile.am

内容如下:

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c

automake会根据你写的Makefile.am来自动生成Makefile.in 。
Makefile.am中定义的宏和目标,会指导automake生成指定的代码 。例如,宏bin_PROGRAMS将导致编译和连接的目标被生成 。
5、运行automake
命令:

$ automake --add-missing
configure.in: installing `./install-sh"
configure.in: installing `./mkinstalldirs"
configure.in: installing `./missing"
Makefile.am: installing `./depcomp"

automake会根据Makefile.am文件产生一些文件,包含最重要的Makefile.in 。
6、执行configure生成Makefile

$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
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 ANSI C... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
$ ls -l Makefile
-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile

你可以看到,此时Makefile已经产生出来了 。
7、使用Makefile编译代码
$ make
if gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -
DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="helloworld" -DVERSION="1.0"
-I. -I. -g -O2 -MT helloworld.o -MD -MP -MF ".deps/helloworld.Tpo"
-c -o helloworld.o `test -f "helloworld.c" || echo "./"`helloworld.c;

推荐阅读