Chinaunix首页 | 论坛 | 博客
  • 博客访问: 537647
  • 博文数量: 119
  • 博客积分: 3391
  • 博客等级: 中校
  • 技术积分: 981
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-12 11:49
文章分类

全部博文(119)

文章存档

2014年(3)

2013年(1)

2011年(18)

2010年(27)

2009年(70)

我的朋友

分类: LINUX

2009-05-19 14:43:19

使用autotools建立Makefile简单实例解析


对于一个较大的项目而言,完全手动建立Makefile是一件费力而又容易出错的工作。autotools系列工具只需用户输入简单的目标文件、依赖文件、文件目录等就可以比较轻松地生成Makefile了。现在Linux上的软件开发一般都是用autotools来制作Makefile。

autotools工具主要有:aclocal、autoscan、autoconf、autoheader、automake。使用autotools主要就是利用各个工具的脚本文件来生成最后的Makefile。下面结合实例来介绍具体的流程。

第一步 autoscan

使用autoscan在给定目录及其子目录树中检查源文件,如果没有给出目录,就在当前目录及其子目录树中进行检查。最终生成两个文件:configure.scan、autoscan.log
[armlinux@lqm autotools-easy]$ tree
.
|-- main.c
|-- mytool1.c
|-- mytool1.h
|-- mytool2.c
`-- mytool2.h

0 directories, 5 files
[armlinux@lqm autotools-easy]$ autoscan
[armlinux@lqm autotools-easy]$ tree
.
|-- autoscan.log
|-- configure.scan
|-- main.c
|-- mytool1.c
|-- mytool1.h
|-- mytool2.c
`-- mytool2.h

0 directories, 7 files

其中,configure.scan是configure.in的原型文件。而configure.in是autoconf的脚本配置文件。所以下一步的工作就是要对configure.scan进行修改,将其转化为configure.in。

第二步 autoconf

configure.scan文件内容如下:
# -*- 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([main.c])
AC_CONFIG_HEADER([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_PREREQ宏声明本文件要求的autoconf版本。
3、AC_INIT宏用来定义软件的名称和版本等信息,这里的BUG-REPORT-ADDRESS可以省略。
4、AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。这个参数一般不需要修改。
5、AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

修改时需要增加一个宏AM_INIT_AUTOMAKE(PACKAGE,VERSION),还要把AC_CONFIG_HEADER更改为AM_CONFIG_HEADER。具体如下:
# -*- 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_INIT(main,1.0)
AM_INIT_AUTOMAKE(main,1.0)
AC_CONFIG_SRCDIR([main.c])
AM_CONFIG_HEADER([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(Makefile)

第三步 autoheader

首先把configure.scan更改名称为configure.in。然后执行aclocal,autoconf,autoheader。
[armlinux@lqm autotools-easy]$ mv configure.scan configure.in
[armlinux@lqm autotools-easy]$ ls
autoscan.log configure.in main.c mytool1.c mytool1.h mytool2.c mytool2.h
[armlinux@lqm autotools-easy]$ aclocal
[armlinux@lqm autotools-easy]$ ls
aclocal.m4 autoscan.log configure.in main.c mytool1.c mytool1.h mytool2.c mytool2.h
[armlinux@lqm autotools-easy]$ autoconf
[armlinux@lqm autotools-easy]$ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.in main.c mytool1.c mytool1.h mytool2.c mytool2.h
[armlinux@lqm autotools-easy]$ autoheader

第四步 automake

这是很重要的一步。automake需要的脚本配置文件是Makefile.am,这个文件需要自己建立。
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=main.c mytool1.c mytool1.h mytool2.c mytool2.h

AUTOMAKE_OPTIONS为设置automake的选项。automake提供了3种软件等级:foreign、gnu、gnits,让用户选择使用,默认等级是gnu。现在使用的foreign只是检测必要的文件。

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

file_SOURCES定义file这个执行程序的依赖文件。同样的,对于多个执行文件,那就要定义相应的file_SOURCES。

接下来就是使用automake对其生成configure.in文件。这里可以使用选项--adding-missing让automake自动添加一些必要的脚本文件。

第五步 运行configure
[armlinux@lqm autotools-easy]$ automake --add-missing
[armlinux@lqm autotools-easy]$ ./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
...

最后,使用 make dist 打包程序
阅读(1077) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~