现在的位置: 首页 > 综合 > 正文

linux环境下利用autotools工具自动生成make file 运用Autoconf和Automake生成Makefile的学习之路 运用Autoconf和Automake生成Makefile的学习之路

2013年06月08日 ⁄ 综合 ⁄ 共 23272字 ⁄ 字号 评论关闭

作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便。一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Makefile就不那么容易了。

  在本文中,将给大家介绍如何使用 autoconfautomake两个工具来帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常见的GNU程序一样,只要使用“./configure”“make”“make
install”
就可以把程序安装到Linux系统中去了。这将特别适合想做开放源代码软件的程序开发人员,又或如果你只是自己写些小的Toy程序,那么这个文章对你也会有很大的帮助。

编译一个简单的源文件main.c,需要自动生成一个makefile,以下是步骤:

第一步:

----------

在/root/project/main目录下创建一个文件main.c,其内容如下:

------------------------------------------------

#include <stdio.h>

int main(int argc, char** argv)

{

    printf("Hello, Auto Makefile!\n");

    return 0;

}

------------------------------------------------

此时状态如下:

[root@localhost main]# pwd

/root/project/main

[root@localhost main]# ls

main.c

[root@localhost main]#

第二步:

----------

运行 autoscan , 自动创建两个文件: autoscan.log configure.scan

此时状态如下:

[root@localhost main]# autoscan

[root@localhost main]# ls

autoscan.log configure.scan main.c

[root@localhost main]#

第三步:

----------

修改configure.scan的文件名为configure.in

查看configure.in的内容:

------------------------------------------------

#                                               -*- Autoconf -*-

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

AC_PREREQ(2.61)

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

------------------------------------------------

解读以上的文件:

------------------------------------------------

#                                               -*- Autoconf -*-

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

# AC_PREREQ:

# 确保使用的是足够新的Autoconf版本。如果用于创建configure的Autoconf的版

# 本比version 要早,就在标准错误输出打印一条错误消息并不会创建configure。

AC_PREREQ(2.61)

#

# 初始化,定义软件的基本信息,包括设置包的全称,版本号以及报告BUG时需要用的邮箱地址

#

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

#

# 用来侦测所指定的源码文件是否存在,来确定源码目录的有效性

#

AC_CONFIG_SRCDIR([main.c])

#

# 用于生成config.h文件,以便autoheader使用

#

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.

#

# 创建输出文件。在`configure.in'的末尾调用本宏一次。

#

AC_OUTPUT

------------------------------------------------

修改动作:

    1.修改AC_INIT里面的参数: AC_INIT(main,1.0, pgpxc@163.com)

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

    3.在AC_OUTPUT后添加输出文件Makefile

修改后的结果:

------------------------------------------------

#                                               -*- Autoconf -*-

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

AC_PREREQ(2.61)

AC_INIT(main, 1.0, pgpxc@163.com)

AC_CONFIG_SRCDIR([main.c])

AC_CONFIG_HEADER([config.h])

AM_INIT_AUTOMAKE(main,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_OUTPUT([Makefile])

------------------------------------------------

第四步:

运行 aclocal, 生成一个“aclocal.m4”文件和一个缓冲文件夹autom4te.cache,该文件主要处理本地的宏定义。

此时的状态是:

[root@localhost main]# aclocal

[root@localhost main]# ls

aclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ main.c

[root@localhost main]#

第五步:

运行 autoconf, 目的是生成 configure

此时的状态是:

[root@localhost main]# autoconf

[root@localhost main]# ls

aclocal.m4      autoscan.log configure.in   main.c

autom4te.cache configure     configure.in~

[root@localhost main]#

第六步:

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

此时的状态是:

[root@localhost main]# autoheader

[root@localhost main]# ls

aclocal.m4      autoscan.log configure     configure.in~

autom4te.cache config.h.in   configure.in main.c

[root@localhost main]#

第七步:

下面即将运行 automake, 但在此之前应该做一下准备工作!

首先

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

这个Makefile.am的内容如下:

------------------------------------------------

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=main

main_SOURCES=main.c

------------------------------------------------

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

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

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

    main_SOURCES定义“main”这个执行程序所需要的原始文件。如果”main”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原 始文件都列出来,并用空格隔开。例如:若目标体“main”需要“main.c”、“sunq.c”、“main.h”三个依赖文件,则定义 main_SOURCES=main.c sunq.c main.h。要注意的是,如果要定义多个执行文件,则对每个执行程序都要定义相应的file_SOURCES。

其次

使用automake对其生成“configure.in”文件,在这里使用选项“—adding-missing”可以让automake自动添加有一些必需的脚本文件。

运行后的状态是:

------------------------------------------------

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

configure.in:8: installing `./missing'

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

Makefile.am: installing `./depcomp'

[root@localhost main]# ls

aclocal.m4      config.h.in   configure.in~ main.c        Makefile.in

autom4te.cache configure     depcomp        Makefile.am missing

autoscan.log    configure.in install-sh     Makefile.am~

[root@localhost main]#

------------------------------------------------

第八步

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

运行的结果如下:

------------------------------------------------

[root@localhost main]# ./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: executing depfiles commands

[root@localhost main]# ls

aclocal.m4      config.h.in    configure.in   main.c        Makefile.in

autom4te.cache config.log     configure.in~ Makefile     missing

autoscan.log    config.status depcomp        Makefile.am  stamp-h1

config.h        configure     
install-sh
     Makefile.am~

[root@localhost main]#

------------------------------------------------

第九步

运行 make,对配置文件Makefile进行测试一下

此时的状态如下:

------------------------------------------------

[root@localhost main]# make

cd . && /bin/sh /root/project/main/missing --run aclocal-1.10

cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign

cd . && /bin/sh /root/project/main/missing --run autoconf

/bin/sh ./config.status --recheck

running CONFIG_SHELL=/bin/sh /bin/sh ./configure   --no-create --no-recursion

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

/bin/sh ./config.status

config.status: creating Makefile

config.status: creating config.h

config.status: config.h is unchanged

config.status: executing depfiles commands

cd . && /bin/sh /root/project/main/missing --run autoheader

rm -f stamp-h1

touch config.h.in

make all-am

make[1]: Entering directory `/root/project/main'

gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c

mv -f .deps/main.Tpo .deps/main.Po

gcc -g -O2   -o main main.o

cd . && /bin/sh ./config.status config.h

config.status: creating config.h

config.status: config.h is unchanged

make[1]: Leaving directory `/root/project/main'

[root@localhost main]# ls

aclocal.m4      autoscan.log config.h.in config.status configure.in   depcomp    main    main.o    Makefile.am   Makefile.in stamp-h1

autom4te.cache config.h      config.log   configure      configure.in~ install-sh main.c Makefile Makefile.am~ missing

[root@localhost main]#

------------------------------------------------

第十步

运行生成的文件 main:

------------------------------------------------

[root@localhost main]# ./main

Hello, Auto Makefile!

http://blog.csdn.net/dengzhilong_cpp/article/details/7486791

使用automake自动生成makefile的过程主要有八个步骤:

1、建立好源文件以后到源文件所在目录

2、autoscan命令 将configure.scan文件修改为configure.in
          修改configure.in文件中的内容:
               AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)修改为AC_INIT(main, 1.0, pgpxc@163.com)
        在AC_CONFIG_HEADER([config.h])后面添加AM_INIT_AUTOMAKE(main,1.0)   
          在最后添加AC_OUTPUT([Makefile])
3、运行aclocal
4、运行autoconf
5、运行autoheader
6、创建Makefile.am文件,内容为
     AUTOMAKE_OPTIONS=foreign

     bin_PROGRAMS=main 如果有多个用空格分开

     main_SOURCES=main.c 定义main所需源文件,多个可执行文件分别定义
7、运行automake --add-missing
8、运行./configure
9、运行make

在第六步中需要自己写Makefile.am文件,特别是其中的main_SOURCES需要把生成main所以来的文件都包含进来。并且那些间接依赖的文件也需要包含进来。比如说我有三个文件:main.cpp Add.cpp Add.h  Num.h Num.cpp其中在main.cpp中包含了Add.h  在Add.cpp中包含了Num.h这样在完成main的依赖文件时就需要包含以上所有的问个文件main.cpp
Add.cpp Add.h  Num.h Num.cpp
才可以。

http://blog.csdn.net/dengzhilong_cpp/article/details/7487224

运用Autoconf和Automake生成Makefile的学习之路

http://blog.csdn.net/dengzhilong_cpp/article/details/7487243

前言:

       这次task,我大概用了4天的时间去完成。四天的时间内,我不停地去查资料,不停的去做小Demo,不停的总结,终于做完了这次的作业。下面的内容记录了我做这次Makefile作业的学习之路。

 

一、    相关概念的介绍

什么是Makefile?怎么书写Makefile?竟然有工具可以自动生成Makefile?怎么生成啊?开始的时候,我有这么多疑问,所以,必须得先把基本的概念搞个清楚。

 

1. Makefile

  makefile用来定义整个工程的编译规则。一个工程中的源文件计数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为 makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。

  makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。make是一个命令工具,是一个解释makefile中指令的命令工具,一般来说,大多数的IDE都有这个命令,比如:Delphi的make,Visual C++的nmake,Linux下GNU的make。可见,makefile都成为了一种在工程方面的编译方法。

 

2. Autoconf

       Autoconf是一个用于生成可以自动地配置软件源代码包以适应多种Unix类系统的 shell脚本的工具。由Autoconf生成的配置脚本在运行的时候与Autoconf是无关的, 就是说配置脚本的用户并不需要拥有Autoconf。

       对于每个使用了Autoconf的软件包,Autoconf从一个列举了该软件包需要的,或者可以使用的系统特征的列表的模板文件中生成配置脚本。在shell代码识别并响应了一个被列出的系统特征之后,Autoconf允许多个可能使用(或者需要)该特征的软件包共享该特征。 如果后来因为某些原因需要调整shell代码,就只要在一个地方进行修改; 所有的配置脚本都将被自动地重新生成以使用更新了的代码。

 

3. Automake

       Automake是一个从文件`Makefile.am'自动生成`Makefile.in' 的工具。每个`Makefile.am'基本上是一系列make的宏定义 (make规则也会偶尔出现)。生成的`Makefile.in'服从GNU Makefile标准。GNU Makefile标准文档长、复杂,而且会发生改变。Automake的目的就是解除个人GNU维护者维护Makefile的负担 (并且让Automake的维护者来承担这个负担)。典型的Automake输入文件是一系列简单的宏定义。处理所有这样的文件以创建
`Makefile.in'。在一个项目(project)的每个目录中通常包含一个 `Makefile.am'。Automake在几个方面对一个项目做了限制;例如它假定项目使用Autoconf并且对`configure.in'的内容施加了某些限制。

       Automake支持三种目录层次: “flat”、“shallow”和“deep”。一个flat(平)包指的是所有文件都在一个目录中的包。为这类包提供的`Makefile.am' 缺少宏SUBDIRS。这类包的一个例子是termutils。一个deep(深)包指的是所有的源代码都被储存在子目录中的包;顶层 目录主要包含配置信息。GNU cpio 是这类包的一个很好的例子,GNU tar也是。deep包的顶层`Makefile.am'将包括
宏SUBDIRS,但没有其它定义需要创建的对象的宏。一个shallow(浅)包指的是主要的源代码储存在顶层目录中,而 各个部分(典型的是库)则储存在子目录中的包。Automake本身就是这类包(GNU make也是如此,它现在已经不使用automake)。

 

下面,就以这三种目录层次结构给大家介绍

二、    Flat目录结构:

1. 目录结构:

Helloworld

|-mytest.h

|-mytest.c

|-mymain.c

  顶级目录helloworld,该目录下存在三个文件。mytest.h头文件声明了sayhello()方法;mytest.c中实现了sayhello()方法;mymain.c中的main调用了sayhello()方法。

2. 执行步骤:

2.1. Autoscan

   在helloworld目录下执行autoscan命令,其中生成一个configure.scan的文件。

2.2. 将configure.scan文件更名为configure.in文件

2.3. 打开configure.in文件,修改文件内容

复制代码
 1 #                                               -*- Autoconf -*-
 2 # Process this file with autoconf to produce a configure script.
 3 
 4 #AC_INIT([2.68])
 5 AC_INIT([hello], [1.0], [**@126.com])
 6 AC_CONFIG_SRCDIR([mymain.c])
 7 #AC_CONFIG_HEADERS([config.h])
 8 
 9 AM_INIT_AUTOMAKE(hello, 1.0)
10 
11 # Check for programs
12 AC_PROG_CC
13 
14 # Check for libraries
15 # Check for header files
16 # Check for typedefs, structures, and compiler characteristics.
17 # Check for library functions.
18 
19 AC_OUTPUT(Makefile)
复制代码

2.4. 然后分别执行以下两个命令:

aclocal

autoconf

2.5. 在helloworld文件夹下创建一个名为Makefile.am的文件,并输入一下内容:   

1 AUTOMAKE_OPTIONS=foreign
2 bin_PROGRAMS=hello
3 hello_SOURCES=mymain.c mytest.c mytest.h

2.6. 执行命令“automake --add-missing”,automake 会根据Makefile.am 文件产生一些文件,其中包含最重要的Makefile.in

2.7. 执行“./configure”命令生成Makefile文件

2.8. 执行“make”命令来编译hello.c程序,从而生成可执行程序hello。生成可执行程序hello后,执行“./hello”。

 

 哈哈,一定看到你想要的结果了吧。

 

三、    shallow目录结构

1. 目录结构

helloworld

|-mymain.c

|head

||-mytest.h

||-mytest.c

   顶级目录helloworld,该目录下存在一个主文件mymain.c和一个目录head。head目录中,mytest.h头文件声明了sayhello()方法;mytest.c中实现了sayhello()方法;mymain.c中的main调用了sayhello()方法。

2. 执行步骤:

2.1. 在顶层目录下运行autoscan产生configure.scan文件

2.2. 将configure.scan文件更名为configure.in文件

2.3. 打开configure.in文件,修改文件内容

复制代码
 1 #                                               -*- Autoconf -*-
 2 # Process this file with autoconf to produce a configure script.
 3  
 4 #AC_INIT([2.68])
 5 AC_INIT([hello], [1.0], [**@126.com])
 6 AC_CONFIG_SRCDIR([mymain.c])
 7 #AC_CONFIG_HEADERS([config.h])
 8  
 9 AM_INIT_AUTOMAKE(hello, 1.0)
10  
11 # Check for programs
12 AC_PROG_CC
13 #使用静态库编译,需要此宏定义
14 AC_PROG_RAMLIB
15 
16 # Check for libraries
17 # Check for header files
18 # Check for typedefs, structures, and compiler characteristics.
19 # Check for library functions.
20 
21 AC_OUTPUT(Makefile head/Makefile)
复制代码

2.4. 然后分别执行以下两个命令:

aclocal

autoconf

2.5. 在head文件夹下创建Makefile.am文件,内容如下:

1 AUTOMAKE_OPTIONS=foreign
2 noinst_LIBRARIES=libmytest.a
3 libmytest_a_SOURCES=mytest.h mytest.c

2.6. 在helloworld文件夹下创建Makefile.am文件,内容如下:

1 AUTOMAKE_OPTIONS=foreign
2 SUBDIRS=head
3 bin_PROGRAMS=hello
4 hello_SOURCES=mymain.c
5 hello_LDADD=head/mytest.a

2.7. 执行命令“automake –add-missing”,automake会根据Makefile.am 文件产生一些文件,其中包含最重要的Makefile.in

2.8. 执行“./configure”命令生成Makefile文件

2.9. 执行“make”命令来编译hello.c程序,从而生成可执行程序hello。生成可执行程序hello后,执行“./hello”。

 

哈哈,shallow的目录结构也搞定了哦~~

 

四、    Deep目录结构

1. 目录结构

helloworld

|head

||-mytest.h

||-mytest.c

|src

||-mymain.c

 

  顶级目录helloworld,该目录下存在两个目录src和head。Head目录中,mytest.h头文件声明了sayhello()方法;mytest.c中实现了sayhello()方法;src目 录中的mymain.c中的main调用了sayhello()方法。

2. 执行步骤

2.1.  在顶层目录下运行autoscan产生configure.scan文件

2.2.  将configure.scan文件更名为configure.in文件

2.3.  打开configure.in文件,修改文件内容

复制代码
 1 #                                               -*- Autoconf -*-
 2 # Process this file with autoconf to produce a configure script.
 3  
 4 #AC_INIT([2.68])
 5 AC_INIT([hello], [1.0], [**@126.com])
 6 AC_CONFIG_SRCDIR([src/mymain.c])
 7 #AC_CONFIG_HEADERS([config.h])
 8  
 9 AM_INIT_AUTOMAKE(hello, 1.0)
10  
11 # Check for programs
12 AC_PROG_CC
13 #使用静态库编译,需要此宏定义
14 AC_PROG_RAMLIB
15 
16 # Check for libraries
17 # Check for header files
18 # Check for typedefs, structures, and compiler characteristics.
19 # Check for library functions.
20 
21 AC_OUTPUT(Makefile head/Makefile src/Makefile)
复制代码

2.4.  然后分别执行以下两个命令: 

aclocal

autoconf

2.5.  在head文件夹下创建Makefile.am文件,内容如下:

1 AUTOMAKE_OPTIONS=foreign
2 noinst_LIBRARIES=libmytest.a
3 libmytest_a_SOURCES=mytest.h mytest.c

2.6.  在src文件夹下创建Makefile.am文件,内容如下:

1 AUTOMAKE_OPTIONS=foreign
2 bin_PROGRAMS=hello
3 hello_SOURCES=mymain.c
4 hello_LDADD=../head/libmytest.a

2.7.  在helloworld文件夹下创建Makefile.am文件,内容如下:

1 AUTOMAKE_OPTIONS=foreign
2 SUBDIRS=head src

2.8.  执行命令“automake –add-missing”,automake会根据Makefile.am 文件产生一些文件,其中包含最重要的Makefile.in

2.9.  执行“make”命令来编译hello.c程序,从而生成可执行程序hello。生成可执行程序hello后,执行“./hello”。

 

 哈哈,deep目录下的编译与链接也搞定了!

 

五、    总结:

归纳一下以上所有例子的流程:

(1)在存放源代码的顶层目录下执行autoscan命令生成configure.scan文件。

(2)将configure.scan文件改名为configure.in,并对其默认配置进行修改。

(3)执行aclocal、autoconf两个命令,分别生成aclocal.m4、configure文件。

(4)在每个目录下创建一个名为Makefile.am的文件,并输入相应的内容。

(5)执行automake --add-missing,它根据Makefile.am文件,生成Makefile.in。

(6)执行./configure脚本文件,它根据Makefile.in文件,生成最终的Makefile文件。

(7)生成Makefile之后,执行“make”编译工程并且生成可执行程序。

autoconf,automake生成makefile的流程

 

 














六、    能力进阶

以上的的程序还只是处于初级阶段,并且生成的是 静态库 。我们可以发现,用autoconf和automake生成Makefile的关键在于configure.in和Makefile.am的文件的书写。所以,要想使自己的功力更上一层,需要熟悉autoconf和automake这两个工具的使用,其中有很多重要的宏需要我们了解。这里时具体的参考手册:

autoconf手册

       英文版:http://www.gnu.org/software/autoconf/manual/autoconf.html

       中文版:http://www.linuxforum.net/books/autoconf.html

automake手册

       中文版:http://www.linuxforum.net/books/automake.html

 

七、    Configure.in文件解析

autoconf是用来产生“configure”文件的工具。“configure”是一个Shell脚本,它可以自动设定一些编译参数使程序能够在不同平台上进行编译。autoconf读取configure.in 文件然后产生’configure’这个Shell脚本。

configure.in 文件的内容是一系列GNU m4 的宏,这些宏经autoconf处理后会变成检查系统特性的Shell脚本。configure.in文件中宏的顺序并没有特别的规定,但是每一个configure.in 文件必须以宏AC_INIT开头,以宏AC_OUTPUT结束。一般可先用autoscan这个工具扫描原始文件以产生一个configure.scan 文件,再对configure.scan 作些修改,从而生成 configure.in 文件。

 

configure.in 文件中一些宏的含义如下

#或dnl

#或dnl后面的内容作为注释不会被处理,它们是注释的起始标志

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

 

AM_INIT_AUTOMAKE(PACKAGE,VERSION)

这个是后面运行automake命令所必需的宏,PACKAGE指明要产生软件的名称,VERSION 是其版本号

AC_PROG_CC

检查系统可用的C编译器,若源代码是用C语言编写的就需要这个宏

AC_OUTPUT(FILE)

设置configure命令所要产生的文件。我们最终期望产生Makefile

这个文件,因此一般将其设置为AC_OUTPUT(Makefile)

 

在运行automake命令时,还需要一些其他的宏,这些额外的宏由aclocal产生。执行aclocal会产生aclocal.m4文件,如果没有特别的要求,无需修改它。用 aclocal产生的宏将会提示automake如何动作。

 

另一个重要的文件是Makefile.am。automake根据configure.in中的宏并在perl的帮助下把Makefile.am转成Makefile.in文件。Makefile.am 文件定义所要产生的目标。

 

八、    Makefile.am

 

 

 

 

  对于可执行文件和静态库类型,如果只想编译,不想安装到系统中,可以用noinst_PROGRAMS代替bin_PROGRAMS,noinst_LIBRARIES代替lib_LIBRARIES。

 

automake设置了默认的安装路径:

1) 标准安装路径

默认安装路径为:$(prefix) = /usr/local,可以通过./configure --prefix=<new_path>的方法来覆盖。

其它的预定义目录还包括:bindir = $(prefix)/bin, libdir = $(prefix)/lib, datadir = $(prefix)/share, sysconfdir = $(prefix)/etc等等。

2) 定义一个新的安装路径

比如test, 可定义builddir = $(prefix)/build, 然后test_LIBRARIES =mytest.h mytest.c,则mytest.h mytest.c 会作为静态库安装到$(prefix)/build目录下。

 

九、    如何使用产生的Makefile文件

执行configure脚本文件所产生的Makefile文件有几个预定的选项可供使用:

make all:产生设定的目标,即生成所有的可执行文件。使用make也可以达到此目的。

make clean:删除之前编译时生成的可执行文件及目标文件(形如*.o的中间文件)。

make distclean:除了删除可执行文件和目标文件以外,把configure所产生的 Makefile文件也清除掉。通常在发布软件前执行该命令。

make install:将使用make all或make命令产生的可执行文件以软件的形式安装到系统中。若使用bin_PROGRAMS宏,程序将会被安装到 /usr/local/bin下,否则安装到预定义的目录下。

make dist:将程序和相关的文档包装为一个压缩文档以供发布。执行完该命令,在当前目录下会产生一个名为PACKAGE-VERSION.tar.gz的文件。PACKAGE 和 VERSION 这两个参数是来自configure.in文件中的AM_INIT_AUTOMAKE(PACKAGE,

VERSION)。如在上个例子中执行make dist命令,会产生名为“hello-1.0.tar.gz”的文件。

make distcheck:与make dist类似,但是加入了检查包装以后的压缩文件是否正常。

 

十、    动态库编译

需要在Makefile.am中指定:

lib_LTLIBRARIES=libhello.la

libhello_la_SOURCES=mytest.h mytest.c

在根目录下的configure.in中加AC_PROG_LIBTOOL

 

动态库编译之前,需要安装libtool工具:apt-get install libtool。

若出现:“required file `./ltmain.sh' not found”错误,是因为libtool的配置问题。

解决方法:

$libtoolize --automake --debug --copy –force

运用Autoconf和Automake生成Makefile的学习之路

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

make file全部手工编写对于较大的工程来说应该是个噩梦来,还好有和我们一样的懒人开发来automake 工具进行make file的自动生成。
对于make file的原理,大家可以看下陈皓写的比较经典的《跟我一起写make file》

本机用的实验平台为 ubuntu 11.10,对于其他linux发行版应该不会有太大区别。

首先在$HOME 目录下建立一个文件夹 main。编写一段测试的代码如下,同时保存为main.cpp文件:

#include <iostream>
using namespace std;

int main(void)
{
    cout<<"Hello World automake!"<<endl;
    return 0;
}

接下来我们分N步来自动建立makefile文件。

第一步:运行 autoscan , 自动创建两个文件: autoscan.log configure.scan

此时状态如下:
nash635@ubuntu:~/main$ autoscan
nash635@ubuntu:~/main$ ls
autoscan.log  configure.scan  main.cpp
nash635@ubuntu:~/main$

第二步:修改configure.scan的文件名为configure.in
nash635@ubuntu:~/main$ mv configure.scan configure.in
nash635@ubuntu:~/main$

此时configure.in文件的内容如下:

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

AC_PREREQ([2.68])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([main.cpp])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

关键部分注释如下:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
# 确保使用的是足够新的Autoconf版本。如果用于创建configure的Autoconf的版
# 本比version 要早,就在标准错误输出打印一条错误消息并不会创建configure。
AC_PREREQ([2.68])
#
# 初始化,定义软件的基本信息,包括设置包的全称,版本号以及报告BUG时需要用的邮箱地址
#
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
#
# 用来侦测所指定的源码文件是否存在,来确定源码目录的有效性
#
AC_CONFIG_SRCDIR([main.cpp])
#
# 用于生成config.h文件,以便autoheader使用
#
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
#
# 创建输出文件。在`configure.in’的末尾调用本宏一次。
#
AC_OUTPUT

 

需要修改的部分如下:
1.修改AC_INIT里面的参数: AC_INIT(main,1.0, nash635@gmail.com)
2.添加宏AM_INIT_AUTOMAKE, 它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号。
3.在AC_OUTPUT后添加输出文件Makefile

修改后的部分如下:

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

AC_PREREQ([2.68])
AC_INIT(main, 1.0, nash635@gmail.com)
AC_CONFIG_SRCDIR([main.cpp])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(main,1.0)
# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT([Makefile])

 

第三步: 运行 aclocal, 生成一个“aclocal.m4”文件和一个缓冲文件夹autom4te.cache,该文件主要处理本地的宏定义。

此时的状态是:
nash635@ubuntu:~/main$ aclocal
nash635@ubuntu:~/main$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.in  configure.in~  main.cpp
nash635@ubuntu:~/main$

第四步:运行 autoconf, 目的是生成 configure

此时的状态是:
nash635@ubuntu:~/main$ autoconf
nash635@ubuntu:~/main$ ls
aclocal.m4      autoscan.log  configure.in   main.cpp
autom4te.cache  configure     configure.in~
nash635@ubuntu:~/main$

第五步:运行 autoheader,它负责生成config.h.in文件。该工具通常会从“acconfig.h”文件中复制用户附加的符号定义,因此此处没有附加符号定义,所以不需要创建“acconfig.h”文件。

此时的状态是:
nash635@ubuntu:~/main$ autoheader
nash635@ubuntu:~/main$ ls
aclocal.m4      autoscan.log  configure     configure.in~
autom4te.cache  config.h.in   configure.in  main.cpp
nash635@ubuntu:~/main$

第六步:下面即将运行 automake, 但在此之前应该做一下准备工作!

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

这个Makefile.am的内容如下:
————————————————
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=main.cpp
————————————————

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

其次,使用automake对其生成“configure.in”文件,在这里使用选项“—adding-missing”可以让automake自动添加有一些必需的脚本文件。
运行后的状态是:
————————————————
nash635@ubuntu:~/main$ automake –add-missing
configure.in:8: installing `./install-sh’
configure.in:8: installing `./missing’
Makefile.am: installing `./depcomp’
nash635@ubuntu:~/main$ ls
aclocal.m4      config.h.in   configure.in~  main.cpp      Makefile.in
autom4te.cache  configure     depcomp        Makefile.am   missing
autoscan.log    configure.in  install-sh     Makefile.am~
nash635@ubuntu:~/main$
————————————————

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

nash635@ubuntu:~/main$ ./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 g++… g++
checking whether the C++ compiler works… yes
checking for C++ compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
checking for suffix of object files… o
checking whether we are using the GNU C++ compiler… yes
checking whether g++ accepts -g… yes
checking for style of include used by make… GNU
checking dependency style of g++… gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
nash635@ubuntu:~/main$

 

第八步:运行 make,对配置文件Makefile进行测试一下

此时的状态如下:

nash635@ubuntu:~/main$ make
make  all-am
make[1]: Entering directory `/home/nash635/main’
g++ -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
mv -f .deps/main.Tpo .deps/main.Po
g++  -g -O2   -o main main.o
make[1]: Leaving directory `/home/nash635/main’
nash635@ubuntu:~/main$

第九步:运行生成的文件 main:
nash635@ubuntu:~/main$ ./main
Hello World automake!
nash635@ubuntu:~/main$

OK,这样我们就成功的利用automake工具生成了这个简单工程的make file,可能你要说,手写个make file也没这么费劲。

可是你要想一下,如果你的工程很大,有很多的文件的话,这个自动化的过程会不会给你省下很多时间?

下面利用一张图片来总结一下automake的工作过程吧:

抱歉!评论已关闭.