//在unbutu10.10测试过,可以正常运行,关键步骤已经用红色标出
使用 Vi编辑器编辑源程序,手动创建hello.c
在 Linux 操作 Shell 提示符使用 Vi 编辑器下创建 hello.c 源程序。
[root@localhost ch0206]# mkdir hello //创建文件夹
[root@localhost ch0206]# cd hello //切换文件
[root@localhost hello]# ls //已经创建的 hello.c
文件 hello.c
[root@localhost hello]# cat hello.c //C 源程序代码
#include <stdio.h>
int main(int argc,char** argv)
{
printf("hello world\n");
return 0;
}
使用 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 //输入文件名 2.6.1
Autoconf/Automake 工具组简介
下面给出本文件的简要说明(所有以"#"号开始的行为注释):
(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
此文件的相关内容需要根据当前软件和系统环境进行配置,但是,AM_INIT_ AUTOMAKE 宏需要自己添加,它是 automake 所必备的宏,同前面一样,PACKAGE是要产生的软件套件的名称,VERSION 是版本编号。其他设置请参阅注释内容。
使用 aclocal 工具生成 aclocal.m4
aclocal 工具用于扫描 configure.ac 文件生成 aclocal.m4。此工具根据已经安装的宏、用户定义宏和 acinclude.m4 文件中的宏将 configure.ac 文件需要的宏集中定义到文件 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
使用 autoconf 工具生成 configure 文件
将 configure.ac 中的宏展开,生成 configure 脚本。这个过程可能要用到
aclocal.m4 中定义的宏。
[root@localhost hello]# autoconf //执行 autoconf 生成configure 文件
[root@localhost hello]# ls
aclocal.m4 autoscan.log configure.ac hello.c
autom4te.cache configure configure.scan
使用 autoheader 工具生成 config.h.in 文件
autoheader 工具负责生成 config.h.in 文件。该工具会从"acconfig.h"文件中
复制用户附加的符号定义。此步骤可以在第 3 或第 4 步之前完成。
[root@localhost hello]# find / -name acconfig.h //系统 acconfig.h
文件位置
/usr/src/kernels/2.6.11-1.1369_FC4-i686/include/acpi/acconfig.h
[root@localhost hello]# autoheader
[root@localhost hello]# ls //查看生成的 config.h.in 文件
aclocal.m4 autoscan.log configure configure.scan
autom4te.cache config.h.in configure.ac hello.c
创建 Makefile.am 文件
Automake 工具会根据 configure.in 中的参量把 Makefile.am 转换成
Makefile.in 文件。在使用 Automake 工具前,读者需要手工创建脚本配置文件
Makefile.am。本例中,作者创建的文件如下所示:
[root@localhost hello]# ls Makefile.am
Makefile.am
[root@localhost hello]# cat Makefile.am //
Makefile.am 范例
AUTOMAKE_OPTIONS = foreign //软件等级
bin_PROGRAMS = hello //可执行文件名
hello_SOURCES = hello.c //源文件名
其中 :
(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。
Autoconf/Automake 工具组简介
使用 Automake 生成 Makefile.in 文件
下面使用 Automake 生成"Makefile.in"文件,使用选项"--add-missing"可以让
Automake 自动添加一些必需的脚本文件。如下所示:
[root@localhost hello]# automake --add-missing
configure.ac: installing './install-sh' //创建 install-sh 文件
configure.ac: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: required file './NEWS' not found
Makefile.am: required file './README' not found
Makefile.am: required file './AUTHORS' not found
Makefile.am: required file './ChangeLog' not found Makefile.am: installing './COPYING'
Makefile.am: installing './depcomp'
[root@localhost hello]# automake --add-missing //再运行一次,可以辅
助生成几个必要的文件
Makefile.am: required file './NEWS' not found //没有找到 NEWS 文件
Makefile.am: required file './README' not found
Makefile.am: required file './AUTHORS' not found
Makefile.am: required file './ChangeLog' not found
[root@localhost hello]# touch NEWS //创建 NEWS 文件,如果没有自动生成,手工创建
[root@localhost hello]# touch README //创建 README 文件
[root@localhost hello]# touch AUTHORS //创建 AUTHORS 文件
[root@localhost hello]# touch ChangeLog //创建 ChangeLog 文件
[root@localhost hello]# automake --add-missing //再运行一次
[root@localhost hello]# ls //生成必要的文件
aclocal.m4 ChangeLog configure.scan INSTALL missing
AUTHORS config.h.in COPYING install-sh NEWS
autom4te.cache configure depcomp Makefile.am README
autoscan.log configure.ac hello.c Makefile.in
[root@localhost hello]# ls configure.in -l
-rw-r--r-- 1 root root 536 Dec 27 04:29 configure.in
配置
运行自动配置设置文件 configure,把 Makefile.in 变成最终的 Makefile。
[root@localhost hello]# ./configure //配置,生成 Makefile 文件
……
config.status: creating Makefile
config.status: executing depfiles commands
[root@localhost hello]# ls
aclocal.m4 ChangeLog config.status COPYING install-sh mi
ssing
AUTHORS config.h configure depcomp Makefile NE
WS
autom4te.cache config.h.in configure.ac hello.c Makefile.am RE
ADME
autoscan.log config.log configure.scan INSTALL Makefile.in st
amp-h1
[root@localhost hello]# ls -l Makefile*
-rw-r--r-- 1 root root 16876 Dec 27 04:51 Makefile
-rw-r--r-- 1 root root 68 Dec 27 04:46 Makefile.am
-rw-r--r-- 1 root root 17180 Dec 27 04:50 Makefile.in 9 99 9. .. .测试 测试 测试 测试
运行 make 命令进行编译,下面的示例中 Make 的主要编译命令为"gcc -g
-O2 -o hello hello.o",此句对应本节前面介绍的手工编辑的 Makefile 文
件内容。
[root@localhost hello]# cd ../hello
[root@localhost hello]# make //执行 make 命令
make all-am
……
gcc -g -O2 -o hello hello.o //编译指令
……
编译成功后,将在当前目录下生成并运行可执行程序 hello。测试源代码是否正
确:
[root@localhost hello_2]# ./hello
hello!GNU
此方法生成的 makefile 文件很全面。使用"make install"命令把目标文件安装
在系统中。
[root@localhost hello]# make install //安装
make[1]: Entering directory
'/root/book/ch02/ch0206/hello'
test -z "/usr/local/bin" || mkdir -p --
"/usr/local/bin"
/usr/bin/install -c 'hello'
'/usr/local/bin/hello' //安装目标文件
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory
'/root/book/ch02/ch0206/hello'
使用"make uninstall"命令把目标文件从系统中卸载。
[root@localhost hello]# make uninstall //卸载
命令
rm -f '/usr/local/bin/hello' //从系统中卸载
使用"make clean"命令清除编译时的 obj 文件。
[root@localhost hello]# make clean
test -z "hello" || rm -f hello
rm -f *.o //删除 obj 文件
//使用"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