Autotools学习

来源:互联网 发布:爱心机构手机网站源码 编辑:程序博客网 时间:2024/05/21 02:21

      我们通常用诸如 "./configure", "make","make install" 等命令就可以把源码包安装到系统中,但是背后的原理是什么呢?当我们需要修改源码,加入自己的代码后如何修改呢?所以需要学习如何利用 GNU Autoconf 及 Automake 这两套工具来自动产生 Makefile文件。下面的内容综合了一些网上找到的资源。

先来一个例子

1.系统中需要安装 autosan , acloal , automake, autoconf , make 等工具,根据错误提示来进行安装。
2.进入工作目录,建立 include src两个目录放头文件和主文件:   include/util.h ,    src/main.c 测试。

#ifndef UTIL_H
#define UTIL_H

#include <stdio.h>
void myprint(char *str);

#endif
----------
#include "util.h"

void myprint(char *str){
    printf("HELLO===%s===WORLD\n",str);
}

int main(){
    char *s = "vonzhou";
    myprint(s);
}
 
3.利用命令autoscan 生成configure.ac  ( configure.ac是automake的输入文件)。会生成 autoscan.log  configure.scan 俩文件。
4. 将configure.scan 重命名为 configure.ac 并做修改。(configure.in是旧版本的写法)
(1)AC_INIT([hello], [0.1], [von@163.com]) 分别是 程序的名称,版本号,汇报bug的地址;
(2)添加 AM_INIT_AUTOMAKE([hello],[0.1]) ,没有参数的话,就用上面默认的;
(3)添加 AC_CONFIG_FILES([Makefile]) 指明输出文件的名称。内容如下:
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
AC_INIT(hello, 0.1, von@163.com)
AM_INIT_AUTOMAKE(hello,0.1)

AC_CONFIG_SRCDIR([include/util.h])
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

5. 执行aclocal 会根据 configure.ac 生成aclocal.m4 和目录 autom4te.cache。
6. 构建 Makefile.am 文件,定义了比Makefile 更高层次的规则。内容是:

#Makefile.am

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=include/util.h src/main.c
hello_CPPFLAGS=-I include/

7. 执行 autoheader 生成 config.h config.h.in 。
8.执行 automake --add-missing 会由Makefile.am 生成 Makefile.in 文件。
9.执行 autoconf 生成 configure 可执行文件。
10. 最后就是熟悉的 ./configure 生成 Makefile 文件。
11. make 就会生成我们的可执行文件 hello。


每个命令的作用

1. autoscan 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是configure.ac的一个雏形。
The autoscan program can help you create and/or maintain a configure.ac file for a software package.  autoscan examines source files in the directory tree rooted at a directory given as a command line argument, or the current directory if none is given. It searches the source files for common portability problems and creates a  file  configure.scan  which is a preliminary configure.ac for that package, and checks a possibly existing configure.ac for completeness.

2. aclocal :根据已经安装的宏,用户定义宏和acinclude.m4文件中的宏将configure.ac文件所需要的宏集中定义到文件 aclocal.m4中。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”

3. autoheader(autoconf): 根据configure.ac中的某些宏,比如cpp宏定义,运行m4,生成 config.h.in。

4.automake: automake将Makefile.am中定义的结构建立Makefile.in,然后configure脚本将生成的Makefile.in文件转换为Makefile。如果在configure.ac中定义了一些特殊的宏,比如AC_PROG_LIBTOOL,它会调用libtoolize,否则它会自己产生config.guess和config.sub 。

5.autoconf:将configure.ac中的宏展开,生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。

Makefile.am的语法

一般格式

文件类型书写格式可执行文件bin_PROGRAMES = foofoo_SOURCES = xxxx.cfoo_LDADD =foo_LDFLAGS =foo_DEPENDENCIES =静态库lib_LIBRARIES = libfoo.afoo_a_SOURCES =foo_a_LDADD =foo_a_LIBADD =foo_a_LDFALGS =头文件include_HEADERS = foo.h数据文件data_DATA = data1 data2

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

全局变量

Makefile.am还提供了一些全局变量供所有的目标体使用 :

变量含义INCLUDES比如链接时所需要的头文件LDADD比如链接时所需要的库文件LDFLAGS比如链接时所需要的库文件选项标志EXTRA_DIST源程序和一些默认的文件将自动打入 .tar.gz 包,其他文件若要进入 .tar.gz 包可以使用这种方法,如配置文件,数据文件等。SUBDIRS处理本目录前要递归处理哪些子目录

automake 安装路径

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

默认安装路径为 :$(prefix) = /usr/local可以通过 ./configure --prefix=<new_path> 的方法来覆盖。
GNU Automake很强大,这里仅仅只是入门。

参考:
(1)http://www.laruence.com/2008/11/11/606.html
(2) http://jianlee.ylinux.org/Computer/C和GNU开发/makefile-am.html
(3)http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=727270




1 0
原创粉丝点击