autoconf和automake可以扫描源代码文件,自动生成configure和Makefile文件,可以减少一些重复劳动。设想做个简单测试,在源代码目录下有2个目录,include和main,分别是头文件和源代码,预想达到的目标是用autoconf工具集生成Makefile,运行make命令后能够生成可执行目标文件。


目录结构如下:
.
|-- include
|   `-- stu.h
`-- main
    `-- stu_list.c


include/stu.h:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#ifndef __STUDENT_H_
#define __STUDENT_H_

struct student {
    int no;
    char name[32];
};

#endif

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

 

main/stu_list.c:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#include <stdio.h>
#include <stdlib.h>
#include "include/stu.h"

int main(int argc, char* argv[])
{
    unsigned int i;
    struct student stus[] = {
        { 1, "aaa" },
        { 2, "bbb" }
    };

    for (i = 0; i < sizeof(stus)/sizeof(stus[0]); i++) {
        printf("-- % 3d --> %s\n", stus[i].no, stus[i].name);
    }

    exit(0);
}

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

 

下面是生成Makefile的步骤:

1) 运行autoscan命令

2) 将configure.scan 文件重命名为configure.in,并修改configure.in文件

3) 在当前目录下新建Makefile.am文件,并在main目录下也新建makefile.am文件 4) 在当前目录下新建NEWS、 README、 ChangeLog 、AUTHORS文件

5) 将/usr/share/automake-1.X/目录下的depcomp和complie文件拷贝到本目录下

6) 运行aclocal命令

7) 运行autoconf命令

8) 运行autoheader命令

9) 运行automake -a命令

10) 运行./confiugre脚本

 

configure.in要修改的内容包括:

将AC_CONFIG_HEADER([config.h])修改为:AM_CONFIG_HEADER(config.h), 并加入AM_INIT_AUTOMAKE(mytest,1.0)。在AC_OUTPUT输入要创建的Makefile文件名。

修改后的configure.in:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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

AC_PREREQ([2.65])
AC_INIT(mytest, 1.0, xxxx@hotmail.com)
AC_CONFIG_SRCDIR([include/stu.h])
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(mytest, 1.0)

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT(Makefile main/Makefile)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

 

Makefile.am:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

SUBDIRS = main

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

 

main/Makefile.am:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

noinst_PROGRAMS = mytest
mytest_SOURCES = stu_list.c

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

 

运行make命令,输出:

make  all-recursive
make[1]: 正在进入目录 `~/st/autoconf/project/src'
Making all in main
make[2]: 正在进入目录 `~/st/autoconf/project/src/main'
gcc -DHAVE_CONFIG_H -I. -I..     -g -O2 -MT stu_list.o -MD -MP -MF .deps/stu_list.Tpo -c -o stu_list.o stu_list.c
mv -f .deps/stu_list.Tpo .deps/stu_list.Po
gcc  -g -O2   -o mytest stu_list.o 
make[2]:正在离开目录 `~/st/autoconf/project/src/main'
make[2]: 正在进入目录 `~/st/autoconf/project/src'
make[2]: 没有什么可以做的为 `all-am'。
make[2]:正在离开目录 `~/st/autoconf/project/src'
make[1]:正在离开目录 `~/st/autoconf/project/src'

运行main目录下的mytest,输出:

--   1 --> aaa
--   2 --> bbb