使用Automake,Autoconf生成Makefile(Step by step)

简介: 在Unix上写过程序的人尤其是用 C 来开发程序的人一般都遇到过 Makefile,用 make 来开发和编译程序的确很方便,可是要写出一个Makefile就不那么简单了。
在Unix上写过程序的人尤其是用 C 来开发程序的人一般都遇到过 Makefile,用 make 来开发和编译程序的确很方便,可是要写出一个Makefile就不那么简单了。GNU Make 那份几百页的文件,让许多人害怕。当然,现在关于make的文档比较多,不过写一个Makefile总是一件很烦人的事情,GNU Autoconf 及 Automake 这两个软件就是帮助程序开发者轻松产生Makefile 文件的。现在的GNU软件如Apache, MySQL Minigui等都是利用Autoconf,Automake实现自动编译的。用户只要使用 “./configure”, “make”, “make install” 就可以把程序安裝到系统中。 

简介 
Makefile 基本上就是『目标』(target), 『关联』(dependencies) 和『动作』三者所组成的一系列规则。而 make 就是根据 Makefile 的规则决定如何编译 (compile) 和连接 (link) 程序或者其它动作。当然,make 可做的不只是编译和连接程序,例如 FreeBSD 的 port collection 中,Makefile还可以做到自动下载远程程序,解压缩 (extract) , 打补丁 (patch),设定,然后编译,安装到系统中。 

Makefile 基本结构虽然很简单,但是妥善运用这些规则就可以变换出许多不同的花样。却也因为这样,许多人刚开始学写Makefile 时会觉得没有规范可以遵循,每个人写出来的Makefile都不大一样,不知道从哪里下手,而且常常会受到开发环境的限制,只要环境参数不同或者路径更改,可能 Makefile 就得跟着修改。虽然有GNU Makefile Conventions (GNU Makefile惯例)制订出一些在进行 GNU 程序设计时写 Makefile 的一些标准和规范,但是其内容很长而且很复杂,并且经常作一些调整,为了减轻程序开发人员维护Makefile 的负担,就出现了Automake。 

利用Automake,编程者只需要写一些预先定义好的宏 (macro),提交给Automake处理,就会产生一个可以供 Autoconf 使用的 Makefile.in文件。再配合使用 Autoconf产生的自动配置文件 configure 即可产生一份符合 GNU Makefile 惯例的 Makeifle 了。 

需要的软件 
在开始使用 Automake 之前,首先确认你的系统安装有如下软件: 

1. GNU Automake 
2. GNU Autoconf 
3. GNU m4 
4. perl 
5. GNU Libtool (如果你需要产生 shared library) 

最好也使用 GNU C/C++ 编译器 、GNU Make 以及其它 GNU 的工具程序来作为开发的环境,这些工具都是属于 Open Source Software 不但免费而且功能强大。如果你是使用 Red Hat Linux 可以找到所有上述软件的 rpm 文件。  

使用Automake,Autoconf生成Makefile(Step by step) - happyboy200032 - happyboy200032的博客


一个简单的例子 
Automake 所产生的 Makefile 除了可以做到程序的编译和连接,也可以用来生成文档(如 manual page, info 文件等),还可以有把源码文件包装起来以供发布,所以程序源代码所存放的目录结构最好符合GNU 的标准惯例,接下来就用一个hello.c 來做为例子。 

在工作目录下建立一个新的子目录devel,再在 devel 下建立一个"hello"' 的子目录,这个目录将 
作为存放 hello这个程序及其相关文件的地方: 
% mkdir devel;cd devel;mkdir hello;cd hello 

用编辑器写一个hello.c文件, 
#i nclude <stdio.h> 
int main(int argc, char** argv) 

printf(“Hello, GNU!n”); 
return 0; 


接下来就要用 Autoconf 及 Automake 來产生 Makefile 文件了, 

1. 用 autoscan 产生一个 configure.in 的原型,执行autoscan 后会产生一个configure.scan 的文件,可以用它作为 configure.in文件的蓝本。 
% autoscan 
% ls 
configure.scan hello.c 

2. 编辑 configure.scan文件,如下所示,並且改名为configure.in 
dnl Process this file with Autoconf to produce a configure script. 
AC_INIT(hello.c) 
AM_INIT_AUTOMAKE(hello, 1.0) 
dnl Checks for programs. 
AC_PROG_CC 
dnl Checks for libraries. 
dnl Checks for header files. 
dnl Checks for typedefs, structures, and compiler characteristics. 
dnl Checks for library functions. 
AC_OUTPUT(Makefile) 

3. 执行 aclocal 和 Autoconf ,分別会产生 aclocal.m4 及 configure 两个文件 
% aclocal 
% Autoconf 
% ls 
aclocal.m4 configure configure.in hello.c 

4. 编辑 Makefile.am 文件,內容如下 
AUTOMAKE_OPTIONS= foreign 
bin_PROGRAMS= hello 
hello_SOURCES= hello.c 

5. 执行 Automake --add-missing ,Automake 会根据Makefile.am 文件产生一些文件,包含最重要的Makefile.in 
% Automake --add-missing 
Automake: configure.in: installing `./install-sh' 
Automake: configure.in: installing `./mkinstalldirs' 
Automake: configure.in: installing `./missing' 

6. 最后执行 ./configure: 
% ./configure 
creating cache ./config.cache 
checking for a BSD compatible install... /usr/bin/install -c 
checking whether build environment is sane... yes 
checking whether make sets ${MAKE}... yes 
checking for working aclocal... found 
checking for working Autoconf... found 
checking for working Automake... found 
checking for working autoheader... found 
checking for working makeinfo... found 
checking for gcc... gcc 
checking whether the C compiler (gcc ) works... yes 
checking whether the C compiler (gcc ) is a cross-compiler... no 
checking whether we are using GNU C... yes 
checking whether gcc accepts -g... yes 
updating cache ./config.cache 
creating ./config.status 
creating Makefile 

$ ls 
Makefile aclocal.m4 config.status hello.c mkinstalldirs 
Makefile.am config.cache configure install-sh 
Makefile.in config.log configure.in missing 

現在你的目录下已经产生了一个 Makefile 文件,输入make指令就可以编译 hello.c 了! 
% make 
gcc -DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c hello.c 
gcc -g -O2 -o hello hello.o 

你还可以试试 “make clean“,”make install“,”make dist“: 
[root@localhost hello]# make clean 
test -z "hello " || rm -f hello 
rm -f *.o core *.core 
[root@localhost hello]# make install 
gcc -DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c hello.c 
gcc -g -O2 -o hello hello.o 
make[1]: Entering directory `/home/joe/devel/hello' 
/bin/sh ./mkinstalldirs /usr/local/bin 
/usr/bin/install -c hello /usr/local/bin/hello 
make[1]: Nothing to be done for `install-data-am'. 
make[1]: Leaving directory `/home/joe/devel/hello' 
[root@localhost hello]# make dist 
rm -rf hello-1.0 
mkdir hello-1.0 
chmod 777 hello-1.0 
here=`cd . && pwd`; 
top_distdir=`cd hello-1.0 && pwd`; 
distdir=`cd hello-1.0 && pwd`; 
cd . 
&& Automake --include-deps --build-dir=$here --srcdir-name=. --output-dir=$top_distdir --foreign Makefile 
chmod -R a+r hello-1.0 
GZIP=--best gtar chozf hello-1.0.tar.gz hello-1.0 
rm -rf hello-1.0 

一切工作得很好! 当然,在make install时由于需要向系统目录拷贝文件,您需要有root权限。 

更进一步 
上述产生Makefile 的过程和以往自行编写的方式非常不一样,使用 Automake 只需用到一些已经定义好的宏就可以了。我们把宏及目标 (target)写在Makefile.am 文件内,Automake 读入 Makefile.am 文件后会把这一串已经定义好的宏展开并产生相对应的 
Makefile.in 文件,然后再由configure这个 shell script 根据 Makefile.in 产生合适的Makefile。 
具体流程如下所示: 
代码 --> [autoscan*] --> [configure.scan] --> configure.in 
configure.in --. .------> Autoconf* -----> configure 
+---+ 
[aclocal.m4] --+ `---. 
[acsite.m4] ---' | 
+--> [autoheader*] -> [config.h.in] 
[acconfig.h] ----. | 
+-----' 
[config.h.top] --+ 
[config.h.bot] --' 

Makefile.am --&#61664; [Autoconf*] -------> Makefile.in 

.-------------> config.cache 
configure* ------------+-------------> config.log 

[config.h.in] -. v .-> [config.h] -. 
+--> config.status* -+ +--> make* 
Makefile.in ---' `-> Makefile ---' 

上图表示在整个过程中要使用的文件及产生出来的文件,有星号 (*) 代表可执行文件。在此示例中可由 Autoconf 及 Automake 工具所产生的额外文件有 configure.scan、aclocal.m4、configure、Makefile.in,需要加入设置的有configure.in 及 Makefile.am。 开发者要书写的文件集中为confiugre.in和Makefile.am,在minigui项目中,我们把一系列的命令集中到一个批处理文件中:autogen.sh: 
#!/bin/sh 
aclocal 
autoheader 
Automake --add-missing 
Autoconf 

只要执行该批处理文件,结合configure.in和Makefile.am,就可以生成需要的Makefile了。 

编辑 configure.in 文件 
Autoconf 是用来产生 'configure'文件的工具。'configure' 是一个 shell script,它可以自动设定一些编译参数使程序能够条件编译以符合各种不同平台的Unix 系统。Autoconf会读取configure.in 文件然后产生'configure' 这个 shell script。 

configure.in 文件内容是一系列GNU m4 的宏,这些宏经Autoconf处理后会变成检查系统特性的shell scripts。 configure.in文件中宏的顺序并没有特别的规定,但是每一个configure.in 文件必须在所有其它宏前加入 AC_INIT 宏,然后在所有其它宏的最后加上 AC_OUTPUT宏。一般可先用 autoscan 扫描原始文件以产生一个 configure.scan 文件,再对 configure.scan 做些修改成 configure.in 文件。在例子中所用到的宏如下: 

dnl 
这个宏后面的内容不会被处理,可以视为注释 

AC_INIT(FILE) 
该宏用来检查源代码所在路径,autoscan 会自动产生,一般无须修改它。 

AM_INIT_AUTOMAKE(PACKAGE,VERSION) 
这个是使用 Automake 所必备的宏,PACKAGE 是所要产生软件的名称,VERSION 是版本编号。 

AC_PROG_CC 
检查系统可用的C编译器,若源代码是用C写的就需要这个宏。 

AC_OUTPUT(FILE) 
设置 configure 所要产生的文件,若是Makefile ,configure 便会把它检查出来的结果填充到Makefile.in 文件后产生合适的 Makefile。 

实际上,在使用 Automake 时,还需要一些其他的宏,这些额外的宏我们用 aclocal来帮助产生。执行 aclocal会产生aclocal.m4 文件,如果没有特别的用途,不需要修改它,用 aclocal 所产生的宏会告诉 Automake如何动作。 

有了 configure.in 及 aclocal.m4两个文件以后,便可以执行 Autoconf来产生 configure 文件了。 

编辑Makefile.am 文件 
接下来要编辑Makefile.am 文件,Automake 会根据 configure.in 中的宏并在perl的帮助下把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'、'hello.h' 三个文件的话,则定义 
hello_SOURCES= hello.c main.c hello.h 
如果定义多个执行文件,则对每个执行程序都要定义相对的filename_SOURCES。 

编辑好 Makefile.am 文件,就可以用 Automake --add-missing来产生 Makefile.in。加上 --add-missing 选项来告诉 Automake顺便加入包装一个软件所必须的文件,如果你不使用该选项,Automake可能会抱怨缺少了什么文件。Automake产生出來的 Makefile.in 文件是完全符合 GNU Makefile 惯例的,只要执行 configure这个shell 
script 便可以产生合适的 Makefile 文件了。 

使用 Makefile 
利用 configure 所产生的 Makefile文件有几个预先设定的目标可供使用,这里只用几个简述如下: 

make all 
产生设定的目标,既范例中的可执行文件。只敲入make 也可以,此时会开始编译源代码,然后连接并产生执行文件。 

make clean 
清除之前所编译的可执行文件及目标文件(object file, *.o)。 

make distclean 
除了清除可执行文件和目标文件以外,也把 configure 所产生的 Makefile 清除掉。 通常在发布软件前执行该命令。 

make install 
将程序安装到系统中,若源码编译成功,且执行结果正确,便可以把程序安装到系统预先设定的执行文件存放路径中,若用 bin_PROGRAMS 宏的话,程序会被安装到 /usr/local/bin下。 

make dist 
将程序和相关的文档包装为一个压缩文档以供发布 (distribution) 。执行完在目录下会产生一个以 
PACKAGE-VERSION.tar.gz 为名称的文件。PACKAGE 和 VERSION 这两个参数是根据 configure.in 文中 
AM_INIT_AUTOMAKE(PACKAGE, VERSION) 的定义。在我们的例子中会产生 'hello-1.0.tar.gz' 的文件。 

make distcheck 
和 make dist 类似,但是加入检查包装以后的压缩文件是否正常,这个目标除了把程序和相关文档包装成 tar.gz 文件外,还会自动把这个压缩文件解开,执行 configure,并执行 make all ,确认编译无错误以后,方显示这个 tar.gz 文件已经准备好并可以发布了。当你看到: 
========================================== 
hello-1.0.tar.gz is ready for distribution 
========================================== 

就可以放心地发布您的软件了,检查过关的套件,基本上可以给任何具备 GNU 开发环境的人去重新编译成功。 
要注意的是,利用 Autoconf 及 Automake 所产生出來的软件套件是可以在没有安装 Autoconf 及 Automake 的环境使用的,因为 configure 是一个 shell script,它己被设计为可以在一般 Unix 的 sh 这个 shell 下执行。但是如果要修改 configure.in 及 Makefile.am 文件再产生新的 configure 及 Makefile.in 文件时就一定要有 Autoconf 及 Automake 了。 

相关资料 
通常我们掌握了一些入门知识就可以开始实践了,在有新的需求时,参照相关的文档和别人的例子解决问题,在实践中不断提高。 
Autoconf 和 Automake 功能十分强大,可以从它们附带的 info 文档中找到详细的使用说明。或者您喜欢html,可以从gun站点上下载hmtl版本。你也可以从许多现有的GNU 软件或 Open Source 软件如Minigui中找到相关的 configure.in 或 Makefile.am 文件,他们是学习 Autoconf 及 Automake 更多技巧的最佳范例。
目录
相关文章
|
1月前
|
C语言
make的执行步骤以及常见的make命令,make distclean 以及和make clean的区别
make的执行步骤以及常见的make命令,make distclean 以及和make clean的区别
18 0
|
4月前
|
C++
make 及 make clean 的作用
make 及 make clean 的作用
34 0
|
6月前
|
C++
make的路径搜索
make的路径搜索
24 0
|
9月前
|
Linux
make/makefile的使用
make/makefile的使用
make menuconfig‘ requires the ncurses libraries.
make menuconfig‘ requires the ncurses libraries.
91 0
make[1]: Nothing to be done for `all-am'.
make[1]: Nothing to be done for `all-am'.
141 0
make[1]: Nothing to be done for `all-am&#39;.
make[1]: Nothing to be done for `all-am&#39;.
488 0
|
Unix Shell Linux
./configure、make、make install 命令详解
这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤 一、基本信息 1、./configure 是用来检测你的安装平台的目标特征的。
3740 0

热门文章

最新文章