Chinaunix首页 | 论坛 | 博客
  • 博客访问: 507795
  • 博文数量: 235
  • 博客积分: 1209
  • 博客等级: 少尉
  • 技术积分: 1417
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-19 19:59
文章分类

全部博文(235)

文章存档

2012年(107)

2011年(128)

分类:

2011-11-23 00:35:39

本文也是 automake/autoconf 入门 基础资料。
 
Linux下的进行程序开发的人员一定都遇到过编写Makefile文件的情况,毕竟在 Linux 系统下,用 C/C++ 进行开发的还是很多,使用make命令来编译自己写的程序确实是很方便。一般情况下,大家都是手工写一个简单Makefile,可是如果要想写出一个符合自由软件惯例的Makefile就不那么容易了。
其实我们编写一个用于跨平台的 Makefile 的好处是非常多的,它可以帮我们检查编译环境,避免我们开发的软件在跨平台编译的情况下,出现大量的编译问题。

其实生成 Makefile 文件主要用到的是 autoconfautomake这两个工具,他们可以帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常 见的GNU程序一样,只要使用“./configure”“make”“make instal”就可以把程序安装到Linux系统中去了。

一、Makefile介绍

Makefile
是用于自动编译和链接的,一个工程有很多文件组成,每一个文件的改变都会导致工程的重新链接,但是不是所有的文件都需要重新编译,Makefile中纪录有文件的信息,在make时会决定在链接的时候需要重新编译哪些文件。

Makefile
的宗旨就是:让编译器知道要编译一个文件需要依赖其他的哪些文件。当那些依赖文件有了改变,编译器会自动的发现最终的生成文件已经过时,而重新编译相应的模块。

Makefile
的基本结构不是很复杂,但当一个程序开发人员开始写Makefile时,经常会怀疑自己写的是否符合惯例,而且自己写的 Makefile经常和自己的开发环境相关联,当系统环境变量或路径发生了变化后,Makefile可能还要跟着修改。这样就造成了手工书写 Makefile的诸多问题,automake恰好能很好地帮助我们解决这些问题。

使用automake,程序开发人员只需要写一些 简单的含有预定义宏的文件,由autoconf根据一个宏文件生成configure,由automake根据另一个宏文件生成Makefile.in, 再使用configure依据Makefile.in来生成一个符合惯例的Makefile。下面我们将详细介绍Makefileautomake生成 方法。

二、使用的环境

本文所提到的程序是基于Linux发行版本:CentOS 6 x86_64,它包含了我们要用到的autoconfautomake

三、第一个例子: hello

hello.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "verison.h"

  5. int main ( void )
  6. {
  7.        int a = 6;
  8.        int b = 4;
  9.        fprintf(stdout,"%s\n",HELLO);
  10.        fprintf(stdout,"%d+%d = %d\n",a,b,add(a,b));
  11.        return 0;
  12. }

add.c

  1. /* ******************** *
  2. * add function return   *
  3. * a + b                 *
  4. * ********************* */
  5. int add(int a, int b)
  6. {
  7.         return (a+b);
  8. }

verison.h

  1. #ifndef __VERISON__
  2. #define __VERISON__
  3. //verison.h
  4. #define HELLO "Hello World"
  5. extern int add(int a , int b);
  6. #endif //__VERISON__


下面我们来看看,这个 configure 和 Makefile 文件是如何生成出来的。

我们需要用到的命令如下:
aclocal; autoconf; automake --add-missing;
现在开始介绍详细的过程:
step 1)
      使用autoscan命令来帮助我们根据目录下的源代码生成一个configure.in的模板文件。
      $ autoscan
      $ ls
      $ configure.scan hello.c add.c verison.h
      看到 configure.scan 文件没有,它就是我们生成configure.in的蓝本。
      现在将configure.scan拷贝成configure.in,并且编辑它,按下面的内容修改,去掉无关的语句:
 

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

AC_INIT(hello.c)

AM_INIT_AUTOMAKE(hello,1.0)

C_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

AC_CHECK_HEADERS([stdio.h stdlib.h string.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT(Makefile)

 step 2 )

      执行命令aclocalautoconf,生成aclocal.m4configure两个文件:
      $ aclocal
      $ ls
      $ aclocal.m4 configure.in hello.c add.c verison.h
      $ autoconf
      $ ls
      $ aclocal.m4 autom4te.cache configure configure.in hello.c add.c verison.h

      $ autoheader
      $ ls
      $ aclocal.m4 autom4te.cache configure configure.in hello.c add.c verison.h
  config.h.in

大家可以看到configure.in内容是一些宏定义,这些宏经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。

autoconf
是用来生成自动配置软件源代码脚本(configure)的工具。configure脚本能独立于autoconf运行,且在运行的过程中,不需要用户的干预。

要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4

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

autoconf
configure.in这个列举编译软件时所需要各种参数的模板文件中创建configure

autoconf
需要GNU m4宏处理器来处理aclocal.m4,生成configure脚本。

autoheaderconfigure.in这个列举编译软件时所需要各种参数的模板文件中创建config.g.in

m4是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,循环等。m4既可以作为编译器的前端,也可以单独作为一个宏处理器。

step 3)

     新建Makefile.am

     我们使用 vi 编辑器来编写一个Makefile.am文件,内容如下:

  1. AUTOMAKE_OPTIONS=foreign
  2. bin_PROGRAMS=hello
  3. hello_SOURCES=hello.c add.c verison.h
automake会根据你写的Makefile.am来自动生成Makefile.in

Makefile.am
中定义的宏和目标,会指导automake生成指定的代码。例如,宏bin_PROGRAMS将导致编译和连接的目标被生成。

step 4)

    运行automake,生成一系列的安装编译配置信息文件,如 install-sh、mkinstalldirs 等文件
    命令如下:

    $ automake --add-missing
    configure.in: installing `./install-sh'
    configure.in: installing `./mkinstalldirs'
    configure.in: installing `./missing'
    Makefile.am: installing `./depcomp'

    automake
会根据Makefile.am文件产生一些文件,包含最重要的Makefile.in

    主要的文件信息如下:

-rw-rw-r--. 1 work work  34611 11月 21 23:55 aclocal.m4
-rw-rw-r--. 1 work work     61 11月 21 01:05 add.c
drwxr-xr-x. 2 work work   4096 11月 21 23:55 autom4te.cache
-rw-rw-r--. 1 work work      0 11月 21 01:06 autoscan.log

-rw-rw-r--. 1 work work      2312 11月 21 01:06 config.h.in
-rwxrwxr-x. 1 work work 135392 11月 21 23:55 configure
-rw-rw-r--. 1 work work    438 11月 21 23:55 configure.in
-rw-rw-r--. 1 work work    495 11月 21 01:06 configure.scan
lrwxrwxrwx. 1 work work     32 11月 21 23:51 depcomp -> /usr/share/automake-1.11/depcomp
-rw-rw-r--. 1 work work    148 11月 21 01:16 hello.c
lrwxrwxrwx. 1 work work     35 11月 21 23:51 install-sh -> /usr/share/automake-1.11/install-sh
-rw-rw-r--. 1 work work     88 11月 21 23:49 Makefile.am
-rw-rw-r--. 1 work work  18876 11月 21 23:55 Makefile.in
lrwxrwxrwx. 1 work work     32 11月 21 23:51 missing -> /usr/share/automake-1.11/missing
-rw-rw-r--. 1 work work     72 11月 21 01:06 verison.h

   
    至此,我们的所有工作都已经完成了,测试一下看看 。 ^_^


    测试
    step 1)
执行configure生成Makefile

         $ ./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: executing depfiles commands

         $ ls -l Makefile
         -rw-rw-r--. 1 work work 18796 11月 22 00:27 Makefile

        我们可以看到,此时Makefile已经产生出来了。


   step 2)
使用Makefile编译代码

        $ make
          gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
          gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT add.o -MD -MP -MF .deps/add.Tpo -c -o add.o add.c
mv -f .deps/add.Tpo .deps/add.Po
          gcc  -g -O2   -o hello hello.o add.o 
          make[1]: Leaving directory `/home/work/temp/1'

   step 3)运行hello
        $ ./hello
        Hello,World!

        6+4 = 10

最后,我们针对上面的内容做一个总结:

1 autoscan

autoscan
是用来扫描源代码目录生成configure.scan 文件的。autoscan 可以用目录名做为参数,但如果你不使用参数的话,那么 autoscan 将认为使用的是当前目录。autoscan 将扫描你所指定目录中的源文件,并创建configure.scan 文件。

2
configure.scan

configure.scan
包含了系统配置的基本选项,里面都是一些宏定义。我们需要将它改名为configure.in

3
aclocal

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

4
autoconf

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

configure.in
文件的内容是一些宏,这些宏经过autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的shell 脚本。configure.in 文件中的宏的顺序并没有规定,但是你必须在所有宏的最前 面和最后面分别加上AC_INIT 宏和AC_OUTPUT 宏。

configure.in 中:

#
号表示注释,这个宏后面的内容将被忽略。

AC_INIT(FILE)

这个宏用来检查源代码所在的路径。

AM_INIT_AUTOMAKE(PACKAGE, VERSION)

这个宏是必须的,它描述了我们将要生成的软件包的名字及其版本号:PACKAGE 是软件包的名字,VERSION 是版本号。当你使用make dist 命令时,它会给你生成一个类似hello-1.0.tar.gz 的软件发行包,其中就有对应的软件包的名字和版本号。

AC_PROG_CC

这个宏将检查系统所用的C 编译器。

AC_OUTPUT(FILE)

这个宏是我们要输出的Makefile 的名字。

我们在使用automake 时,实际上还需要用到其他的一些宏,但我们可以用aclocal 来帮我们自动产生。执行aclocal 后我们会得到aclocal.m4 文件。

产生了configure.inaclocal.m4 两个宏文件后,我们就可以使用autoconf 来产生configure 文件了。

5
Makefile.am

Makefile.am
是用来生成Makefile.in 的,需要你手工书写。Makefile.am 中定义了一些内容:

AUTOMAKE_OPTIONS

这个是automake 的选项。在执行automake 时,它会检查目录下是否存在标准GNU 软件包中应具备的各种文件,例如AUTHORSChangeLogNEWS 等文件。我们将其设置成foreign 时,automake 会改用一般软件包的标准来检查。

bin_PROGRAMS

这个是指定我们所要产生的可执行文件的文件名。如果你要产生多个可执行文件,那么在各个名字间用空格隔开。

hello_SOURCES

这个是指定产生“hello” 时所需要的源代码。如果它用到了多个源文件,那么请使用空格符号将它们隔开。比如需要 hello.c add.c verison.h 那么请写成hello_SOURCES= verison.h hello.c add.c。

如果你在bin_PROGRAMS 定义了多个可执行文件,则对应每个可执行文件都要定义相对的filename_SOURCES

6
automake

我们使用automake --add-missing 来产生Makefile.in

选项--add-missing 的定义是“add missing standard files to package” ,它会让automake 加入一个标准的软件包所必须的一些文件。

我们用automake 产生出来的Makefile.in 文件是符合GNU Makefile 惯例的,接下来我们只要执行configure 这个shell 脚本就可以产生合适的 Makefile 文件了。

7
Makefile

在符合GNU Makefiel 惯例的Makefile 中,包含了一些基本的预先定义的操作:

make

根据Makefile 编译源代码,连接,生成目标文件,可执行文件。

make clean

清除上次的make 命令所产生的object 文件(后缀为“.o” 的文件)及可执行文件。

make install

将编译成功的可执行文件安装到系统目录中,一般为/usr/local/bin 目录。

make dist

产生发布软件包文件(即distribution package )。这个命令将会将可执行文件及相关文件打包成一个tar.gz 压缩的文件用来作为发布软件的软件包。

它会在当前目录下生成一个名字类似“PACKAGE-VERSION.tar.gz” 的文件。PACKAGEVERSION ,是我们在configure.in 中定义的AM_INIT_AUTOMAKE(PACKAGE, VERSION)

make distcheck

生成发布软件包并对其进行测试检查,以确定发布包的正确性。这个操作将自动把压缩包文件解开,然后执行configure 命令,并且执行make ,来确认编译不出现错误,最后提示你软件包已经准备好,可以发布了。

===============================================
hello-1.0.tar.gz is ready for distribution
===============================================
make distclean

类似make clean ,但同时也将configure 生成的文件全部删除掉,包括Makefile

结束语

通过上面的介绍,大家应该可以很容易地生成一个你自己的符合GNU惯例的Makefile文件及对应的项目文件。
如果你想写出更复杂的且符合惯例的Makefile,你可以参考一些开放代码的项目中的configure.inMakefile.am文件

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