这是一个 autoconf / automake 的 "Hello World"
gztt.ll@gmail.com

主要步骤是
- 准备工程目录结构和程序
- autoscan 生成 configure.scan
- 编辑修改 configure.scan,重命名为 configure.ac 或 configure.in
- aclocal; libtoolize; autoheader; autoconf 生成 'configure' 可执行文件
- 写 Makefile.am
- automake 生成 Makefile
- 完成。后面就可以 ./configure; make; make install; make dist; ...

试验工程结构如下

hello
|
|---- src
|     |---- main
|     |     |---- main.c
|     |
|     |---- foo
|     |     |---- foo.c
|     |
|     |---- bar
|           |---- bar.c
|
|---- include
       |---- foo.h
       |---- bar.h

说明一下
"hello"     工程的$(top_srcdir)
"src/main"   生成可执行文件 hello 主目录
"src/foo"   生成静态库的目录 libfoo.a;hello 主程序 link 时用
"src/bar"   生成动态库的目录 libbar.so;hello 主程序运行时加载
"include"   头文件目录

$mkdir hello && cd hello

准备示例程序,如下

### 文件内容 ###

/* ### src/main/main.c ### */
#include <stdio.h>
#include "foo.h"
#include "bar.h"
int main(int argc, char *argv[])
{
     printf("main +\n");
     func_foo();
     func_bar();
     printf("main -\n");
    return 0;
}

/* ### include/foo.h ### */
#include <stdio.h>
void func_foo(void);

/* ### src/foo/foo.c ### */
#include "foo.h"
void func_foo(void)
{
     printf("foo +-\n");
}

/* ### include/bar.h ### */
#include <stdio.h>
void func_bar(void);

/* ### src/bar/bar.c ### */
#include "bar.h"
void func_bar(void)
{
     printf("bar +-\n");
}

下面进入顶层目录 $(top_srcdir)

$cd hello
$autoscan

自动生成 configure.scan 文件,我们要作稍许修改,重命名为 configure.ac 或 configure.in
(configure.in 是老版本 autoconf 支持的)

$mv configure.scan configure.ac

修改 configure.ac 如下

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

AC_PREREQ(2.61)
AC_INIT(hello, 1.0.0, gztt.ll@gmail.com)                ### Modified
AM_INIT_AUTOMAKE(hello,1.0.0, gztt.ll@gmail.com)      ### Added
AC_CONFIG_SRCDIR([src/main/main.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC
AC_PROG_RANLIB                                         ### Added if static libary used
AC_PROG_LIBTOOL                                         ### Added if dynamic libary used

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST

# Checks for library functions.
AC_OUTPUT(Makefile                                       ### Added
           include/Makefile
           src/main/Makefile
           src/foo/Makefile
           src/bar/Makefile
          )

说明一下:
如果不使用静态库或动态库 'AC_PROG_RANLIB' 和'AC_PROG_LIBTOOL' 则都不需要
如果仅使用静态库,增加 'AC_PROG_RANLIB' 即可
如果需要使用动态库,需加上 'AC_PROG_LIBTOOL'
'AC_CONFIG_HEADER(...)',我在 RedHat9 上试的时候,这行要改面 'AM_CONFIG_HEADER(...)',原因未知。
用 RedHat 9 时,最后 'AC_OUTPUT(...)' 里文件也不太一样, RedHat9 需要多一行 'src/Makefile',详见下面说明。
(我默认是用 debian 5)

接着执行下面的命令

$aclocal
$libtoolize
$autoheader
$autoconf

注:只有用到动态库时,才需要 '$libtoolize'
否则,只需要运行

$aclocal
$autoheader
$autoconf

即可。

此时应该可以生成 'configure' 可执行文件

然后我们写 Makefile.am

/* ### Makefile.am ### */
SUBDIRS = include src/bar src/foo src/main

/* ### include/Makefile.am ### */
helloincludedir=$(includedir)
helloinclude_HEADERS=foo.h bar.h

/* ### src/main/Makefile.am ### */
bin_PROGRAMS=hello
hello_SOURCES=main.c
hello_LDADD=$(top_srcdir)/src/foo/libfoo.a
LIBS=-lbar
INCLUDES=-I$(top_srcdir)/include
hello_LDFLAGS=-L$(top_srcdir)/src/bar

/* ### src/foo/Makefile.am ### */
noinst_LIBRARIES=libfoo.a
libfoo_a_SOURCES=foo.c
INCLUDES=-I$(top_srcdir)/include

/* ### src/bar/Makefile.am ### */
lib_LTLIBRARIES=libbar.la
libbar_la_SOURCES=bar.c
INCLUDES=-I$(top_srcdir)/include

说明一下,
第一个 'Makefile.am' 文件,即'$(top_srcdir)/Makefile.am' 只说了子目录说明,按编译顺序排的。
RedHat 9 会报错,说不能含 '/' 符号;解决办法是,我们新增加一个  '$(top_srcdir)/src/Makefile.am'
这样:
/* ### Makefile.am ### */
SUBDIRS = include src
/* ### src/Makefile.am ### */
SUBDIRS = bar foo main
此时,如前面所讲 'configure.ac' 最后的'AC_OUTPUT' 就需多一行'src/Makefile' 了

'$(top_srcdir)/src/main/Makefile.am' 相对复杂一点,
特别指出的是 'hello_LDFLAGS' 后,应是'...=-L$(top_srcdir)/...',而不是'...=-L $(top_srcdir)/...'
多一个空格,后面就有错误。

接着 automake 的话,无法通过的,还需要下面几个文件,我们暂时 touch 一下即可

$touch README NEWS AUTHORS ChangeLog

注意是在 $(top_srcdir) 目录下面

然后
$automake --add-missing
$./configure

至此,所有 Makefile 文件应该全部生成
我们可以执行
$make
$make clean
$make install
$make uninstall
$make dist
$make ...

autoconf automake libtool的更多相关文章

  1. 手动安装m4, autoconf, automake, libtool

    转自http://ruby-china.org/topics/2434 系列文章原载于自己的博客,TOPI.CO (http://topi.co) ,某天不小心就push错啦,懒得从头再来,上传到Ru ...

  2. 安装 Autoconf, Automake & Libtool

    今天在使用sudo apt-get install命令安装autoconf和automake时,出现了问题,说是不能sudo apt-get install安装这些软件似乎不是最新的.由此,我通过搜索 ...

  3. Autoconf/Automake工具简介

    在linux下编程的时候,有时候工程项目很大,文件比较多,此时需要使用自动创建Makefile文件功能.也就是使用Autoconf/Automake工具自动生成Makefile,为编译程序带来了方便, ...

  4. cmake与autoconf+automake

    cmake与autoconf+automakes是同类的编译工具,本人常用的是cmake. 这有一篇对比的文章,记录一下. cmake与autoconf+automake的对比

  5. [strongswan][autoconf][automake][cento] 在CentOS上编译strongswan git源码时遇到的autoconf问题

    编译strongswan的git源码问题 1. 概述 首先,我们想要通过源码编译strongswan.当满足以下条件时,通常你会遇见此问题: 源码时通过git clone的得来的,而不是官网下载的源码 ...

  6. 安装m4,autoconf,automake

    ###安装m4 wget http://mirrors.kernel.org/gnu/m4/m4-1.4.13.tar.gz \ && tar -xzvf m4-1.4.13.tar. ...

  7. autoscan; aclocal; autoconf; automake --add-missing; ./configure; make

    1.autoscan 在源码目录下执行autoscan,生成configure.scan,重命名为configure.in或者configure.ac,然后编辑文件内容: ============== ...

  8. Mac下安装MacProt,并GNU autotools的安装和使用 autoconf,automake

    1 MacPort的下载:http://www.macports.org/install.php, 需要安装xCode支持macport 2 安装MacPorts 与其他Mac的软件的安装方式相同,挂 ...

  9. autoconf / automake工具使用介绍

    本文转自:http://blog.csdn.net/gulansheng/article/details/42683809 一.简介 作为Linux下的程序开发人员,一定都遇到过Makefile,用m ...

随机推荐

  1. Eclipse快捷键调试

    Eclipse中有如下一些和运行调试相关的快捷键Ctrl+Shift+B:在当前行设置断点或取消设置的断点  F11:调试最后一次执行的程序    Ctrl+F11:运行最后一次执行的程序F5:跟踪到 ...

  2. Linux kernel驱动相关抽象概念及其实现 之“bus,device,driver”

    bus,device,driver三个很重要的概念贯穿Linux内核驱动架构,特转载一篇博文: (转载自http://blog.csdn.net/gdt_a20/article/details/642 ...

  3. 【JavaScript设计模式系列---开篇预览】

    转:http://www.cnblogs.com/Darren_code/archive/2011/08/31/JavascripDesignPatterns.html 2011-08-31 23:5 ...

  4. Java Interview Reference Guide--reference

    Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...

  5. iOS在xib或storyboard里为控件添加圆角、外框和外框颜色

    如果要在xib和storyboard里为控件添加圆角和外框宽度,只要这样做就可以: layer.borderWidth 设置外框宽度属性 layer.cornerRadius 设置圆角属性 只要为属性 ...

  6. ubuntu14.04使用root用户登录桌面 分类: 学习笔记 linux ubuntu 2015-07-05 10:30 199人阅读 评论(0) 收藏

    ubuntu安装好之后,默认是不能用root用户登录桌面的,只能使用普通用户或者访客登录.怎样开启root用户登录桌面呢? 先用普通用户登录,然后切换到root用户,然后执行如下命令: vi /usr ...

  7. 今天给大家分享一下Android中的资源与国际化的问题

    摘要:该文章将向大家分享Android中的资源与国际化的问题. 今天给大家分享一下Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示: 我们主要看一下layo ...

  8. IntelliJ插件安装

    1. 插件管理器在线安装 在IntelliJ插件管理页面([FileàSettingsàIDE SettingsàPlugins]),点击[Browse repositories-]按钮,在搜索框内输 ...

  9. PHP中用mysqli面向过程打开连接关闭mysql数据库

    代码如下: <meta http-equiv="content-type" content="text/html" charset="utf-8 ...

  10. Bash中的数组

    变量:$VAR或者${VAR} 数组:${VAR[$i]} 打印整个数组:echo ${VAR[@]} 统计数组元素个数:echo ${#VAR[@]} 从文件读入数组(按行读入):VAR=(`cat ...