automake自动生成Makefile文件

Linux下编程时,为了方便编译,往往使用Makefile文件自动完成编译,但是Makefile文件本身的书写十分复杂,规则很多。好在Linux为我们提供了自动生成功能完善的Makefile文件的工具autoconf/automake。本文讲述如何使用它们生成Makefile文件。

环境

  • OS
    ubuntu 16.04
  • aclocal
    1.15
  • autoconf
    2.69-9
  • automake
    1.15

使用示例

准备

创建一个main.c文件

1
2
3
4
5
6
#include <stdio.h>

int main(){
printf("Hello automake\n");
return 0;
}
1
2
$ ls
main.c

创建configure.in文件

运行autoscan,自动创建两个文件autoscan.logconfigure.scan

1
2
3
4
5
$ autoscan
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/\${ <-- HERE [^\}]*}/ at /usr/bin/autoscan line 361.

$ ls
autoscan.log configure.scan main.c

修改configure.scan,AC_INIT里面的参数: AC_INIT(main,1.0, test@263.com);添加宏AM_INIT_AUTOMAKE, 它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号;在AC_OUTPUT后添加输出文件Makefile。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT(main, 1.0, main@google.com)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(main, 1.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])

configure.scan重命名成configure.ac

1
2
$ ls
autoscan.log configure.ac main.c

生成aclocal相关文件

运行aclocal,生成文件aclocal.m4、目录autom4te.cache,处理本地的宏定义。

1
2
3
$ aclocal
$ ls
aclocal.m4 autom4te.cache autoscan.log configure.ac main.c

生成configure文件

运行autoconf

1
2
3
$ autoconf
$ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac main.c

生成config.h.in文件

运行autoheader,生成config.h.in,该工具通常会从“acconfig.h”文件中复制用户附加的符号定义,因此此处没有附加符号定义,所以不需要创建“acconfig.h”文件。

1
2
3
$ autoheader
$ ls
aclocal.m4 autom4te.cache autoscan.log config.h.in configure configure.ac main.c

运行automake

运行automake之前先创建一个Makefile.am,这一步是创建Makefile很重要的一步,automake要用的脚本配置文件是Makefile.am,用户需要自己创建相应的文件。之后,automake工具转换成Makefile.in

Makefile.am

1
2
3
4
AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=main
main_SOURCES=main.c

  • AUTOMAKE_OPTIONS为设置automake的选项
    由于GNU对自己发布的软件有严格的规范,比如必须附带许可证声明文件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。
1
2
3
4
$ automake --add-missing
configure.ac:8: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:8: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
Makefile.am: installing './depcomp'

使用automake对其生成“configure.in”文件,在这里使用选项“—adding-missing”可以让automake自动添加有一些必需的脚本文件。

1
2
$ ls
Makefile.am Makefile.in aclocal.m4 autom4te.cache autoscan.log compile config.h.in configure configure.ac depcomp install-sh main.c missing

运行configure生成Makefile

通过运行自动配置设置文件configure,把Makefile.in变成了最终的Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ ./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... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... 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 whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

$ ls
Makefile Makefile.am Makefile.in aclocal.m4 autom4te.cache autoscan.log compile config.h config.h.in config.log config.status configure configure.ac depcomp install-sh main.c missing stamp-h1

make编译工程

1
2
3
4
5
6
7
$ make
make all-am
make[1]: Entering directory '/root/automake'
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -g -O2 -o main main.o
make[1]: Leaving directory '/root/automake'
1
2
3
4
$ ls
Makefile Makefile.am Makefile.in aclocal.m4 autom4te.cache autoscan.log compile config.h config.h.in config.log config.status configure configure.ac depcomp install-sh main main.c main.o missing stamp-h1
$ ./main
Hello automake

参考鸣谢