之所以要用到Autotools, 是因为当工程比较大的时候, 手工编写Makefile文件是很麻烦的一件事情~此时就需要一款可以收集系统配置信息并自动生成Makefile文件的工具~
Autotools工具是由以下5个工具组成:
1)autoscan 扫描源代码目录
2)autocal 展开宏
3)autoconf 生成configure文件
4)autoheader生成header文件
5)automake 生成Makefile相关
先来看一下GNU官网的Autotools工具使用流程图:
[http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Making-configure-Scripts.html#Making-configure-Scripts]Files used in preparing a software package for distribution, when using just Autoconf:
your source files --> [autoscan*] --> [configure.scan] --> configure.ac configure.ac --. | .------> autoconf* -----> configure [aclocal.m4] --+---+ | `-----> [autoheader*] --> [config.h.in] [acsite.m4] ---' Makefile.in
Additionally, if you use Automake, the following additional productions come into play:
[acinclude.m4] --. | [local macros] --+--> aclocal* --> aclocal.m4 | configure.ac ----' configure.ac --. +--> automake* --> Makefile.in Makefile.am ---'
Files used in configuring a software package:
.-------------> [config.cache] configure* ------------+-------------> config.log | [config.h.in] -. v .-> [config.h] -. +--> config.status* -+ +--> make* Makefile.in ---' `-> Makefile ---'
下面这个可能更直观一些:
举个栗子:
先用vi写一个hello.c并保存;
#include<stdio.h>
void main()
{
printf("hello,tudou!");
}
autoscan
功能: 扫描源代码目录并生成configure.scan文件
lmtd@lmtd-virtual-machine:~/demo$ autoscan
lmtd@lmtd-virtual-machine:~/demo$ ls
autoscan.log configure.scan hello.c
lmtd@lmtd-virtual-machine:~/demo$ mv configure.scan configure.ac
lmtd@lmtd-virtual-machine:~/demo$ ls
autoscan.log configure.ac hello.c
编辑一下configure.ac文件:
1 # -*- Autoconf -*-
2 # Process this file with autoconf to produce a configure script.
3
4 AC_PREREQ([2.69])
5 AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
6 AC_CONFIG_SRCDIR([hello.c])
7 AC_CONFIG_HEADERS([config.h])
8
9 # Checks for programs.
10 AC_PROG_CC
11
12 # Checks for libraries.
13
14 # Checks for header files.
15
16 # Checks for typedefs, structures, and compiler characteristics.
17
18 # Checks for library functions.
19
20 AC_OUTPUT
将第5行,第20行的
5 AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
<pre name="code" class="cpp"> 20 AC_OUTPUT
改为:
5 AC_INIT(hello,1.0,lmtd@sina.com) //分别对应三个参数([文件全称],[版本号],[错误报告地址])
20 AC_OUTPUT(hello,1.0)
aclocal
功能:perl脚本程序,自动生成aclocal.m4文件.
lmtd@lmtd-virtual-machine:~/demo$ aclocal
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4 autom4te.cache autoscan.log configure.ac hello.c
之后我们会发现多了一个aclocal.m4宏展开文件和一个autom4te.cache缓存文件.
autoconf
功能:生成configure文件.
lmtd@lmtd-virtual-machine:~/demo$ autoconf
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac hello.c
虽然已经生成了configure文件,但是现在还不能进行配置,因为还缺少一些shell脚本文件,这些文件可以由automake生成.
automake
功能:产生Makefile.in文件.
具体用法:automake --add-missing
lmtd@lmtd-virtual-machine:~/demo$ 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
configure.ac:11: installing './compile'
configure.ac:8: installing './install-sh'
configure.ac:8: installing './missing'
automake: error: no 'Makefile.am' found for any configure output
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4 autoscan.log config.log configure.ac install-sh
autom4te.cache compile configure hello.c missing
configure.ac:11: error: required file './compile' not found
configure.ac:11: 'automake --add-missing' can install 'compile'
configure.ac:8: error: required file './install-sh' not found
configure.ac:8: 'automake --add-missing' can install 'install-sh'
configure.ac:8: error: required file './missing' not found
configure.ac:8: 'automake --add-missing' can install 'missing'
就是说缺少install-sh等脚本文件.
GNU官网说它举个栗子:
Now it is time to write your Makefile.am for zardoz
. Since zardoz
is a user program, you want to install it where the rest of the user programs go: bindir
. Additionally, zardoz
has some Texinfo documentation. Your configure.ac script uses AC_REPLACE_FUNCS
, so you need to link against ‘$(LIBOBJS)’. So here’s what you’d write:
bin_PROGRAMS = zardoz //要命名的程序 zardoz_SOURCES = main.c head.c float.c vortex9.c gun.c //源代码程序 zardoz_LDADD = $(LIBOBJS) //引用类库的路径 info_TEXINFOS = zardoz.texi
Now you can run ‘automake --add-missing’ to generate your Makefile.in and grab any auxiliary files you might need, and you’re done!
1 bin_PROGRAMS = hello
2 hello_SOURCES = hello.c
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4 compile configure install-sh
autom4te.cache config.log configure.ac Makefile.am
autoscan.log config.status hello.c missing
lmtd@lmtd-virtual-machine:~/demo$ automake
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 './INSTALL'
Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am: Consider adding the COPYING file to the version control system
Makefile.am: for your code, to avoid questions about which license your project uses
configure.ac:7: error: required file 'config.h.in' not found
Makefile.am: installing './depcomp'
lmtd@lmtd-virtual-machine:~/demo$ touch NEWS README AUTHORS ChangeLog
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4 ChangeLog configure hello.c missing
AUTHORS compile configure.ac INSTALL NEWS
autom4te.cache config.log COPYING install-sh README
autoscan.log config.status depcomp Makefile.am
lmtd@lmtd-virtual-machine:~/demo$ 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
configure.ac:7: error: required file 'config.h.in' not found
咦?这里说缺少"config.h.in"这是为什么呢?
autoheader
功能:自动生成config.h.in.
lmtd@lmtd-virtual-machine:~/demo$ autoheader
lmtd@lmtd-virtual-machine:~/demo$ ls
aclocal.m4 ChangeLog config.status depcomp Makefile.am
AUTHORS compile configure hello.c missing
autom4te.cache config.h.in configure.ac INSTALL NEWS
autoscan.log config.log COPYING install-sh README