---
title: "使用automake"
author: "ipjmc"
date: "2012-11-22T16:06:33Z"
tags: []
categories: ["编程语言"]
slug: "automake"
draft: false
lastmod: "2012-11-22T16:06:33Z"
id: 1733377
url: "https://ipjmc.iteye.com/blog/1733377"
---

转载过来，自己加工了一点点，留着参考吧：）

使用automake主要会用到aclocal、autoscan、autoconf、autoheader和automake这几个命令。

**首先简略的说一下用automake生成makefile的步骤：**  
（1）创建源代码文件，使用”autoscan”生成configure.scan文件，将其重命名为configure.ac，并做适当修改，然后使用”aclocal”命令生成aclocal.m4文件，使用”autoconf”命令由configure.ac和aclocal.m4文件生成configure文件。  
（2）手工编辑Makefile.am文件，使用”automake”命令生成configure.in文件。  
（3）手工编辑或由系统给定acconfig.h文件，使用”autoheader”命令生成config.h.in文件。  
（4）使用”configure”命令由configure、configure.in和config.h.in文件生成Makefile文件。从而完成Makefile文件的创建过程。

下面用一个经典的hello world的例子说明automake的用法  
**1.编辑源文件**

```
root@ubuntu:~# mkdir hello
root@ubuntu:~# cd hello
root@ubuntu:~/hello# vim hello.c
root@ubuntu:~/hello# cat hello.c
#include "stdio.h"
int main()
{
printf("hello world\n");
return 0;
}
```

**2．使用Autoscan工具生成configure.ac文件**  
Autoscan工具用来扫描源代码以搜寻一般的可移植性问题，比如检查编译器、库和头文件等，并创建configure.scan文件，它会在给定目录及其子目录树中检查源文件，若没有给出目录，就在当前目录及其子目录树中进行检查。

```
root@ubuntu:~/hello# autoscan          //扫描源文件，生成configure.scan
root@ubuntu:~/hello# ls
autoscan.log  configure.scan  hello.c
root@ubuntu:~/hello# cat configure.scan
#                                               -*- 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
```

下面给出本文件的简要说明（所有以”#”号开始的行为注释）：

（1）AC\_PREREQ宏声明本文件要求的autoconf版本，本例使用的版本为2.59。

（2）AC\_INIT宏用来定义软件的名称和版本等信息，”FULL-PACKAGE-NAME”为软件包名称，”VERSION”为软件版本号，”BUG-REPORT-ADDRESS”为BUG报告地址（一般为软件作者邮件地址）。

（3）AC\_CONFIG\_SRCDIR宏用来侦测所指定的源码文件是否存在，来确定源码目录的有效性。此处为当前目录下的hello.c。

（4）AC\_CONFIG\_HEADER宏用于生成config.h文件，以便autoheader使用。

（5）AC\_PROG\_CC用来指定编译器，如果不指定，选用默认gcc。

（6）AC\_OUTPUT用来设定 configure 所要产生的文件，如果是makefile，configure会把它检查出来的结果带入makefile.in文件产生合适的makefile。使用Automake时，还需要一些其他的参数，这些额外的宏用aclocal工具产生。

此文件只是下面要使用的configure.ac文件的原型,当然我们还要对其做一定的修改：

```
root@ubuntu:~/hello# mv configure.scan  configure.ac   #将名字改成configure.in也行
root@ubuntu:~/hello# vim configure.ac
root@ubuntu:~/hello# cat configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])               //automake版本
AC_INIT(hello, 1.0, xx@163.com)             //设置软件包信息
AM_INIT_AUTOMAKE(hello, 1.0)               //这个需要手动添加，它是automake必备的宏
AC_CONFIG_SRCDIR([hello.c])                //源文件名
#AC_CONFIG_HEADERS([config.h])           //config.h 文件， 这个地方我们不需要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([Makefile])                  //要输出的文件名
```

**3．使用aclocal工具生成aclocal.m4**  
aclocal根据configure.ac生成aclocal.m4

```
root@ubuntu:~/hello# aclocal
root@ubuntu:~/hello# ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  hello.c
```

**4．使用autoconf工具生成configure文件**  
将configure.ac中的宏展开，生成configure脚本。这个过程可能要用到aclocal.m4中定义的宏。

```
root@ubuntu:~/hello# autoconf
root@ubuntu:~/hello# ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  hello.c
```

**5．生成config.h.in文件**

运行 autoheader，它负责生成config.h.in文件。该工具通常会从“acconfig.h”文件中复制用户附加的符号定义，因此此处没有附加符号定义，且我们在上面2中已经注释掉了AC\_CONFIG\_HEADERS(\[config.h]) ，所以不需要创建“config.h.in”文件。

假如我们没有注释掉，运行autoheader后的状态如下：  
\[root@localhost main]# **autoheader**  
\[root@localhost main]# **ls**  
aclocal.m4      autoscan.log configure     configure.in~  
autom4te.cache config.h.in   configure.in hello.c

**6．创建Makefile.am文件**  
Automake工具会根据configure.in中的参量把Makefile.am转换成Makefile.in文件。所以在使用automake之前我们需要自己创建Makefile.am文件

```
root@ubuntu:~/hello# vim Makefile.am
root@ubuntu:~/hello# cat Makefile.am
AUTOMAKE_OPTIONS=foreign           //软件等级
bin_PROGRAMS=hello                  //可执行文件名
hello_SOURCES=hello.c              //源文件名
```

其中：

（1）AUTOMAKE\_OPTIONS为设置Automake的选项。由于GNU对自己发布的软件有严格的规范，比如必须附带许可证声明文件COPYING等，否则Automake执行时会报错。Automake提供了3种软件等级：foreign、gnu和gnits，供用户选择，默认等级为gnu。本例使需用foreign等级，它只检测必须的文件。

（2）bin\_PROGRAMS定义要产生的执行文件名。如果要产生多个执行文件，每个文件名用空格隔开。

（3）hello\_SOURCES定义”hello”这个执行程序所需要的原始文件。如果”hello”这个程序是由多个原始文件所产生的，则必须把它所用到的所有原始文件都列出来，并用空格隔开。例如：若目标体”hello”需要”hello.c”、”hello.h”两个依赖文件，则定义hello\_SOURCES=hello.c hello.h。

**7．使用Automake生成Makefile.in文件**  
下面使用Automake生成”Makefile.in”文件，使用选项”–add-missing”可以让Automake自动添加一些必需的脚本文件。如下所示：

```
root@ubuntu:~/hello# automake --add-missing
configure.ac:6: installing `./install-sh'
configure.ac:6: installing `./missing'
Makefile.am: installing `./depcomp'
root@ubuntu:~/hello# ls
aclocal.m4      autoscan.log  configure.ac  hello.c     Makefile.am  missing
autom4te.cache  configure     depcomp       install-sh  Makefile.in
```

**8.执行./configure 生成Makefile**

**9.测试**

```
root@ubuntu:~/hello# ls
aclocal.m4      config.log     configure.ac  hello.c     Makefile     missing
autom4te.cache  config.status  depcomp       hello.o     Makefile.am
autoscan.log    configure      hello         install-sh  Makefile.in
root@ubuntu:~/hello# ./hello
hello world
```

使用”make dist”命令将程序和相关的文档打包为一个压缩文档以供发布。

```
root@ubuntu:~/hello# make dist
root@ubuntu:~/hello# ls
aclocal.m4      config.status  hello             install-sh   missing
autom4te.cache  configure      hello-1.0.tar.gz  Makefile
autoscan.log    configure.ac   hello.c           Makefile.am
config.log      depcomp        hello.o           Makefile.in

hello-1.0.tar.gz为打包过后的文件
```

```
 
```

```
 
```

**下面是另一种文档结构**

```
|-- conf
|   `-- test.conf
`-- src
    |-- test1.c
    |-- test2.c
    `-- test2.h
```

src为源文件目录，conf为配置文件的目录。创建Makefile的方式和上面有一点点不同

**1.执行autoscan命令**

```
root@ubuntu:~/test# autoscan
root@ubuntu:~/test# ls
autoscan.log  conf  configure.scan  src
root@ubuntu:~/test# cat configure.scan
#                                               -*- 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([src/test2.h])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

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

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT 
```

修改configure.scan为configure.ac

```
root@ubuntu:~/test# mv configure.scan configure.ac
root@ubuntu:~/test# vim configure.ac
root@ubuntu:~/test# cat configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT(test, 1.0)
AM_INIT_AUTOMAKE(test, 1.0)
AC_CONFIG_SRCDIR([src/test2.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.
AC_CHECK_LIB(pthread, pthread_create)  //pthread为链接库名字， pthread_create为库中的一个函数

# Checks for header files.
AC_CHECK_HEADERS(stdlib.h string.h pthread.h) 

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT([Makefile
		    src/Makefile
		    conf/Makefile])             //每一个目录需要输出一个Makefile文件
```

**2．使用aclocal工具生成aclocal.m4**  
**3．使用autoconf工具生成configure文件**  
**4. 创建Makefile.am**  
在顶层目录创建的Makefile.am的内容如下

```
root@ubuntu:~/test# vim Makefile.am
root@ubuntu:~/test# cat Makefile.am
SUBDIRS= src conf
```

在src目录下创建的Makefile.am内容如下

```
root@ubuntu:~/test# cd src/
root@ubuntu:~/test/src# vim Makefile.am
root@ubuntu:~/test/src# cat Makefile.am
bin_PROGRAMS= test
test_SOURCES= test1.c test2.c
```

在conf目录下创建的Makefile.am内容如下

```
root@ubuntu:~/test/conf# vim Makefile.am
root@ubuntu:~/test/conf# cat Makefile.am
configdir = /etc             //这个数据文件将要移动到的目录
config_DATA = test.conf    //数据文件名，如果有多个文件则这样写config_DATA = test1.dat test2.dat
```

**5．使用Automake生成Makefile.in文件**

```
root@ubuntu:~/test# automake --add-missing
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
```

这里出现了一个问题，NEWS， README这几个文件不存在， 那么我们就手工创建他们

```
root@ubuntu:~/test# touch NEWS README AUTHORS ChangeLog
```

再次执行automake

```
root@ubuntu:~/test# automake
```

**6.后面的步骤就和上面的例子一样了**