wangsheng@pc01:~/work/train/make/automake$ ls aclocal.m4 autom4te.cache autoscan.log configure configure.in hello.c (5) 运行autoheader命令, 生成config.h.in文件.
该工具通常会从”acconfig.h”文件中复制用户附加的符号定义. 本例中没有附加的符号定义, 所以不需要创建”acconfig.h”文件.
wangsheng@pc01:~/work/train/make/automake$ autoheader wangsheng@pc01:~/work/train/make/automake$ ls aclocal.m4 autom4te.cache autoscan.log config.h.in configure configure.in hello.c (6) 运行automake命令, 生成Makefile.in文件
这一步是创建Makefile很重要的一步, automake要用的脚本配置文件是Makefile.am, 用户需要自己创建相应的文件. 之后, automake工具将自动转换成Makefile.in 本例中, 创建的文件为Makefile.am, 内容如下:
wangsheng@pc01:~/work/train/make/automake$ cat Makefile.am AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=hello hello_SOURCES=hello.c 说明:
?
其中的AUTOMAKE_OPTIONS为设置automake的选项. 由于GNU对自己发布的软件有严格的规范, 比如必须附带许可证声明文件COPYING等, 否则automake执行时会报错. automake提供了3中软件等级:foreign, gnu和gnits, 供用户选择. 默认级别是gnu. 在本例中, 使用了foreign等级, 它只检测必须的文件.
? ?
bin_PROGRAMS定义要产生的执行文件名. 如果要产生多个执行文件, 每个文件名用空格隔开
hello_SOURCES 定义”hello”这个可执行程序所需的原始文件. 如果”hello”这个程序是由多个源文件所产生的, 则必须把它所用到的所有源文件都列出来, 并用空格隔开. 如果要定义多个可执行程序, 那么需要对每个可执行程序建立对应的file_SOURCES.
在这里使用”–add-missiing”选项可以让automake自动添加一些必须的脚本文件.
wangsheng@pc01:~/work/train/make/automake$ automake --add-missing configure.in:7: installing `./install-sh' configure.in:7: installing `./missing' Makefile.am: installing `./depcomp' wangsheng@pc01:~/work/train/make/automake$ ls aclocal.m4 autoscan.log configure depcomp install-sh Makefile.in autom4te.cache config.h.in configure.in hello.c Makefile.am missing (7)运行configure, 生成Makfefile文件
wangsheng@pc01:~/work/train/make/automake$ ./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 wangsheng@pc01:~/work/train/make/automake$ ls aclocal.m4 config.h config.status depcomp Makefile missing autom4te.cache config.h.in configure hello.c Makefile.am stamp-h1 autoscan.log config.log configure.in install-sh Makefile.in autotools生成Makefile流程图如下:
使用由autotools生成的Makefile
autotools生成的Makefile具有以下主要功能: (1) make
编译源程序, 键入make, 默认执行”make all”命令
wangsheng@pc01:~/work/train/make/automake$ make make all-am make[1]: Entering directory `/home/wangsheng/work/train/make/automake' gcc -DHAVE_CONFIG_H -I. -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c mv -f .deps/hello.Tpo .deps/hello.Po gcc -g -O2 -o hello hello.o make[1]: Leaving directory `/home/wangsheng/work/train/make/automake' 此时在本目录下就生成了可执行文件”hello”, 运行”./hello”就能看到程序的执行结果:
wangsheng@pc01:~/work/train/make/automake$ ./hello Hello, autotools! (2) make install
执行该命令, 可以把程序安装到系统目录中
wangsheng@pc01:~/work/train/make/automake$ sudo make install 此时, 直接在console输入hello, 就可以看到程序的运行结果
(3) make clean
清除之前所编译的可执行文件及目标文件
wangsheng@pc01:~/work/train/make/automake$ make clean test -z \rm -f *.o (4) make dist
将程序和相关的文档打包为一个压缩文档以供发布
wangsheng@pc01:~/work/train/make/automake$ make dist wangsheng@pc01:~/work/train/make/automake$ ls -l hello-1.0.tar.gz hello-1.0.tar.gz 可见该命令生成了一个hello-1.0.tar.gz的压缩文档.
4.2 编辑 Makefile.am 档
接下来我们要编辑 Makefile.am 档,Automake 会根据 configure.in 中
的巨集把Makefile.am 转成 Makefile.in 档。Makefile.am 档定义我们所
要产的目标:
AUTOMAKE_OPTIONS
设定 automake 的选项。Automake 主要是帮助开发 GNU 软体的人员
维护软体套件,所以在执行 automake 时,会检查目录下是否存在标
准 GNU 软体套件中应具备的文件档案,例如 'NEWS'、'AUTHOR'、
'ChangeLog' 等文件档。设成 foreign 时,automake 会改用一般软
体套件的标准来检查。
bin_PROGRAMS
定义我们所要产生的执行档档名。如果要产生多个执行档,每个档名
用空白字元隔开。
hello_SOURCES
定义 'hello' 这个执行档所需要的原始档。如果 'hello' 这个程序
是由多个原始档所产生,必须把它所用到的原始档都列出来,以空白
字元隔开。假设 'hello' 这个程序需要 'hello.c'、'main.c'、