automake安装入门

来源:互联网 发布:淘宝直播抢红包软件 编辑:程序博客网 时间:2024/05/17 23:21

AutoTools 简单入门 [转]

学习GNU/LINUX 开发的编程人员,上手之后不久就会在编译开源软件的时候碰到configure脚本,过段时间还会知道configure脚本是autoconf生成的; 但是真正想用起来autoconf,却是要弄明白config.h,configure.in,Makfile.am等一大堆的文件,这可能要花些功夫。 让我们从一个例子开始,争取为大家省点力气。

 我们用个小程序作例子,计算一个整数的开方,建个工作目录:sqrt。程序很简单:
 #include<stdio.h>
 #include<math.h>
 int main()
 {
  int i=0;
  scanf("%d",&i);
  printf("sqrt(%d)=%f\n",i,sqrt(i));
 }
 接下来我们要编译这个程序:
 $cc -X -lm -o sqrt sqrt.c
 因为我们使用了数学库,所以要给链接器传递一个参数-lm。
 程序就完成了。
 我们想专业一点,给这个程序增加Make文件,不用再输入那么长的命令,同时还想让这个程序成为可移植的程序。这个时候,我们就需要用到autotools了。
 autotools包含了几个部分,最常用到的是autoconf和automake。
 我们先加入autoconf。
 autoconf需要一个configure.ac文件,幸运的是,我们不需要自己写这个文件,我们可以使用autoscan来生成这个文件。执行autoscan。
 $autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
错误信息先不用管,目录下多了几个文件: autoscan.log  configure.scan。我们要关心的是configure.scan,这是一个原始版本的configure.ac。打开这个文件,把下面这一行修改一下:
 AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
 改成
 AC_INIT([sqrt], [0.1.0], [you@mail.address])
参数的意思一目了然,不罗嗦了。接下来将文件另存为:configure.ac。执行:
 $autoconf
 我们看到,目录下又多了一些内容,我们关心的是configure这个脚本文件,执行一下试试吧!
 不过现在这个configure还没什么用,要发挥configure的真正目的——识别编译环境,配置编译选项的话,还要进行一些操作。

首先编辑configure.ac文件,在我们之前改动的AC_INIT...一行下面,加入如下一行内容:
AM_INIT_AUTOMAKE 
再执行一次autoconf试试?很不幸,我们遇到了错误:
configure.ac:6: error: possibly undefined macro: AM_INIT_AUTOMAKE...
因为找不到AM_INIT_AUTOMAKE宏,不要担心,因为我们少做了一步,先要把这些宏生成一下,当然是自动的。
$aclocal
$autoconf
现在的autoconf没有报错。这个时候再看看目录下面,发现多了一个aclocal.m4文件,这就是aclocal声称的宏命令文件,autoconf会使用它来生成新的configure脚本。
 是不是现在就能够自动搞定Makefile了?我们现在再执行一下configure,看看输出:
 configure: error: cannot find install-sh or install.sh in . ./.. ./../..
 和我们想的有点不同,我们还要用到automake命令做一些其它的事情,我们先执行一下:
 $automake
 configure.ac: required file `./install-sh' not found
 configure.ac: required file `./missing' not found
 automake: no `Makefile.am' found for any configure output
 我们注意到最后一行,知道了还需要一个`Makefile.am`文件,这个文件我们要写一下,编辑一个文件,增加:
 bin_PROGRAMS = sqrt
 sqrt_SOURCES = sqrt.c
 sqrt_LDADD = $(LIBOJBS)
 执行automake试试?
 configure.ac: required file `./install-sh' not found
configure.ac: required file `./missing' not found
automake: no `Makefile.am' found for any configure output
automake: Did you forget AC_CONFIG_FILES([Makefile]) in configure.ac?
 哦,不行,还要install-sh,missing文件,错误信息中,还提到AC_CONFIG_FILES([Makefile]),是的,我们还要修改一下configure.ac,在最后一行AC_OUTPUT前面增加一行:
 AC_CONFIG_FILES([Makefile])
 现在再执行一次automake吧,但是我们要加一个参数:
 $automake --add-missing
 configure.ac: installing `./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'
 configure.ac:8: required file `config.h.in' not found

 前面缺的四个文件简单,我们按照自己的情况编辑保存即可,config.h.in从哪里来呢? 现在让我们把config.h.in搞出来,这个要用到autoheader,我们执行命令:
 $autoheader
 config.h.in文件就生成好了。准备好了其它几个文本文件,我们再执行一次,这次不用加参数了,不过我们还要再执行一次autoconf,因为我们修改了configure.ac之后还没有执行过autoconf。
 $autoconf
 $automake
 我们再执行一次./configure:
 $./configure
 ...
 config.status: creating Makefile
 config.status: creating config.h
 ...
 让我们执行一下make吧。
 $make
 ...
 /home/nevernew/sqrt/sqrt.c:7: undefined reference to `sqrt'
 ...
 是因为我们没有把数学库加入链接,修改Makefile.am,将对应行修改为:
 sqrt_LDADD = $(LIBOBJS) -lm
 更新一下文件:
 $autoconf
 $automake
 $./configure
 $make
 $./sqrt

 

http://liuskysun.blog.163.com/blog/static/99812978201122454256231/

 

====================================================================================

 Makefile固然可以帮助make完成它的使命,但要承认的是,编写Makefile确实不是一件轻松的事,尤其对于一个较大的项目而言更是如此。那么,有没有一种轻松的手段生成Makefile而同时又能让我们享受make的优越性呢?本节要讲autotools系列工具正是为此而设的,它只需用户输入简单的目标文件、依赖文件、文件目录等就可以轻松地生成Makefile了,这无疑是广大用户的所希望的。另外,这些工具还可以完成系统配置信息的收集,从而可以方便地处理各种移植性的问题。也正是基于此,现在Linux上的软件开发一般都用autotools来制作Makefile。
   
autotools使用流程
   
   正如前面所言,autotools是系列工具,读者首先要确认系统是否装了以下工具(可以用which命令进行查看)。

·  aclocal

·  autoscan

·  autoconf

·  autoheader

·  automake

使用autotools主要就是利用各个工具的脚本文件以生成最后的Makefile。其总体流程是这样的:

·  使用aclocal生成一个“aclocal.m4”文件,该文件主要处理本地的宏定义;

·  改写“configure.scan”文件,并将其重命名为“configure.in”,并使用autoconf文件生成configure文件。

    接下来,将通过一个简单的hello.c例子来熟悉autotools生成makefile的过程.

1.autoscan

    它会在给定目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目录树中进行检查。它会搜索源文件以寻找一般的移植性问题并创建一个文件“configure.scan”,该文件就是接下来autoconf要用到的“configure.in”原型。如下所示:

 

[root@localhost automake]# autoscan

autom4te: configure.ac: no such file or directory

autoscan: /usr/bin/autom4te failed with exit status: 1

[root@localhost automake]# ls

autoscan.log  configure.scan  hello.c

 

    如上所示,autoscan首先会尝试去读入“configure.ac”(同configure.in的配置文件)文件,此时还没有创建该配置文件,于是它会自动生成一个“configure.in”的原型文件“configure.scan”。

2.autoconf

configure.in是autoconf的脚本配置文件,它的原型文件“configure.scan”如下所示:

 

#                                               -*- Autoconf -*-

# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)

#The next one is modified by sunq

#AC_INIT(FULL-PACKAGE-NAME,VERSION,BUG-REPORT-ADDRESS)

AC_INIT(hello,1.0)

# The next one is added by sunq

AM_INIT_AUTOMAKE(hello,1.0)

AC_CONFIG_SRCDIR([hello.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_CONFIG_FILES([Makefile])

AC_OUTPUT

 

下面对这个脚本文件进行解释:

·  以“#”号开始的行为注释。

·  AC_PREREQ宏声明本文件要求的autoconf版本,如本例使用的版本2.59。

·  AC_INIT宏用来定义软件的名称和版本等信息,在本例中省略了BUG-REPORT-ADDRESS,一般为作者的e-mail。

·  AM_INIT_AUTOMAKE是笔者另加的,它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号。

·  AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有

效性。在此处为当前目录下的hello.c。

·  AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

·  AC_CONFIG_FILES宏用于生成相应的Makefile文件。

·  中间的注释间可以添加分别用户测试程序、测试函数库、测试头文件等宏定义。

接下来首先运行aclocal,生成一个“aclocal.m4”文件,该文件主要处理本地的宏定义。如下所示:

 

[root@localhost automake]# aclocal

 

再接着运行autoconf,生成“configure”可执行文件。如下所示:

 

[root@localhost automake]# autoconf

[root@localhost automake]# ls

aclocal.m4  autom4te.cache  autoscan.log  configure  configure.in  hello.c

3.autoheader

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

 

[root@localhost automake]# autoheader

4.automake

    这一步是创建Makefile很重要的一步,automake要用的脚本配置文件是Makefile.am,用户需要自己创建这个文件。之后,automake工具将其转换成Makefile.in。
    在该例中,创建的文件为Makefile.am如下所示:

 

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS= hello

hello_SOURCES= hello.c hello.h

 

下面对该脚本文件的对应项进行解释。

·  其中的AUTOMAKE_OPTIONS为设置automake的选项。由于GNU(在第1章中已经有所介绍)对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING等,否则automake执行时会报错。automake提供了三种软件等级:foreign、gnu和gnits,让用户选择采用,默认等级为gnu。在本例使用foreign等级,它只检测必须的文件。

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

·  hello_SOURCES定义“hello”这个执行程序所需要的原始文件。如果”hello”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用空格隔开。例如:若目标体“hello”需要“hello.c”、“sunq.c”、“hello.h”三个依赖文件,则定义hello_SOURCES=hello.c sunq.c hello.h。
    接下来可以使用automake对其生成“configure.in”文件,在这里使用选项“—adding-missing”可以让automake自动添加有一些必需的脚本文件。如下所示:

 

[root@localhost automake]# automake --add-missing

configure.in: installing './install-sh'

configure.in: installing './missing'

Makefile.am: installing 'depcomp'

[root@localhost automake]# ls

aclocal.m4      autoscan.log  configure.in  hello.c     Makefile.am  missing

autom4te.cache  configure     depcomp    install-sh  Makefile.in  config.h.in

 

可以看到,在automake之后就可以生成configure.in文件。

5.运行configure

在这一步中,通过运行自动配置设置文件configure,把Makefile.in变成了最终的Makefile。如下所示:

 

[root@localhost automake]# ./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 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 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

    可以看到,在运行configure时收集了系统的信息,用户可以在configure命令中对其进行方便地配置.
在./configure的自定义参数有两种,一种是开关式(--enable-XXX或--disable-XXX),另一种是开放式,即后面要填入一串字符(--with-XXX=yyyy)参数。读者可以自行尝试其使用方法。另外,读者可以查看同一目录下的”config.log”文件,以方便调试之用。

到此为止,makefile就可以自动生成了。回忆整个步骤,用户不再需要定制不同的规则,而只需要输入简单的文件及目录名即可,这样就大大方便了用户的使用。

    autotools生成的Makefile除具有普通的编译功能外,还具有以下主要功能:
1.make

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

 

[root@localhost automake]# make

if Gcc -D PACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"hello\" -DVERSION=\"1.0\"  -I. -I.     -g -O2 -MT hello.o -MD -MP -MF ".deps/hello.Tpo" -c -o hello.o hello.c; \

then mv -f ".deps/hello.Tpo" ".deps/hello.Po"; else rm -f ".deps/hello.Tpo"; exit 1; fi

Gcc  -g -O2   -o hello  hello.o

此时在本目录下就生成了可执行文件“hello”,运行“./hello”能出现正常结果,如下所示:

 

[root@localhost automake]# ./hello

Hello everyone!

2.make install

此时,会把该程序安装到系统目录中去,如下所示:

 

[root@localhost automake]# make install

if Gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"hello\" -DVERSION=\"1.0\"  -I. -I.     -g -O2 -MT hello.o -MD -MP -MF ".deps/hello.Tpo" -c -o hello.o hello.c; \

then mv -f ".deps/hello.Tpo" ".deps/hello.Po"; else rm -f ".deps/hello.Tpo"; exit 1; fi

Gcc  -g -O2   -o hello  hello.o

make[1]: Entering directory '/root/workplace/automake'

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/workplace/automake'

 

此时,若直接运行hello,也能出现正确结果,如下所示:

 

[root@localhost automake]# hello

Hello everyone!

3.make clean

此时,make会清除之前所编译的可执行文件及目标文件(object file, *.o),如下所示:

 

[root@localhost automake]# make clean

test -z "hello" || rm -f hello

rm -f *.o

4.make dist

此时,make将程序和相关的文档打包为一个压缩文档以供发布,如下所示:

 

[root@localhost automake]# make dist

[root@localhost automake]# ls hello-1.0-tar.gz

hello-1.0-tar.gz

 

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

0 0
原创粉丝点击