How to use GNU Autotools

一剑行天下 posted @ 2010年5月28日 17:49 in 学习 , 2735 阅读

            The Autotools are supposed to make projects simpler for the maintainer, right? And the answer to that question is a definitive “No”. Don’t misunderstand me here—the Autotools do make your life easier in the long run, but for different reasons than you may first realize. The primary goal of the Autotools is not to make project maintenance simpler, although I honestly believe the system is as simple as it can be, given the functionality it provides. It took me a while to figure this out, and really, it was one of my most significant Autotools epiphanies. Ultimately, I came to understand that the purpose of the Autotools is two-fold: First, to make life easer for your users, and second, to make your project more portable—even to systems on which you’ve never tested, installed or even built your code.

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

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

First,  autoscan,The autoscan program is used to generate a reasonable configure.ac file for a new project. We’ll spend some time on autoscan later in Chapter’s 3 and 6, as we go through the process of setting up the Autotools on a basic project.

    为了便于理解 用具体例子做演示,对test.c文件生成可执行文件main

test.c文件在目录kang@kang-desktop:~/桌面下

文件内容为:

#include<stdio.h>
int main()
{
    int i=0,j=0,k=0,l=10;

    printf("%d,%d,%d,%d\n",i,j,k,l);


   printf("It's great!!!!I'am gland!!\n");


}

使用autoscan在给定目录及其子目录树中检查源文件,如果没有给出目录,就在当前目录及其子目录树中进行检查。执行autoscan最终生成两个文件:configure.scan、autoscan.log。终端显示如下: 

kang@kang-desktop:~/桌面/test >$ autoscan  kang@kang-desktop:~/桌面/test >$ ls 
autoscan.log configure.scan Makefile.am Makefile.am~ test.c

 

 

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

首先打开configure.scan文件

 

>$ gvim configure.scan  

 

将其修改为

#  *- Autoconf -*- # Process this file with autoconf to produce a configure script. 
AC_PREREQ([2.63])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AM_INIT_AUTOMAKE(main,1.0)//修改的内容,即生成的目标执行文件main
PKG_CHECK_MODULES(GOBJECT, gobject-2.0 >= 2.20)//Glib库模块
AC_CONFIG_SRCDIR([test.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_CONFIG_FILES([Makefile]) AC_OUTPUT
或者 内容为 # Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT(pm, 0.1, liyanrui.m2@gmail.com)
AM_INIT_AUTOMAKE(pm, 0.1)
AC_CONFIG_SRCDIR([pm/matrix.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_LIBTOOL

PKG_CHECK_MODULES(GLIB, glib-2.0)
PKG_CHECK_MODULES(GOBJECT, gobject-2.0)
PKG_CHECK_MODULES(GSL, gsl)

# Checks for libraries.
# FIXME: Replace `main' with a function in `-lgsl':
AC_CHECK_LIB([gsl], [main])
# FIXME: Replace `main' with a function in `-lgslcblas':
AC_CHECK_LIB([gslcblas], [main])

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CHECK_FUNCS([sqrt])

AC_CONFIG_FILES([Makefile
pm.pc
pm/Makefile
test/Makefile])
AC_CONFIG_MACRO_DIR([m4])
AC_OUTPUT


 

其中

1、AC_PREREQ宏声明本文件要求的autoconf版本。
2、AC_INIT宏用来定义软件的名称和版本等信息,这里的BUG-REPORT-ADDRESS可以省略。
3、AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。这个参数一般不需要修改。
4、AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

Second, 将configure.scan改名configure.in

 

kang@kang-desktop:~/桌面/test >$ mv configure.scan configure.in 

 

 third ,    aclocal,执行aclocal生成宏命令文件aclocal.m4

The aclocal utility is actually documented by the GNU manuals as a temporary work-around for a certain lack of flexibility in Autoconf.

Autoconf was designed and written first, and then a few years later, the idea for Automake was conceived as an add-on for Autoconf.

But Autoconf was really not designed to be extensible on the scale required by Automake.

kang@kang-desktop:~/桌面/test
>$ aclocal
kang@kang-desktop:~/桌面/test
>$ ls
aclocal.m4 autoscan.log Makefile.am test.c
autom4te.cache configure.in Makefile.am~

Forth ,执行autoconf 

  Autoconf changed this paradigm almost overnight. A quick glance at the AUTHORS file in the Savannah Autoconf project repository will give you an idea of the number of people that have had a hand in the making of Autoconf. The original author was David MacKenzie, who started the Autoconf project in 1991. While configuration scripts were becoming longer and more complex, there were really only a few variables that needed to be specified by the user. Most of these were simply choices to be made regarding components, features and options: Where do I find libraries and header files? Where do I want to install my finished product? Which optional components do I want to build into my products? With Autoconf, instead of modifying, debugging and losing sleep over literally thousands of lines of supposedly portable shell script, developers can write a short meta-script file, using a concise macro-based language, and let Autoconf generate a perfect configuration script such asconfigure.scan。
 

kang@kang-desktop:~/桌面/test >$ autoconf kang@kang-desktop:~/桌面/test >$ ls 
aclocal.m4 autoscan.log configure.in Makefile.am~ autom4te.cache configure Makefile.am test.c

 

fifthautoheader

The autoheader utility generates a C language header file template from configure.ac. This template file is usually called config.h.in.

接着使用 autoheader 命令,它负责生成 config.h.in 文件。该工具通常会从“acconfig.h”文件中复制用户附加的符号定义, 因为此处没有附加符号定义, 所以不需要创建“acconfig.h”文件。如下所示:

 

kang@kang-desktop:~/桌面/test >$ autoheader kang@kang-desktop:~/桌面/test >$ ls
 
aclocal.m4 autoscan.log configure Makefile.am test.c autom4te.cache config.h.in configure.in Makefile.am~

 

Sixthautomake

So, what’s so difficult about writing a makefile? Well, actually, once you’ve done it a few times, writing a basic makefile for a new project is really rather trivial.

The problems occur when you try to do more than just the basics. And let’s face it—what project maintainer has ever been satisfied with just a basic makefile?

The single most significant difference between a successful free software project and one that rarely gets a second glance can be found deep in the heart of project maintenance details.

These details include providing the so-called “standard make targets”. Potential users become disgusted with a project fairly easily—especially when certain

bits of expected functionality are missing or improperly written. Users have come to expect certain more or less standard make targets.

这 一步是创建 Makefile 很重要的一步,automake 要用的脚本配置文件是 Makefile.am,用户需要自己创建相应的文件。之后,automake 工具转换成 Makefile.in。

在该例中,用户创建的文件为 Makefile.am 如下所示:

AUTOMAKE_OPTIONS=foreign   
main_CPPFLAGS = $(GOBJECT_CFLAGS)
main_LDFLAGS= $(GOBJECT_LIBS)
bin_PROGRAMS=main
main_SOURCES=main.c
或者内容为 ACLOCAL_AMFLAGS = -I m4
pkginclude_HEADERS = m2list.h m2vector.h m2point.h m2matrix.h
lib_LTLIBRARIES = liblabtree.la
liblabtree_la_CFLAGS = $(GLIB_CFLAGS) -I../$(top_builddir)
liblabtree_la_LDFLAGS = $(GLIB_LIBS) -lgsl -lgslcblas -version-info 1:0:1
liblabtree_la_SOURCES = m2list.c m2vector.c m2point.c m2matrix.c

 

 

然后执行automake --add-missing

kang@kang-desktop:~/桌面/test >$ automake --add-missing 
configure.in:6: installing `./install-sh' configure.in:6:
installing `./missing' Makefile.am: installing `./depcomp'

 对该脚本文件的对应项进行解释。
• 其中的 AUTOMAKE_OPTIONS 为设置 automake 的选项。由于 GNU 对自己发布的软件有严格的规范, 比如必须附带许可证声明文件 COPYING 等,否则 automake 执行时会报错。automake 提供了 3 种软件等级:foreign、gnu 和 gnits,让用户选择采用,默认等级为 gnu。在本例使用 foreign 等级,它只检测必须的文件。
• bin_PROGRAMS 定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。
• hello_SOURCES 定义“hello”这个执行程序所需要的原始文件。如果“hello”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用 空格隔开。例如:若目标体“main”需要“main.c”、 “sunq.c” 、“hello.h”三个依赖文件,则定义hello_SOURCES=hello.c sunq.c hello.h。要注意的是,如果要定义多个执行文件,则对每个执行程序都要定义相应file_SOURCES。
接 下 来 可 以 使 用 automake 对 其 生 成 “ configure.in ” 文 件 , 在 这 里 使 用 选 项—adding-missing”可以让 automake 自动添加有一些必需的脚本文件

 执行automake ,在这一步中,通过运行自动配置设置文件 configure,把 Makefile.in 变成了最终的Makefile

>$ automake kang@kang-desktop:~/桌面/test >$ ls 
aclocal.m4 config.h.in depcomp Makefile.am~ test.c autom4te.cache configure
install-sh Makefile.in autoscan.log configure.in Makefile.am missing

 

 

Seventh,  运行configure,use configure to generate makefile

 

kang@kang-desktop:~/桌面/test >$ ./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... no checking for mawk... mawk 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: executing depfiles commands
kang@kang-desktop:~/桌面/test >$ ls
aclocal.m4 config.h config.status depcomp Makefile.am missing autom4te.cache
config.h.in configure
install-sh Makefile.am~ stamp-h1 autoscan.log config.log configure.in Makefile
Makefile.in test.c

 

 

Eighth键入 make 默认执行“make all”命令,即目标体为 all,其执行情况如下所示:

kang@kang-desktop:~/桌面/test >$ make 
make all-am make[1]: 正在进入目录 `/home/kang/桌面/test' gcc -DHAVE_CONFIG_H -I.
-g -O2 -MT test.o -MD -MP -MF .deps/test.Tpo -c -o test.o test.c mv -f .deps/test.Tpo .deps/test.Po gcc -g -O2
-o main test.o make[1]:正在离开目录 `/home/kang/桌面/test'
Nineth,最后执行可执行文件main

 

>$ ./main 0,0,0,10 It's great!!!!
kang@kang-desktop:~/桌面/test
>$ make clean
test -z "main" || rm -f main
rm -f *.o

 last

Everytime you edit any of the GNU Autotools input files in your package, you must regenerate the output files. If you add a new source file to
the helloworld_SOURCES variable in Makefile.am you must regenerate Makefile.in. If you are building your package you will need to rerun
configure to regenerate the Makefile's. Many project maintainers put the necessary commands to do this into a script called autogen.sh and
run this script whenever the output files need to be regenerated.

Create a text file called autogen.sh in the top-level directory and make sure you change its file mode to make it executable.

Add the following commands to the file and save it:


#! /bin/sh 
aclocal
autoheader
automake --add-missing
autoconf

 Now you can easily run the following commands to update your project's output files, and rebuild the project:

kang@kang-desktop:~/桌面/test
>$ ./autogen.sh 
kang@kang-desktop:~/桌面/test > $ ./configure
kang@kang-desktop:~/桌面/test >$ make
kang@kang-desktop:~/桌面/test > $ make install

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter