使用Autoconf/Automake工具自动生成Makefile文件的流程图
(1)创建源代码文件,使用"autoscan"生成configure.scan文件,将其重命名为configure.ac,并做适当修改,然 后使用"aclocal"命令生成aclocal.m4文件,使用"autoconf"命令由configure.ac和aclocal.m4文件生成 configure文件。
(2)手工编辑Makefile.am文件,使用"automake"命令生成configure.in文件。
(3)手工编辑或由系统给定acconfig.h文件,使用"autoheader"命令生成config.h.in文件。
(4)使用"configure"命令由configuer、configure.in和config.h.in文件生成Makefile文件。从而完成Makefile文件的创建过程。
1.使用Vi编辑器编辑源程序
2.使用Autoscan工具生成configure.ac文件Autoscan工具用来扫描源代码以搜寻一般的可移植性问题,比如检查编译器、库和头文件等,并创建configure.scan文件,它会在给定目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目录树中进行检查。如下所示:
[root@localhost hello]# autoscan .///在当前文件夹中搜索autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[root@localhost hello]# ls //生成configure.scan文件,它是configure.ac文件原型
autoscan.log configure.scan hello.c
[root@localhost hello]# cat configure.scan //configure.scan文件内容
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script. AC_PREREQ(2.59) //autoconf版本号
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS) //软件的名称和版本等信息
AC_CONFIG_SRCDIR([hello.c]) //侦测源码文件
AC_CONFIG_HEADER([config.h]) //用于生成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 //输入文件名
下面给出本文件的简要说明(所有以"#"号开始的行为注释):
(1)AC_PREREQ宏声明本文件要求的autoconf版本,本例使用的版本为2.59。
(2)AC_INIT宏用来定义软件的名称和版本等信息,"FULL-PACKAGE-NAME"为软件包名称,"VERSION"为软件版本号,"BUG-REPORT-ADDRESS"为BUG报告地址(一般为软件作者邮件地址)。
(3)AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。此处为当前目录下的hello.c。
(4)AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。
(5)AC_PROG_CC用来指定编译器,如果不指定,选用默认gcc。
(6)AC_OUTPUT用来设定 configure 所要产生的文件,如果是makefile,configure会把它检查出来的结果带入makefile.in文件产生合适的makefile。使用 Automake时,还需要一些其他的参数,这些额外的宏用aclocal工具产生。
中间的注释可以分别添加用户测试程序、测试函数库和测试头文件等宏定义。
此文件只是下面要使用的configure.ac文件的原型,要使用此文件,还需要根据情况修改相关内容。
[root@localhost hello]# cp configure.scan configure.ac //复制文件
[root@localhost hello]# ls
autoscan.log configure.ac configure.scan hello.c
[root@localhost hello]# cat configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
AC_INIT(hello,1.0,yangzongde@163.com) //在此行内容中设置当前软件包信息
AM_INIT_AUTOMAKE(hello,1.0) //automake所必备的宏,必须添加
AC_CONFIG_SRCDIR([hello.c]) //源文件名
AC_CONFIG_HEADER([config.h]) //config文件
# 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
3.使用aclocal工具生成aclocal.m4
[root@localhost hello]# aclocal //执行aclocal生成aclocal.m4文件
[root@localhost hello]# ls
aclocal.m4 autom4te.cache autoscan.log configure.ac configure.scan hello.c
4.使用autoconf工具生成configure文件
[root@localhost hello]# autoconf //执行autoconf生成configure文件
[root@localhost hello]# ls
aclocal.m4 autoscan.log configure.ac hello.c
autom4te.cache configure configure.scan
5.使用autoheader工具生成config.h.in文件
autoheader工具负责生成config.h.in文件。该工具会从"acconfig.h"文件中复制用户附加的符号定义。此步骤可以在第3或第4步之前完成。
[root@localhost hello]# find / -name acconfig.h //系统acconfig.h文件位置 |
6.创建Makefile.am文件
Automake工具会根据configure.in中的参量把Makefile.am转换成Makefile.in文件。在使用Automake工具前,读者需要手工创建脚本配置文件Makefile.am。本例中,作者创建的文件如下所示:
[root@localhost hello]# ls Makefile.am |
其中:
(1)AUTOMAKE_OPTIONS为设置Automake的选项。由于GNU对自己发布的软件有严格的规范,比如必须附带许可证声明文件 COPYING等,否则Automake执行时会报错。Automake提供了3种软件等级:foreign、gnu和gnits,供用户选择,默认等级 为gnu。本例使需用foreign等级,它只检测必须的文件。
(2)bin_PROGRAMS定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。
(3)hello_SOURCES定义"hello"这个执行程序所需要的原始文件。如果"hello"这个程序是由多个原始文件所产生的,则必须 把它所用到的所有原始文件都列出来,并用空格隔开。例如:若目标体"hello"需要"hello.c"、"hello.h"两个依赖文件,则定义 hello_SOURCES=hello.c hello.h。
7.使用Automake生成Makefile.in文件
下面使用Automake生成"Makefile.in"文件,使用选项"--add-missing"可以让Automake自动添加一些必需的脚本文件。如下所示:
[root@localhost hello]# automake --add-missing |
8.配置
运行自动配置设置文件configure,把Makefile.in变成最终的Makefile。
[root@localhost hello]# ./configure //配置,生成Makefile文件 |
9.测试
运行make命令进行编译,下面的示例中Make的主要编译命令为"gcc -g -O2 -o hello hello.o",此句对应本节前面介绍的手工编辑的Makefile文件内容。
[root@localhost hello]# cd ../hello |
编译成功后,将在当前目录下生成并运行可执行程序hello。测试源代码是否正确:
[root@localhost hello_2]# ./hello |
[root@localhost hello]# make install //安装 |
使用"make uninstall"命令把目标文件从系统中卸载。
[root@localhost hello]# make uninstall //卸载命令 |
使用"make clean"命令清除编译时的obj文件。
[root@localhost hello]# make clean |
使用"make dist"命令将程序和相关的文档打包为一个压缩文档以供发布。
[root@localhost hello]# make dist
……
[root@localhost hello]# ls //查看生成的文件
aclocal.m4 config.h.in configure.scan install-sh README
AUTHORS config.h.in~ COPYING Makefile stamp-h1
autom4te.cache config.log depcomp Makefile.am
Changelog config.status hello-1.0.tar.gz Makefile.in
ChangeLog configure hello.c missing
config.h configure.ac INSTALL NEWS
[root@localhost hello]# ls hello-1.0.tar.gz -l //打包文件
-rw-r--r-- 1 root root 67699 Dec 27 06:33 hello-1.0.tar.gz
