关于Autotools
Autotools是用来从源代码生成用户可以使用的目标(targets)的自动化工具。目标可以包括库、可执行文件、或者生成的脚本等等。是[构建系统][6]的一种。常用的构建系统有GNU Make、GNU autotools、CMake、Apache Ant(主要用于JAVA)。
Autotools 是一系列工具的组合:
1. autoscan
2. aclocal
3. autoconf
4. autoheader
5. automake
安装Autotools工具
使用automake –version命令确认当前系统是否已经安装,未安装则执行sudo apt-get install automake命令进行安装
源文件在同一目录下
下面介绍下源文件在同一目录下,Autotools工具的使用。
1、源代码
main.c
#include <stdio.h>
#include "sum.h"
#include "sub.h"
int main()
{
int x = 20;
int y = 10;
printf("%d + %d = %d\n", x, y, sum(x, y));
printf("%d - %d = %d\n", x, y, sub(x, y));
return 0;
}
sum.h
#include <stdio.h>
int sum(int x, int y);
sum.c
#include "sum.h"
int sum(int x, int y)
{
return x + y;
}
sub.h
#include <stdio.h>
int sub(int x, int y);
sub.c
#include "sub.h"
int sub(int x, int y)
{
return x - y;
}
此时目录结构如下:
2、autoscan命令
首先在项目根目录执行autoscan命令,扫描目录下的源文件,生成configure.scan文件。将configure.scan更名为configure.ac。
configure.ac原文件
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([main.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
修改成
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT(test, 1.0, test@qq.com)
AM_INIT_AUTOMAKE(test, 1.0)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
configure.ac标签说明:
标签 | 说明 |
---|---|
AC_PREREQ | 声明autoconf要求的版本号 |
AC_INIT | 定义软件名称、版本号、联系方式 |
AM_INIT_AUTOMAKE | 必须要的,参数为软件名称和版本号 |
AC_CONFIG_SCRDIR | 宏用来侦测所指定的源码文件是否存在, 来确定源码目录的有效性.。此处为当前目录下main.c |
AC_CONFIG_HEADER | 宏用于生成config.h文件,以便 autoheader 命令使用。 |
AC_PROG_CC | 指定编译器,默认GCC |
AC_CONFIG_FILES | 生成相应的Makefile文件,不同文件夹下的Makefile通过空格分隔。例如:AC_CONFIG_FILES([Makefile, src/Makefile]) |
AC_OUTPUT | 用来设定 configure 所要产生的文件,如果是makefile,configure 会把它检查出来的结果带入makefile.in文件产生合适的makefile。 |
3、aclocal命令
执行aclocal命令。该命令会扫描configure.ac命令生成autom4te.cache文件夹。 该文件主要处理本地的宏定义,它根据已经安装的宏、用户定义宏和 acinclude.m4 文件中的宏将 configure.ac 文件需要的宏集中定义到文件 aclocal.m4 中。
4、autoconf命令
执行aautoconf命令。执行完生成configure文件。
5、autoheader命令
执行autoheader命令。该命令生成 config.h.in 文件。该命令通常会从 “acconfig.h” 文件中复制用户附加的符号定义。该例子中没有附加的符号定义, 所以不需要创建 “acconfig.h” 文件。
6、创建Makefile.am文件
创建Makefile.am文件。Automake工具会根据 configure.in 中的参量把 Makefile.am 转换成 Makefile.in 文件。最终通过Makefile.in生成Makefile文件
AUTOMARK_OPTIONS = foreign
bin_PROGRAMS = test
test_SOURCES = main.c sub.h sub.c sum.h sum.c
标签 | 说明 |
---|---|
AUTOMARK_OPTIONS | 由于GNU对自己发布的软件有严格的规范, 比如必须附带许可证声明文件COPYING等,否则automake执行时会报错. automake提供了3中软件等级:foreign, gnu和gnits, 供用户选择。默认级别是gnu. 在本例中, 使用了foreign等级, 它只检测必须的文件。 |
bin_PROGRAMS | 生成的可执行文件名称,生成多个可执行文件,可以用空格隔开。 |
test_SOURCES | 生成可执行文件test需要依赖的源文件。其中test_为可执行文件的名称。 |
7、automake命令
执行automake –add-missing命令。最后生成Makefile.in 文件。报错文件不存在,手动创建后重新执行一遍。
8、configure命令
执行./congigure命令。根据Makefile.in 生成 Makefile 文件。
9、make命令
执行make命令。最后在根目录生成test可执行文件。
源文件在不同目录下
1、源代码及目录结构
源代码同上,只是目录结构发生了改变。工程根目录和src目录下分别创建Makefile.am文件。
./Makefile.am文件内容如下:
AUTOMAKE_OPTIONS=foreign
SUBDIRS=src
bin_PROGRAMS=test
test_SOURCES=src/main.c
test_LDADD=src/libpro.a
INCLUDES=-I./include
LIBS=-l pthread
./src/Makefile.am文件内容如下:
noinst_LIBRARIES=libpro.a
libpro_a_SOURCES=sum.c sub.c
INCLUDES=-I../include
2、autoscan命令
如上,执行autoscan命令生成configure.scan,后重命名为configure.ac。然后修改此文件。
原文件内容:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/main.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_CONFIG_FILES([Makefile
src/Makefile])
AC_OUTPUT
修改后文件内容:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([test], [1.0], [test@qq.com])
AM_INIT_AUTOMAKE([test], [1.0])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AC_PROG_RANLIB
# 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_CONFIG_FILES([Makefile
src/Makefile])
AC_OUTPUT
3、aclocal命令
如上,执行aclocal命令。扫描 configure.ac 文件生成 aclocal.m4文件
4、autoconf命令
如上,执行autoconf命令。将 configure.ac 文件中的宏展开,生成 configure 脚本
5、autoheader命令
如上,执行autoheader命令。该命令生成 config.h.in 文件。该命令通常会从 “acconfig.h” 文件中复制用户附加的符号定义。该例子中没有附加的符号定义, 所以不需要创建 “acconfig.h” 文件。
6、automake命令
如上,执行automake –add-missing命令。生成 Makefile.in 文件。
7、configure命令
如上,执行./configure命令。把 Makefile.in 变成最终的 Makefile 文件。
8、make命令
如上,执行 make命令。最后在根目录生成test可执行文件。