📄 xplore 查看主题 - linux 下编程的三个重要工具!.htm
字号:
来开发程序的人一般都遇到过 Makefile,用 make
来开发和编译程序的确很方便,可是要写出一个Makefile就不那么简单了。GNU Make
那份几百页的文件,让许多人害怕。当然,现在关于make的文档比较多,不过写一个Makefile总是一件很烦人的事情,GNU
Autoconf 及 Automake 这两个软件就是帮助程序开发者轻松产生Makefile
文件的。现在的GNU软件如Apache, MySQL
Minigui等都是利用Autoconf,Automake实现自动编译的。用户只要使用 “./configure”,
“make”, “make install” 就可以把程序安裝到系统中。 <BR><BR>简介 <BR>Makefile
基本上就是『目标』(target), 『关联』(dependencies) 和『动作』三者所组成的一系列规则。而 make
就是根据 Makefile 的规则决定如何编译 (compile) 和连接 (link) 程序或者其它动作。当然,make
可做的不只是编译和连接程序,例如 FreeBSD 的 port collection
中,Makefile还可以做到自动下载远程程序,解压缩 (extract) , 打补丁
(patch),设定,然后编译,安装到系统中。 <BR><BR>Makefile
基本结构虽然很简单,但是妥善运用这些规则就可以变换出许多不同的花样。却也因为这样,许多人刚开始学写Makefile
时会觉得没有规范可以遵循,每个人写出来的Makefile都不大一样,不知道从哪里下手,而且常常会受到开发环境的限制,只要环境参数不同或者路径更改,可能
Makefile 就得跟着修改。虽然有GNU Makefile Conventions (GNU
Makefile惯例)制订出一些在进行 GNU 程序设计时写 Makefile
的一些标准和规范,但是其内容很长而且很复杂,并且经常作一些调整,为了减轻程序开发人员维护Makefile
的负担,就出现了Automake。 <BR><BR>利用Automake,编程者只需要写一些预先定义好的宏
(macro),提交给Automake处理,就会产生一个可以供 Autoconf 使用的
Makefile.in文件。再配合使用 Autoconf产生的自动配置文件 configure 即可产生一份符合 GNU
Makefile 惯例的 Makeifle 了。 <BR><BR><BR> <BR><BR>需要的软件 <BR>在开始使用
Automake 之前,首先确认你的系统安装有如下软件: <BR><BR>1. GNU Automake <BR>2.
GNU Autoconf <BR>3. GNU m4 <BR>4. perl <BR>5. GNU Libtool
(如果你需要产生 shared library) <BR><BR>最好也使用 GNU C/C++ 编译器 、GNU Make
以及其它 GNU 的工具程序来作为开发的环境,这些工具都是属于 Open Source Software
不但免费而且功能强大。如果你是使用 Red Hat Linux 可以找到所有上述软件的 rpm 文件。
<BR><BR>一个简单的例子 <BR>Automake 所产生的 Makefile
除了可以做到程序的编译和连接,也可以用来生成文档(如 manual page, info
文件等),还可以有把源码文件包装起来以供发布,所以程序源代码所存放的目录结构最好符合GNU
的标准惯例,接下来就用一个hello.c 來做为例子。 <BR><BR>在工作目录下建立一个新的子目录devel,再在
devel 下建立一个"hello"' 的子目录,这个目录将 <BR>作为存放 hello这个程序及其相关文件的地方:
<BR><BR>% mkdir devel;cd devel;mkdir hello;cd hello
<BR><BR>用编辑器写一个hello.c文件, <BR><BR>#include <stdio.h>
<BR>int main(int argc, char** argv) <BR>{ <BR>printf(“Hello,
GNU!n”); <BR>return 0; <BR>} <BR><BR>接下来就要用 Autoconf 及
Automake 來产生 Makefile 文件了, <BR><BR>1. 用 autoscan 产生一个
configure.in 的原型,执行autoscan 后会产生一个configure.scan 的文件,可以用它作为
configure.in文件的蓝本。 <BR> <BR>% autoscan <BR>% ls
<BR>configure.scan hello.c <BR><BR>2. 编辑
configure.scan文件,如下所示,並且改名为configure.in <BR><BR>dnl Process
this file with Autoconf to produce a configure script.
<BR>AC_INIT(hello.c) <BR>AM_INIT_AUTOMAKE(hello, 1.0) <BR>dnl
Checks for programs. <BR>AC_PROG_CC <BR>dnl Checks for
libraries. <BR>dnl Checks for header files. <BR>dnl Checks for
typedefs, structures, and compiler characteristics. <BR>dnl
Checks for library functions. <BR>AC_OUTPUT(Makefile)
<BR><BR>3. 执行 aclocal 和 Autoconf ,分別会产生 aclocal.m4 及 configure
两个文件 <BR><BR>% aclocal <BR>% Autoconf <BR>% ls <BR>aclocal.m4
configure configure.in hello.c <BR><BR>4. 编辑 Makefile.am
文件,內容如下 <BR><BR>AUTOMAKE_OPTIONS= foreign <BR>bin_PROGRAMS=
hello <BR>hello_SOURCES= hello.c <BR><BR>5. 执行 Automake
--add-missing ,Automake 会根据Makefile.am
文件产生一些文件,包含最重要的Makefile.in <BR><BR>% Automake --add-missing
<BR>Automake: configure.in: installing `./install-sh'
<BR>Automake: configure.in: installing `./mkinstalldirs'
<BR>Automake: configure.in: installing `./missing' <BR><BR>6.
最后执行 ./configure: <BR><BR>% ./configure <BR>creating cache
./config.cache <BR>checking for a BSD compatible install...
/usr/bin/install -c <BR>checking whether build environment is
sane... yes <BR>checking whether make sets ${MAKE}... yes
<BR>checking for working aclocal... found <BR>checking for
working Autoconf... found <BR>checking for working Automake...
found <BR>checking for working autoheader... found
<BR>checking for working makeinfo... found <BR>checking for
gcc... gcc <BR>checking whether the C compiler (gcc ) works...
yes <BR>checking whether the C compiler (gcc ) is a
cross-compiler... no <BR>checking whether we are using GNU
C... yes <BR>checking whether gcc accepts -g... yes
<BR>updating cache ./config.cache <BR>creating ./config.status
<BR>creating Makefile <BR><BR>$ ls <BR>Makefile aclocal.m4
config.status hello.c mkinstalldirs <BR>Makefile.am
config.cache configure install-sh <BR>Makefile.in config.log
configure.in missing <BR><BR>現在你的目录下已经产生了一个 Makefile
文件,输入make指令就可以编译 hello.c 了! <BR><BR>% make <BR>gcc
-DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c hello.c
<BR>gcc -g -O2 -o hello hello.o <BR><BR>你还可以试试 “make
clean“,”make install“,”make dist“: <BR>[root@localhost hello]#
make clean <BR>test -z "hello " || rm -f hello <BR>rm -f *.o
core *.core <BR>[root@localhost hello]# make install <BR>gcc
-DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c hello.c
<BR>gcc -g -O2 -o hello hello.o <BR>make[1]: Entering
directory `/home/joe/devel/hello' <BR>/bin/sh ./mkinstalldirs
/usr/local/bin <BR>/usr/bin/install -c hello
/usr/local/bin/hello <BR>make[1]: Nothing to be done for
`install-data-am'. <BR>make[1]: Leaving directory
`/home/joe/devel/hello' <BR>[root@localhost hello]# make dist
<BR>rm -rf hello-1.0 <BR>mkdir hello-1.0 <BR>chmod 777
hello-1.0 <BR>here=`cd . && pwd`; <BR>top_distdir=`cd
hello-1.0 && pwd`; <BR>distdir=`cd hello-1.0
&& pwd`; <BR>cd . <BR>&& Automake
--include-deps --build-dir=$here --srcdir-name=.
--output-dir=$top_distdir --foreign Makefile <BR>chmod -R a+r
hello-1.0 <BR>GZIP=--best gtar chozf hello-1.0.tar.gz
hello-1.0 <BR>rm -rf hello-1.0 <BR>一切工作得很好! 当然,在make
install时由于需要向系统目录拷贝文件,您需要有root权限。 <BR><BR>更进一步
<BR>上述产生Makefile 的过程和以往自行编写的方式非常不一样,使用 Automake
只需用到一些已经定义好的宏就可以了。我们把宏及目标 (target)写在Makefile.am 文件内,Automake
读入 Makefile.am 文件后会把这一串已经定义好的宏展开并产生相对应的 <BR>Makefile.in
文件,然后再由configure这个 shell script 根据 Makefile.in 产生合适的Makefile。
<BR>具体流程如下所示: <BR>代码 --> [autoscan*] -->
[configure.scan] --> configure.in <BR>configure.in --.
.------> Autoconf* -----> configure <BR>+---+
<BR>[aclocal.m4] --+ `---. <BR>[acsite.m4] ---' | <BR>+-->
[autoheader*] -> [config.h.in] <BR>[acconfig.h] ----. |
<BR>+-----' <BR>[config.h.top] --+ <BR>[config.h.bot] --'
<BR><BR>Makefile.am -- [Autoconf*] -------> Makefile.in
<BR><BR>.-------------> config.cache <BR>configure*
------------+-------------> config.log <BR>|
<BR>[config.h.in] -. v .-> [config.h] -. <BR>+-->
config.status* -+ +--> make* <BR>Makefile.in ---' `->
Makefile ---' <BR><BR>上图表示在整个过程中要使用的文件及产生出来的文件,有星号 (*)
代表可执行文件。在此示例中可由 Autoconf 及 Automake 工具所产生的额外文件有
configure.scan、aclocal.m4、configure、Makefile.in,需要加入设置的有configure.in
及
Makefile.am。开发者要书写的文件集中为confiugre.in和Makefile.am,在minigui项目中,我们把一系列的命令集中到一个批处理文件中:
autogen.sh: <BR><BR>#!/bin/sh <BR>aclocal <BR>autoheader
<BR>Automake --add-missing <BR>Autoconf
<BR><BR>只要执行该批处理文件,结合configure.in和Makefile.am,就可以生成需要的Makefile了。
<BR><BR>编辑 configure.in 文件 <BR>Autoconf 是用来产生
'configure'文件的工具。'configure' 是一个 shell
script,它可以自动设定一些编译参数使程序能够条件编译以符合各种不同平台的Unix
系统。Autoconf会读取configure.in 文件然后产生'configure' 这个 shell script。
<BR><BR>configure.in 文件内容是一系列GNU m4
的宏,这些宏经Autoconf处理后会变成检查系统特性的shell scripts。
configure.in文件中宏的顺序并没有特别的规定,但是每一个configure.in 文件必须在所有其它宏前加入
AC_INIT 宏,然后在所有其它宏的最后加上 AC_OUTPUT宏。一般可先用 autoscan 扫描原始文件以产生一个
configure.scan 文件,再对 configure.scan 做些修改成 configure.in
文件。在例子中所用到的宏如下: <BR><BR>dnl <BR>这个宏后面的内容不会被处理,可以视为注释
<BR><BR>AC_INIT(FILE) <BR>该宏用来检查源代码所在路径,autoscan
会自动产生,一般无须修改它。 <BR><BR>AM_INIT_AUTOMAKE(PACKAGE,VERSION)
<BR>这个是使用 Automake 所必备的宏,PACKAGE 是所要产生软件的名称,VERSION 是版本编号。
<BR><BR>AC_PROG_CC <BR>检查系统可用的C编译器,若源代码是用C写的就需要这个宏。
<BR><BR>AC_OUTPUT(FILE) <BR>设置 configure 所要产生的文件,若是Makefile
,configure 便会把它检查出来的结果填充到Makefile.in 文件后产生合适的 Makefile。
<BR><BR>实际上,在使用 Automake 时,还需要一些其他的宏,这些额外的宏我们用 aclocal来帮助产生。执行
aclocal会产生aclocal.m4 文件,如果没有特别的用途,不需要修改它,用 aclocal 所产生的宏会告诉
Automake如何动作。 <BR><BR>有了 configure.in 及 aclocal.m4两个文件以后,便可以执行
Autoconf来产生 configure 文件了。 <BR><BR>编辑Makefile.am 文件
<BR>接下来要编辑Makefile.am 文件,Automake 会根据 configure.in
中的宏并在perl的帮助下把Makefile.am 转成 Makefile.in 文件。 Makefile.am
文件定义所要产生的目标: <BR><BR>AUTOMAKE_OPTIONS <BR>设置 Automake
的选项。Automake 主要是帮助开发 GNU 软件的人员来维护软件,所以在执行Automake
时,会检查目录下是否存在标准 GNU 软件中应具备的文件,例如 'NEWS'、'AUTHOR'、
<BR>'ChangeLog' 等文件。设置为foreign 时,Automake 会改用一般软件的标准来检查。
<BR><BR>bin_PROGRAMS <BR>定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空白符隔开。
<BR><BR>hello_SOURCES <BR>定义 'hello' 这个执行程序所需要的原始文件。如果
'hello'这个程序是由多个原始文件所产生, <BR>必須把它所用到的所有原始文件都列出来,以空白符隔开。假设
'hello' 还需要 'hello.c'、'main.c'、'hello.h' 三个文件的话,则定义
<BR>hello_SOURCES= hello.c main.c hello.h
<BR>如果定义多个执行文件,则对每个执行程序都要定义相对的filename_SOURCES。 <BR><BR>编辑好
Makefile.am 文件,就可以用 Automake --add-missing来产生 Makefile.in。加上
--add-missing 选项来告诉
Automake顺便加入包装一个软件所必须的文件,如果你不使用该选项,Automake可能会抱怨缺少了什么文件。Automake产生出來的
Makefile.in 文件是完全符合 GNU Makefile 惯例的,只要执行 configure这个shell
<BR>script 便可以产生合适的 Makefile 文件了。 <BR><BR>使用 Makefile <BR>利用
configure 所产生的 Makefile文件有几个预先设定的目标可供使用,这里只用几个简述如下:
<BR><BR>make all <BR>产生设定的目标,既范例中的可执行文件。只敲入make
也可以,此时会开始编译源代码,然后连接并产生执行文件。 <BR><BR>make clean
<BR>清除之前所编译的可执行文件及目标文件(object file, *.o)。 <BR><BR>make
distclean <BR>除了清除可执行文件和目标文件以外,也把 configure 所产生的 Makefile 清除掉。
通常在发布软件前执行该命令。 <BR><BR>make install
<BR>将程序安装到系统中,若源码编译成功,且执行结果正确,便可以把程序安装到系统预先设定的执行文件存放路径中,若用
bin_PROGRAMS 宏的话,程序会被安装到 /usr/local/bin下。 <BR><BR>make dist
<BR>将程序和相关的文档包装为一个压缩文档以供发布 (distribution) 。执行完在目录下会产生一个以
<BR>PACKAGE-VERSION.tar.gz 为名称的文件。PACKAGE 和 VERSION 这两个参数是根据
configure.in 文中 <BR>AM_INIT_AUTOMAKE(PACKAGE, VERSION)
的定义。在我们的例子中会产生 'hello-1.0.tar.gz' 的文件。 <BR><BR>make distcheck
<BR>和 make dist 类似,但是加入检查包装以后的压缩文件是否正常,这个目标除了把程序和相关文档包装成
tar.gz 文件外,还会自动把这个压缩文件解开,执行 configure,并执行 make all
,确认编译无错误以后,方显示这个 tar.gz 文件已经准备好并可以发布了。当你看到:
<BR>==========================================
<BR>hello-1.0.tar.gz is ready for distribution
<BR>==========================================
<BR><BR>就可以放心地发布您的软件了,检查过关的套件,基本上可以给任何具备 GNU 开发环境的人去重新编译成功。
<BR>要注意的是,利用 Autoconf 及 Automake 所产生出來的软件套件是可以在没有安装 Autoconf 及
Automake 的环境使用的,因为 configure 是一个 shell script,它己被设计为可以在一般 Unix
的 sh 这个 shell 下执行。但是如果要修改 configure.in 及 Makefile.am 文件再产生新的
configure 及 Makefile.in 文件时就一定要有 Autoconf 及 Automake 了。
<BR><BR>相关资料
<BR>通常我们掌握了一些入门知识就可以开始实践了,在有新的需求时,参照相关的文档和别人的例子解决问题,在实践中不断提高。
<BR>Autoconf 和 Automake 功能十分强大,可以从它们附带的 info
文档中找到详细的使用说明。或者您喜欢html,可以从gun站点上下载hmtl版本。你也可以从许多现有的GNU 软件或
Open Source 软件如Minigui中找到相关的 configure.in 或 Makefile.am
文件,他们是学习 Autoconf 及 Automake 更多技巧的最佳范例。</SPAN><SPAN
class=postbody><BR>_________________<BR><SPAN
style="COLOR: red">女友对我说:“如果秋天过去了,我会在雪地爱你,如果世界消失了,我会在天堂爱你,如果你消失了,我会在泪水中爱你,如果我消失了,我会在心中爱你!”</SPAN>
<BR><IMG
src="Xplore 查看主题 - Linux 下编程的三个重要工具!.files/sign_mlsx.gif"
border=0> <BR><A class=postlink href="http://mlsx.xplore.cn/"
target=_blank>http://mlsx.xplore.cn/</A></SPAN><SPAN
class=gensmall></SPAN></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=row1 vAlign=center align=left width=150><SPAN class=nav><A
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -