Linux|ubuntu automake

1) 软件下载
sudo apt-get install automake autoconf
2) 编写单个 C 文件.
#include
int main(int argc, char** argv)
{
printf("Hello, Auto Makefile!\n");
return 0;
}
3)运行 autoscan
# autoscan
说明:运行 autoscan 后,?自动创建两个文件: autoscan.log configure.scan, ls 查看后,将有如下结果:
total 16
drwxr-xr-x 2 sxz sxz 4096 2011-05-22 09:40 ./
drwxr-xr-x 4 sxz sxz 4096 2011-05-22 09:01 ../
-rw-r--r-- 1 sxz sxz0 2011-05-22 09:40 autoscan.log
-rw-r--r-- 1 sxz sxz467 2011-05-22 09:40 configure.scan
-rw-r--r-- 1 sxz sxz82 2011-05-22 09:40 hello.c
4) 将 configure.scan 文件改名为configure.in 并查看其结果
$ mv configure.scan configure.in | vi configure.in

#-*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([hello.c])
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_OUTPUT
5) 修改 configure.in 文件的相关参数。?
5.1.修改AC_INIT里面的参数: AC_INIT(hello,1.0, xinzhushi@gmail.com)
5.2.添加宏AM_INIT_AUTOMAKE, 它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号。
5.3.在AC_OUTPUT后添加输出文件Makefile
修改的结果如下:
#-*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65])
AC_INIT(hello, 2.0, xinzhushi@gmail.com)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(hello,2.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])
6) 运行 aclocal 将生成一个“aclocal.m4”文件和一个缓冲文件夹autom4te.cache,该文件主要处理本地的宏定义。
total 56
drwxr-xr-x 3 sxz sxz4096 2011-05-22 10:17 ./
drwxr-xr-x 4 sxz sxz4096 2011-05-22 09:01 ../
-rw-r--r-- 1 sxz sxz 34611 2011-05-22 10:17 aclocal.m4
drwxr-xr-x 2 sxz sxz4096 2011-05-22 10:17 autom4te.cache/
-rw-r--r-- 1 sxz sxz0 2011-05-22 09:40 autoscan.log
-rw-r--r-- 1 sxz sxz488 2011-05-22 10:03 configure.in
-rw-r--r-- 1 sxz sxz82 2011-05-22 09:40 hello.c 7) 运行 autoconf , 目的是生成 config 文件。 $: autoconf total 192
drwxr-xr-x 3 sxz sxz4096 2011-05-22 10:24 ./
drwxr-xr-x 4 sxz sxz4096 2011-05-22 09:01 ../
-rw-r--r-- 1 sxz sxz34611 2011-05-22 10:17 aclocal.m4
drwxr-xr-x 2 sxz sxz4096 2011-05-22 10:24 autom4te.cache/
-rw-r--r-- 1 sxz sxz0 2011-05-22 09:40 autoscan.log
-rwxr-xr-x 1 sxz sxz 137641 2011-05-22 10:24 configure*
-rw-r--r-- 1 sxz sxz488 2011-05-22 10:03 configure.in
-rw-r--r-- 1 sxz sxz82 2011-05-22 09:40 hello.c 8) 运行 autoheader,它负责生成config.h.in文件。该工具通常会从“acconfig.h”文件中复制用户附加的符号定义,因此此处没有附加符号定义,所以不需要创建“acconfig.h”文件。
total 196
drwxr-xr-x 3 sxz sxz4096 2011-05-22 10:26 ./
drwxr-xr-x 4 sxz sxz4096 2011-05-22 09:01 ../
-rw-r--r-- 1 sxz sxz34611 2011-05-22 10:17 aclocal.m4
drwxr-xr-x 2 sxz sxz4096 2011-05-22 10:24 autom4te.cache/
-rw-r--r-- 1 sxz sxz0 2011-05-22 09:40 autoscan.log
-rw-r--r-- 1 sxz sxz625 2011-05-22 10:26 config.h.in
-rwxr-xr-x 1 sxz sxz 137641 2011-05-22 10:24 configure*
-rw-r--r-- 1 sxz sxz488 2011-05-22 10:03 configure.in
-rw-r--r-- 1 sxz sxz82 2011-05-22 09:40 hello.c
9:下面即将 用 automake 创建 Makefile. 但是在做这一步之前,我们需要创建一个 Makefile.am 文件。
9.1 ) 创建一个 Makefile.am.这一步是创建Makefile很重要的一步,automake要用的脚本配置文件是Makefile.am,用户需要自己创建相应的文件。之后,automake工具转换成Makefile.in。
Makefile.am 的内容如下:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.c
下面对 Makefile.am的文件内容进行分析一下
其中的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。
9.2) 使用automake对其生成“configure.in”文件,在这里使用选项“—add-missing”可以让automake自动添加有一些必需的脚本文件。
$ automake --add-missing
total 220
drwxr-xr-x 3 sxz sxz4096 2011-05-22 10:40 ./
drwxr-xr-x 4 sxz sxz4096 2011-05-22 09:01 ../
-rw-r--r-- 1 sxz sxz34611 2011-05-22 10:17 aclocal.m4
drwxr-xr-x 2 sxz sxz4096 2011-05-22 10:24 autom4te.cache/
-rw-r--r-- 1 sxz sxz0 2011-05-22 09:40 autoscan.log
-rw-r--r-- 1 sxz sxz625 2011-05-22 10:26 config.h.in
-rwxr-xr-x 1 sxz sxz 137641 2011-05-22 10:24 configure*
-rw-r--r-- 1 sxz sxz488 2011-05-22 10:03 configure.in
lrwxrwxrwx 1 sxz sxz32 2011-05-22 10:39 depcomp -> /usr/share/automake-1.11/depcomp*
-rw-r--r-- 1 sxz sxz82 2011-05-22 09:40 hello.c
lrwxrwxrwx 1 sxz sxz35 2011-05-22 10:39 install-sh -> /usr/share/automake-1.11/install-sh*
-rw-r--r-- 1 sxz sxz66 2011-05-22 10:40 Makefile.am
-rw-r--r-- 1 sxz sxz19390 2011-05-22 10:40 Makefile.in
lrwxrwxrwx 1 sxz sxz32 2011-05-22 10:39 missing -> /usr/share/automake-1.11/missing*
10) 运行configure,在这一步中,通过运行自动配置设置文件configure,把Makefile.in变成了最终的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 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 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
11) 查看一下,看是否生成了 Makefile
$ ls
total 316
drwxr-xr-x 4 sxz sxz4096 2011-05-22 10:57 ./
drwxr-xr-x 4 sxz sxz4096 2011-05-22 09:01 ../
-rw-r--r-- 1 sxz sxz34611 2011-05-22 10:17 aclocal.m4
drwxr-xr-x 2 sxz sxz4096 2011-05-22 10:24 autom4te.cache/
-rw-r--r-- 1 sxz sxz0 2011-05-22 09:40 autoscan.log
-rw-r--r-- 1 sxz sxz765 2011-05-22 10:56 config.h
-rw-r--r-- 1 sxz sxz625 2011-05-22 10:26 config.h.in
-rw-r--r-- 1 sxz sxz8488 2011-05-22 10:56 config.log
-rwxr-xr-x 1 sxz sxz32604 2011-05-22 10:56 config.status*
-rwxr-xr-x 1 sxz sxz 137641 2011-05-22 10:24 configure*
-rw-r--r-- 1 sxz sxz488 2011-05-22 10:03 configure.in
lrwxrwxrwx 1 sxz sxz32 2011-05-22 10:39 depcomp -> /usr/share/automake-1.11/depcomp*
drwxr-xr-x 2 sxz sxz4096 2011-05-22 10:57 .deps/
-rwxr-xr-x 1 sxz sxz9721 2011-05-22 10:57 hello*
-rw-r--r-- 1 sxz sxz82 2011-05-22 09:40 hello.c
-rw-r--r-- 1 sxz sxz4328 2011-05-22 10:57 hello.o
lrwxrwxrwx 1 sxz sxz35 2011-05-22 10:39 install-sh -> /usr/share/automake-1.11/install-sh*
-rw-r--r-- 1 sxz sxz19532 2011-05-22 10:56 Makefile
-rw-r--r-- 1 sxz sxz66 2011-05-22 10:40 Makefile.am
-rw-r--r-- 1 sxz sxz19390 2011-05-22 10:40 Makefile.in
lrwxrwxrwx 1 sxz sxz32 2011-05-22 10:39 missing -> /usr/share/automake-1.11/missing*
-rw-r--r-- 1 sxz sxz23 2011-05-22 10:56 stamp-h1
12 ) 运行一下,测试是否正确生成。
【Linux|ubuntu automake】 结果发现,一切OK。

    推荐阅读