Chinaunix首页 | 论坛 | 博客
  • 博客访问: 106464
  • 博文数量: 40
  • 博客积分: 1650
  • 博客等级: 上尉
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-20 13:05
文章分类
文章存档

2011年(1)

2009年(1)

2008年(1)

2007年(37)

我的朋友

分类: LINUX

2007-07-24 15:05:47

     本文将介绍如何利用Autoconf 及Automake协助我们产生 Makefile
1. 简介

    Makefile 基本上就是目标(target), 关连(dependencies) 和动作三者所组成的一连串规则。而 make会根据 Makefile 的规则来决定如何编译 (compile) 和连结 (link) 程式。实际上,make 可做的不只是编译和连结程式,Makefile 还可以做到自动下载原始程式套件,解压缩 (extract) ,修补 (patch),设定,然后编译,连接及安装至系统中。

    Makefile 基本构造虽然简单,但是能妥善运用这些规则并不是件容易的事。为了减轻程式设计师维护 Makefile 的负担,因此有了Automake。

    程式设计师只需写一些预先定义好的巨集 (macro),交给 Automake 处理後会产生一个可供Autoconf 使用的 Makefile.in 档。再配合利用Autoconf 产生的自动设定档 configure即可产生一份符合 GNU Makefile惯例的 Makeifle 了。

2.确认系统软体

    在开始用 Automake 之前,先确认你的系统已经安装以下的软体:
1. GNU Automake
2. GNU Autoconf
3. GNU m4
4. GNU Libtool (如果你需要产生 shared library)

3. 一个简单的例子
   下面以拿hello.c 来做个示范。

    在工作目录下建立一个新的子目录 ``demo'',这个目录将作为我们存放 hello 这个程式及其相关档案的地方:

    bash-2.05b# mkdir demo
    bash-2.05b# cd demo

    用编辑器写个 hello.c 档,
    #include stdio.h
    int main(int argc, char** argv)
    {
    printf("Hello, world\n'');
    return 0;
    }

    接下来就要用 Autoconf 及 Automake 来帮我们产生 Makefile 档了

1. 用 autoscan 产生一个 configure.in 的雏型,执行 autoscan 後会产生一个configure.scan 的档案,我们可以用它做为configure.in档的蓝本。

bash-2.05b# autoscan
bash-2.05b# ls
autoscan.log  configure.scan  hello.c

2. 编辑 configure.scan 档,如下所示,并且把它的档名改成configure.in
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
                                                                                                           
AC_PREREQ(2.57)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([hello.c])
AM_INIT_AUTOMAKE(hello,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)

3. 执行 aclocal 和 autoconf ,分别会产生 aclocal.m4 及 configure 两个档案
bash-2.05b#aclocal
bash-2.05b#autoconf
bash-2.05b#ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.in  hello.c

4. 编辑 Makefile.am 档,内容如下
AUTOMAKE_OPTIONS= foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c

5. 执行 automake --add-missing ,Automake 会根据 Makefile.am 档产生一些档案,包含最重要的 Makefile.in
bash-2.05b# automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./mkinstalldirs'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'

6. 最後执行 ./configure ,
bash-2.05b# ./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

    现在你的目录下已经产生了一个 Makefile 档,make'指令就可以开始编译 hello.c

bash-2.05b# make
source='hello.c' object='hello.o' libtool=no \
depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo' \
depmode=gcc3 /bin/sh ./depcomp \
gcc -DPACKAGE_NAME=\"FULL-PACKAGE-NAME\" -DPACKAGE_TARNAME=\"full-package-name\" -DPACKAGE_VERSION=\"VERSION\" -DPACKAGE_STRING=\"FULL-PACKAGE-NAME\ VERSION\" -DPACKAGE_BUGREPORT=\"BUG-REPORT-ADDRESS\" -DPACKAGE=\"hello\" -DVERSION=\"1.0\"  -I. -I.     -g -O2 -c `test -f 'hello.c' || echo './'`hello.c
gcc  -g -O2   -o hello  hello.o

bash-2.05b# ./hello
Hello, world

    你还可以试试 "make clean'',''make install'',''make dist'' 看看会有什麽结果。

4. 探究

    上述产生 Makefile 的过程和以往自行编写的方式非常不一样,舍弃传统自行定义 make 的规则,使用 Automake 只需用到一些已经定义好的巨集即可。我们把巨集及目标 (target)写在 Makefile.am 档内,Automake读入 Makefile.am 档後会把这一串已经定义好的巨集展
开并且产生对应的Makefile.in 档, 然後再由 configure 这个 shell script 根据Makefile.in 产生适合的 Makefile。
    在此范例中可藉由 Autoconf 及 Automake 工具所产生的档案有 configure.scan、aclocal.m4、configure、Makefile.in,需要我们加入设定者为 configure.in 及 Makefile.am。

4.1 编辑 configure.in 档

    Autoconf 是用来产生 'configure' 档的工具。'configure' 是一个shell script,它可以自动设定原始程式以符合各种不同平台上 Unix 系统的特性,并且根据系统叁数及环境产生合适的 Makefile 档或是C 的标头档 (header file),让原始程式可以很方便地在这些不同
的平台上被编译出来。Autoconf 会读取 configure.in 档然後产生 'configure' 这个shell script。

    configure.in 档的内容是一连串 GNU m4 的巨集,这些巨集经过autoconf 处理後会变成检查系统特徵的 shell script。configure.in 内巨集的顺序并没有特别的规定,但是每一个 configure.in 档必须在所有巨集前加入 AC_INIT 巨集,然後在所有巨集的最後面加上 AC
_OUTPUT 巨集。我们可先用 autoscan 扫描原始档以产生一个 configure.scan 档,再对 configure.scan 做些修改成 configure.in 档。在范例中所用到的巨集如下:

dnl
    这个巨集後面的字不会被处理,可视为注解。

AC_INIT(FILE)
    这个巨集用来检查原始码所在的路径,autoscan 会自动产生,我们不必修改它。

AM_INIT_AUTOMAKE(PACKAGE,VERSION)
    这是使用 Automake 所必备的巨集,PACKAGE 是我们所要产生软体套件的名称,VERSION 是版本编号。

AC_PROG_CC
    检查系统可用的 C 编译器,如果原始程式是用 C 写的就需要这个巨集。

AC_OUTPUT(FILE)
    设定 configure 所要产生的档案,如果是 Makefile 的话,configure 便会把它检查出来的结果带入 Makefile.in 档然後产生
合适的 Makefile。

    实际上,我们使用 Automake 时,还须要一些其它的巨集,这些额外的巨集我们用 aclocal来帮我们产生。执行 aclocal 会产生 aclocal.m4档,如果没有特别的用途,我们可以不必修改它,用 aclocal 所产生的巨集会告诉 Automake 怎麽做。

    有了 configure.in 及 aclocal.m4 两个档案後,便可以执行 autoconf来产生 configure档了。

4.2 编辑 Makefile.am 档

    接下来我们要编辑 Makefile.am 档,Automake 会根据 configure.in 中的巨集把Makefile.am 转成 Makefile.in 档。Makefile.am 档定义我们所要产的目标:

AUTOMAKE_OPTIONS
    设定 automake 的选项。Automake 主要是帮助开发 GNU 软体的人员维护软体套件,所以在执行 automake 时,会检查目录下是否存在标准 GNU 软体套件中应具备的文件档案,例如 'NEWS'、'AUTHOR'、'ChangeLog' 等文件档。设成 foreign 时,automake 会改用一般软
体套件的标准来检查。

bin_PROGRAMS
    定义我们所要产生的执行档档名。如果要产生多个执行档,每个档名用空白字元隔开。

hello_SOURCES
    定义 'hello' 这个执行档所需要的原始档。如果 'hello' 这个程式是由多个原始档所产生,必须把它所用到的原始档都列出来,以空白字元隔开。假设 'hello' 这个程式需要 'hello.c'、'main.c'、

'hello.h'
    三个档案的话,则定义
        hello_SOURCES= hello.c main.c hello.h
    如果我们定义多个执行档,则对每个执行档都要定义相对的filename_SOURCES。

    编辑好 Makefile.am 档,就可以用 automake --add-missing 来产生Makefile.in。加上 --add-missing 选项是告诉 automake 顺便帮我们加入包装一个软体套件所必备的档案。Automake 产生出来的 Makefile.in档是完全符合 GNU Makefile 的惯例,我们只要执行 confi
gure 这个shell script 便可以产生合适的 Makefile 档了。

4.3 使用 Makefile

    利用 configure 所产生的 Makefile 档有几个预设的目标可供使用,我们只拿其中几个简述如下:

make all
    产生我们设定的目标,即此范例中的执行档。只打 make 也可以,此时会开始编译原始码,然後连结,并且产生执行档。

make clean
    清除之前所编译的执行档及目的档 (object file, *.o)。

make distclean
    除了清除执行档和目的档外,也把 configure 所产生的 Makefile也清除掉。

make install
    将程式安装至系统中。如果原始码编译无误,且执行结果正确,便可以把程式安装至系统预设的执行档存放路径。如果我们用bin_PROGRAMS 巨集的话,程式会被安装至 /usr/local/bin 这个目录。

make dist
    将程式和相关的档案包装成一个压缩档以供散播 (distribution) 。执行完在目录下会产生一个以 PACKAGE-VERSION.tar.gz 为名称的档案。PACKAGE 和 VERSION 这两个变数是根据 configure.in 档中AM_INIT_AUTOMAKE(PACKAGE, VERSION) 的定义。在此范例中会产生
'hello-1.0.tar.gz' 的档案。

make distcheck
    和 make dist 类似,但是加入检查包装後的压缩档是否正常。这个目标除了把程式和相关档案包装成 tar.gz 档外,还会自动把这个压
缩档解开,执行 configure,并且进行 make all 的动作,确认编译无误後,会显示这个 tar.gz 档已经准备好可供散播了。这个检查非
常有用,检查过关的套件,基本上可以给任何一个具备 GNU 发展境的人去重新编译。就 hello-1.tar.gz 这个范例而言,除了在 Red
Hat Linux 上,在 FreeBSD 2.2.x 版也可以正确地重新编译。

    要注意的是,利用 Autoconf 及 Automake 所产生出来的软体套件是可以在没有安装 Autoconf 及 Automake 的环境上使用的,因为 configure 是一个 shell script,它己被设计可以在一般 Unix 的 sh 这个 shell 下执行。但是如果要修改 configure.in 及 Makefile.a
m 档再产生新的configure 及 Makefile.in 档时就一定要有 Autoconf 及 Automake 了。

    Autoconf 和 Automake 功能十分强大,你可以从它们所附的 info 档找到详细的用法。

阅读(701) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~