连续型电子期刊的区别:autotools 的使用|『Linux System Programming』 - 曲径...

来源:百度文库 编辑:九乡新闻网 时间:2024/07/14 12:42:47

autotools 的使用

对于一个较大的项目而言,编写 Makefile 不是一件轻松的事情。而使用 autotools 系列工具,它只需要用户输入简单的目标文件、依赖文件、文件目录就可以轻松生成 Makefile 。

使用 autotools 系列工具,首先确认是否安装了以下工具:
引用 beyes@linux-beyes:~/C/Make> which aclocal
/usr/bin/aclocal
beyes@linux-beyes:~/C/Make> which autoscan
/usr/bin/autoscan
beyes@linux-beyes:~/C/Make> which autoconf
/usr/bin/autoconf
beyes@linux-beyes:~/C/Make> which autoheader
/usr/bin/autoheader
beyes@linux-beyes:~/C/Make> which automake
/usr/bin/automake

使用 autotools 主要就是利用各个工具的脚本文件以生成最后的 Makefile 。

这些工具的作用分别是
(1) autoscan
autoscan 用来在给定目录及其子目录树中检查源文件,扫描源代码目录从而生成 configure.scan 文件。autoscan 可以用目录名作为参数,也可以省略参数,此时 autoscan 将把当前目录当作参数。
configure.scan 文件很重要,它是 configure.in 文件的原型,通过修改 configure.scan 后得到 configure.in 文件。

( 2 ) aclocal
aclocal 是一个 perl 脚本程序。aclocal 根据 configure.in 文件的内容,自动生成 aclocal.m4 文件。aclocal 的定义是: “ aclocal-create aclocal.m4 by scanning configure.ac "

( 3 ) autoconf
autoconf 是用来产生 configure 文件的。configure 是一个脚本,它能设置源程序来适应不同的操作系统平台,并且根据不同的系统生成合适的 Makefile ,从而可以使源代码能够在不同的操作系统平台上被编译出来。

( 4 )autoheader
autoheader 负责生成 config.h.in 文件。该工具通常从 acconfig.h 文件中复制用户添加的符号定义。

( 5 ) automake
automake 是工具集中的重要成员之一,它调用脚本文件 Makefile.am , 并最终生成 configure 文件。我们就是通过调用 configure 文件来自动产生 makefile 的。


1、在一个目录里有以下文件:
引用 beyes@linux-beyes:~/C/Make> ls
fun.c head.h hello.c


2、使用 autoscan 扫描当前目录,得到一个 configure.scan 文件。configure.scan 文件是 configure.in 的原型文件。内容如下:
引用 # -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([fun.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

说明
<1> # 符号 : 此符号为注释符号

<2> AC_PRERREQ 宏声明文件要求的 autoconf 版本,这里是 2.63 ,这可以通过以下命令得知( 系统为使用基于 RPM 包管理的的 OpenSuse 11.1 ):
引用 beyes@linux-beyes:~/C/Make> rpm -q autoconf
autoconf-2.63-1.97


<3>AC_INIT 宏用来定义软件的名称和版本等信息。其中FULL-PACKAGE-NAME 表示软件的名称;VERSION 为软件的版本号; BUG-REPORT-ADDRESS 一般为作者的 E-mail 。
AC_CONFIG_SRCDIR 宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。
AC_CONFIG_HEAERS 宏用于生成 config.h 文件,以便 autoheader 使用。

3、现在需要将 configure.scan 改名为 configure.in ,然后会编辑这个 configure.in 这个文件。confiugre.in调用一系列 autoconf 宏来测试程序需要的或用到的特性是否存在,以及这些特性的功能。从 configure.scan 中可以看到 configure.in 的一般布局:
引用 AC_INIT
测试程序
测试函数库
测试头文件
测试类型定义
测试结构
测试编译器特性
测试库函数
测试系统调用
AC_OUTPUT

[/pre]上面的调用次序是建议性的,但强烈建议不要随便改变这些次序。下面是经过修改后的 configure.in 文件:
引用 # -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT(hello, 1.0, l4nneret@163.com)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])
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_CONFIG_FILES([Makefile])
AC_OUTPUT

说明
  • AM_INIT_AUTOMAKE 是 automake 所必备的宏,也同前面一样,里面的内容分别为 “产生套件的名称”,“VERSION" 版本号。
  • AC_CONFIG_FILES 宏用于生成相应的 Makefile 文件。
4、运行 aclocal 文件。这里可能会生成一个 aclocal.m4 ,该文件主要处理本地的宏定义:
引用 beyes@linux-beyes:~/C/Make/mktst> ls
aclocal.m4 autoscan.log configure.in head.h
autom4te.cache configure fun.c hello.c


5、接着再运行 autoconf ,生成 configure 可执行文件:
引用 beyes@linux-beyes:~/C/Make/mktst> autoconf
beyes@linux-beyes:~/C/Make/mktst> ls
aclocal.m4 autoscan.log configure.in head.h
autom4te.cache configure fun.c hello.c


6、autoheader
再运行 autoheader 命令,它负责生成 config.h.in 文件。该工具通常会从 "acconfig.h" 文件中复制用户附加的符号定义,由于此处没有附加符号定义,所以不需要创建 "acconfig.h" 文件:
引用 beyes@linux-beyes:~/C/Make/mktst> autoheader
beyes@linux-beyes:~/C/Make/mktst> ls
aclocal.m4 autoscan.log configure fun.c hello.c
autom4te.cache config.h.in configure.in head.h
beyes@linux-beyes:~/C/Make/mktst> cat config.h.in
/* config.h.in. Generated from configure.in by autoheader. */

/* Name of package */
#undef PACKAGE

/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT

/* Define to the full name of this package. */
#undef PACKAGE_NAME

/* Define to the full name and version of this package. */
#undef PACKAGE_STRING

/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME

/* Define to the version of this package. */
#undef PACKAGE_VERSION

/* Version number of package */
#undef VERSION


7、automake
这是创建 Makefile 的重要一步,automake 要用的脚本配置文件是 Makefile.am ,这个文件需要用户自己创建。之后, automake 工具转换成 Makefile.in 。Makefile.am 的内容为:
引用 AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.c fun.c head.h

说明
AUTOMAKE_OPTIONS 是设置 automake 的选项。由于 GNU 对自己发布的软件有严格的规范,比如必须附带许可证声明文件 COPYING 等,否则 automake 执行时会报错。
automake 提供了 3 种软件等级:foreign, gnu 和 gnits 让用户选择采用,默认等级为 gnu。这里采用 foreign ,它只检查必须的文件。

bin_PROGRAMS
定义要产生的可执行文件名。如果要产生多个可执行文件,那么每个文件名就用空格隔开。

hello_SOURCES 定义 "hello" 这个可执行程序所需的原始文件。如果 "hello" 这个程序由多个可原始文件产生,则必须把它所用到的所有原始文件都列出来,各文件名用空格隔开。需要注意的是,如果定义多个执行文件,则对每个执行程序都要定义相应的 file_SOURCES 。

执行以下执行命令:
引用 beyes@linux-beyes:~/C/Make/mktst> automake --add-missing
beyes@linux-beyes:~/C/Make/mktst> ls
aclocal.m4 autoscan.log configure fun.c hello.c Makefile.in
autom4te.cache config.h.in configure.in head.h Makefile.am


8、运行 configure
通过运行自动配置文件 configure ,把 Makefile.in 变成最终的 Makefile :
引用 beyes@linux-beyes:~/C/Make/mktst> ./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... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... 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 ISO C89... 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: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands

由上可以看到,已经生成 Makefile 文件。
运行 ./configure 时收集了系统的信息,用户可以在 configure 命令中对其方便的配置。在 ./configure 的自定义参数有两种,一种是开关式 ( --enable-XXX 或 --disable-XXX );另一种是开放式,即后面要填入一串字符 ( --with-XXX=yyy ) 参数。

9、使用 autotools 生成的 Makefile

<1> make
引用 beyes@linux-beyes:~/C/Make/mktst> make
make all-am
make[1]: Entering directory `/home/beyes/C/Make/mktst'
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT fun.o -MD -MP -MF .deps/fun.Tpo -c -o fun.o fun.c
mv -f .deps/fun.Tpo .deps/fun.Po
gcc -g -O2 -o hello hello.o fun.o
make[1]: Leaving directory `/home/beyes/C/Make/mktst'

从上面可以看到,生成了 hello 可执行文件,运行 ./hello 得到结果:
引用 beyes@linux-beyes:~/C/Make/mktst> ./hello
hello Makefile world
num is: 138
man is: 178


<2>make install
这条命令在权限许可的情况下,会把该程序安装到系统目录中去,此时若直接运行 hello 就可以显式出结果了。

<3> make clean
运行此命令后,make 会清除之前所编译的可执行文件和目标文件 ( object file, *.o ),如下:
引用 beyes@linux-beyes:~/C/Make/mktst> ls
aclocal.m4 config.h.in configure.in hello Makefile.am
autom4te.cache config.log fun.c hello.c Makefile.in
autoscan.log config.status fun.o hello.o stamp-h1
config.h configure head.h Makefile
beyes@linux-beyes:~/C/Make/mktst> make clean
test -z "hello" || rm -f hello
rm -f *.o
beyes@linux-beyes:~/C/Make/mktst> ls
aclocal.m4 config.h config.status fun.c Makefile stamp-h1
autom4te.cache config.h.in configure head.h Makefile.am
autoscan.log config.log configure.in hello.c Makefile.in


<4> make dist -- 发布软件
运行此命令后,make 将程序和相关的文档打包为一个压缩文档以供发布:
引用 beyes@linux-beyes:~/C/Make/mktst> make dist
{ test ! -d hello-1.0 || { find hello-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr hello-1.0; }; }
test -d hello-1.0 || mkdir hello-1.0
find hello-1.0 -type d ! -perm -777 -exec chmod a+rwx {} \; -o \
! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
! -type d ! -perm -400 -exec chmod a+r {} \; -o \
! -type d ! -perm -444 -exec /bin/sh /home/beyes/C/Make/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r hello-1.0
tardir=hello-1.0 && /bin/sh /home/beyes/C/Make/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >hello-1.0.tar.gz
{ test ! -d hello-1.0 || { find hello-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr hello-1.0; }; }
beyes@linux-beyes:~/C/Make/mktst> ls
aclocal.m4 config.h.in configure.in hello-1.0.tar.gz Makefile.am
autom4te.cache config.log depcomp hello.c Makefile.in
autoscan.log config.status fun.c install-sh missing
config.h configure head.h Makefile stamp-h1

可见,该命令生成了一个 hello-1.0.tar.gz 的压缩文件。

整个流程如下图所示

另外,有个复杂的例子可以参考:
http://www.ibm.com/developerworks/cn/linux/l-makefile