📄 1038.html
字号:
</font></div>
</td>
</tr>
<tr>
<td noWrap>
<div align="center">
<input maxlength=100 size=30 name=keyword2>
</div>
</td>
</tr></tbody>
</table>
</form>
</TD>
<TD rowSpan=2><IMG src="images/header_r1_c7.gif" tppabs="http://www.linuxhero.com/docs/images/header_r1_c7.gif" width=26 border=0 name=header_r1_c7></TD>
<TD><IMG height=83 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1 border=0></TD></TR>
<TR>
<TD background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif"><IMG height=22
src="images/header_r2_c1.gif" tppabs="http://www.linuxhero.com/docs/images/header_r2_c1.gif" width=296 border=0
name=header_r2_c1></TD>
<TD background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colSpan=5>
<DIV align=right><FONT class=normalfont>当前位置:
<A href="index.html" tppabs="http://www.linuxhero.com/docs/index.html">本站首页</A>
<font color="#FF6699">>></font>
<A href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</A> | <A href="copyright.html" tppabs="http://www.linuxhero.com/docs/copyright.html">版权说明</A></font></DIV>
</TD>
<TD><IMG height=22 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=10 cellPadding=0 width="100%" bgColor=#ffffff
border=0>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%" border=0>
<TR>
<TD vAlign=top align=middle width="60%">
<TABLE cellSpacing=0 cellPadding=0 width="100%"
background="images/back.gif" tppabs="http://www.linuxhero.com/docs/images/back.gif" border=0>
<TBODY>
<TR>
<TD vAlign=top width="80%">
<DIV align=center>
<FORM action="search.html" tppabs="http://www.linuxhero.com/docs/search.html" method=get>
</FORM>
<TABLE cellSpacing=0 cellPadding=0 width="95%"
border=0><TBODY>
<TR>
<TD background="images/bgi.gif" tppabs="http://www.linuxhero.com/docs/images/bgi.gif"
height=30></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=3 width="95%"
align=center border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%"
border=0>
<TBODY>
<TR>
<TD vAlign=top>
<p><FONT class=normalfont><B><font color=blue>makefile写法</font></B></FONT><BR><FONT class=smallfont color=#ff9900>2004-04-23 15:18 pm</FONT><BR><FONT class=normalfont>作者:许明彦<br>来自:Linux知识宝库<br>联系方式:无名<br><br>Abstract:<br>
<br>
在 Unix 上写程式的人大概都碰过 Makefile,尤其是用 C 来开发程式的人。用 make来开发和编译程式的确很方便,可是要写出一个 Makefile就不简单了。偏偏介绍 Makefile 的文件不多,GNU Make 那份印出来要几百页的文件,光看完 Overview 就快阵亡了,难怪许多<br>
人闻 Unix 色变。<br>
<br>
本文将介绍如何利用 GNU Autoconf 及 Automake 这两套软体来协助我们『自动』产生 Makefile 档,并且让开发出来的软体可以像 Apache, MySQL 和常见的 GNU 软体一样,只要会 ``./configure'', ``make'', ``make install'' 就可以把程式安装到系统中。如果您有<br>
心开发 Open Source 的软体,或只是想在 Unix 系统下写写程式。希望这份介绍文件能帮助您轻松地进入 Unix Programming 的殿堂。<br>
<br>
1. 简介<br>
<br>
Makefile 基本上就是『目标』(target), 『关连』(dependencies) 和『动作』三者所组成的一连串规则。而 make 就会根据 Makefile 的规则来决定如何编译 (compile) 和连结 (link) 程式。实际上,make 可做的不只是编译和连结程式,例如 FreeBSD 的 port collect<br>
ion 中, Makefile 还可以做到自动下载原始程式套件,解压缩 (extract) ,修补 (patch),设定,然後编译,安装至系统中。<br>
<br>
Makefile 基本构造虽然简单,但是妥善运用这些规则就也可以变出许多不同的花招。却也因此,许多刚开始学习写 Makefile 时会感到没有规范可循,每个人写出来的 Makefile 长得都不太一样,不知道从何下手,而且常常会受限於自己的开发环境,只要环境变数不同或路<br>
径改一下,可能Makefile 就得跟着修改。虽然有 GNU Makefile Conventions (GNU Makefile 惯例) 订出一些使用 GNU 程式设计时撰写 Makefile 的一些标准和规范,但是内容很长而且很复杂, 并且经常做些调整,为了减轻程式设计师维护 Makefile 的负担,因此有了Automake。<br>
<br>
程式设计师只需写一些预先定义好的巨集 (macro),交给 Automake 处理後会产生一个可供Autoconf 使用的 Makefile.in 档。再配合利用Autoconf 产生的自动设定档 configure即可产生一份符合 GNU Makefile惯例的 Makeifle 了。<br>
<br>
2. 上路之前<br>
<br>
在开始试着用 Automake 之前,请先确认你的系统已经安装以下的软体:<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 档,FreeBSD 也有现成的 package 可以直<br>
接安装,或着你也可以自行下载这些软体的原始档回来 DIY。以下的范例是在 Red Hat linux 5.2 + CLE2 的环境下所完成的。<br>
<br>
3. 一个简单的例子<br>
<br>
Automake 所产生的 Makefile 除了可以做到程式的编译和连结,也已经把如何产生程式文件(如 manual page, info 档及 dvi 档) 的动作,还有把原始程式包装起来以供散 的动作都考虑进去了,所以原始程式所存放的目录架构最好符合 GNU 的标准惯例,接下来我拿hello.c 来做为例子。<br>
<br>
在工作目录下建立一个新的子目录 ``devel'',再在 devel 下建立一个``hello'' 的子目录,这个目录将作为我们存放 hello 这个程式及其相关档案的地方:<br>
<br>
% mkdir devel<br>
% cd devel<br>
% mkdir hello<br>
% cd hello<br>
<br>
用编辑器写个 hello.c 档,<br>
#include stdio.h<br>
int main(int argc, char** argv)<br>
{<br>
printf(``Hello, GNU! '');<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>
dnl Process this file with autoconf to produce a con figure 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 ch aracteristics.<br>
dnl Checks for library functions.<br>
AC_OUTPUT(Makefile)<br>
<br>
3. 执行 aclocal 和 autoconf ,分别会产生 aclocal.m4 及 configure 两个档案<br>
% aclocal<br>
% autoconf<br>
% ls<br>
aclocal.m4 configure configure.in hello.c<br>
<br>
4. 编辑 Makefile.am 档,内容如下<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>
% 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>
% ./configure<br>
creating cache ./config.cache<br>
checking for a BSD compatible install... /usr/bin/in stall -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-co mpiler... 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>
现在你的目录下已经产生了一个 Makefile 档,下个 ``make'' 指令就可以开始编译 hello.c 成执行档,执行 ./hello 和 GNU 打声招呼吧!<br>
<br>
% make<br>
gcc -DPACKAGE="hello" -DVERSION="1.0" -I. -I. -g -O2 -c he llo.c<br>
gcc -g -O2 -o hello hello.o<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -