⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 蚂蚁公社 - 轻轻松松产生 makefile.htm

📁 有关Makefile生成的全攻略
💻 HTM
📖 第 1 页 / 共 3 页
字号:
      href="http://www.antpower.org/Folder_AntPoll/MPoll.2004-08-12.3415">察看结果</A>|<A 
      href="http://www.antpower.org/Folder_AntForum/Forum_SoftAndFramwork/msg_1072855174/mpoll_list">所有调查</A></DIV></DIV></DIV></DIV></DIV></TD>
    <TD class=main><!-- antpower -->
      <DIV class=spacer></DIV>
      <DIV id=contentTabs><A class=selected 
      href="http://www.antpower.org/Folder_AntForum/Forum_SoftAndFramwork/msg_1072855174/post_view">查看 
      </A><A class=plain 
      href="http://www.antpower.org/Folder_AntForum/Forum_SoftAndFramwork/msg_1072855174/forum_post_toForum">论坛首页 
      </A><A class=plain 
      href="http://www.antpower.org/Folder_AntForum/Forum_SoftAndFramwork/msg_1072855174/forum_post_toSearch">查找 
      </A></DIV>
      <DIV id=contentBar> </DIV>
      <DIV class=editableDocument style="FLOAT: left">
      <DIV id=content>
      <DIV class=documentActions><A href="javascript:this.print();"><IMG 
      title=打印本页面 alt=Print src="蚂蚁公社 - 轻轻松松产生 Makefile.files/print_icon.gif"> 
      </A>&nbsp;&nbsp;&nbsp;&nbsp; </DIV>
      <DIV>
      <DIV>[ <A 
      href="http://www.antpower.org/Folder_AntForum/Forum_SoftAndFramwork">主题家园</A> 
      | <A 
      href="http://www.antpower.org/Folder_AntForum/Forum_SoftAndFramwork/forum_search_form">查询</A> 
      ] </DIV><SPAN>
      <STYLE>.color1 {
	FONT-SIZE: 9pt; SCROLLBAR-ARROW-COLOR: #ffffff; SCROLLBAR-BASE-COLOR: #e7e3d6; BACKGROUND-COLOR: #e7e3d6
}
.color2 {
	FONT-SIZE: 9pt; SCROLLBAR-ARROW-COLOR: #ffffff; SCROLLBAR-BASE-COLOR: #e7e3d6; BACKGROUND-COLOR: #f0f0e0
}
</STYLE>
      </SPAN><SPAN class=message_header><SPAN><SPAN><B>前一条信息</B>: <A 
      href="http://www.antpower.org/Folder_AntForum/Forum_SoftAndFramwork/msg_1072853381">Linux下的动态连接库及其实现机制</A> 
      <BR></SPAN></SPAN></SPAN><BR><SPAN class=message_header>
      <DIV>
      <DIV class=label>标题</DIV>: <B>轻轻松松产生 Makefile </B></DIV>
      <TABLE cellSpacing=0 cellPadding=0 width=620 border=0>
        <TBODY>
        <TR class=color2 height=25>
          <TD align=middle width=50>
            <DIV><IMG alt=Post src="蚂蚁公社 - 轻轻松松产生 Makefile.files/icon_post.gif"> 
            <IMG alt="" src="蚂蚁公社 - 轻轻松松产生 Makefile.files/smiley3.gif">&nbsp; 
            </DIV></TD>
          <TD width=150><SPAN class=label>作者</SPAN> <FONT 
            color=#3366cc><B>chensy</B> </FONT></TD>
          <TD align=right><SPAN class=label>日期</SPAN> <FONT color=#3366cc>Dec 
            31, 2003 3:19 pm</FONT> </TD></TR>
        <TR>
          <TD colSpan=5 height=1></TD>
        <TR class="content even">
          <TD 
          style="PADDING-RIGHT: 2em; PADDING-LEFT: 2em; PADDING-BOTTOM: 0.5em; PADDING-TOP: 0.5em" 
          colSpan=4>
            <DIV class=message_body>
            <P>轻轻松松产生 Makefile 许明彦 <MYHSU@CYBERDUDE.COM>February 11, 1999 
            Abstract: 在 Unix 上写程式的人大概都碰过 Makefile,尤其是用 C 来开发程式的 人。用 make 
            来开发和编译程式的确很方便,可是要写出一个 Makefile 就不简单了。偏偏介绍 Makefile 的文件不多,GNU Make 
            那份印出来要几 百页的文件,光看完 Overview 就快阵亡了,难怪许多人闻 Unix 色变。 本文将介绍如何利用 GNU 
            Autoconf 及 Automake 这两套软体来协助我们 『自动』产生 Makefile 档,并且让开发出来的软体可以像 
            Apache, MySQL 和常见的 GNU 软体一样,只要会 ``./configure'', ``make'', ``make 
            install'' 就可以把程式安装到系统中。如果您有心开发 Open Source 的软体,或只是想在 Unix 
            系统下写写程式。希望这份介绍文件能 帮助您轻松地进入 Unix Programming 的殿堂。 1. 简介 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。 
            程式设计师只需写一些预先定义好的巨集 (macro),交给 Automake 处理 後会产生一个可供 Autoconf 使用的 
            Makefile.in 档。再配合利用 Autoconf 产生的自动设定档 configure 即可产生一份符合 GNU 
            Makefile 惯例的 Makeifle 了。 2. 上路之前 在开始试着用 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 档,FreeBSD 也有现成的 
            package 可以直接安装,或着你也 可以自行下载这些软体的原始档回来 DIY。以下的范例是在 Red Hat Linux 5.2 + 
            CLE2 的环境下所完成的。 </P>
            <OL>
              <LI>一个简单的例子 </LI></OL>
            <P>Automake 所产生的 Makefile 除了可以做到程式的编译和连结,也已经把 </P>
            <P>如何产生程式文件 (如 manual page, info 档及 dvi 档) 的动作,还有 </P>
            <P>把原始程式包装起来以供散 的动作都考虑进去了,所以原始程式所存放 </P>
            <P>的目录架构最好符合 GNU 的标准惯例,接下来我拿 hello.c 来做为例 </P>
            <P>子。 </P>
            <P>在工作目录下建立一个新的子目录 ``devel'',再在 devel 下建立一个 </P>
            <P>``hello'' 的子目录,这个目录将作为我们存放 hello 这个程式及其相关 </P>
            <P>档案的地方: </P>
            <P>% mkdir devel </P>
            <P>% cd devel </P>
            <P>% mkdir hello </P>
            <P>% cd hello </P>
            <P>用编辑器写个 hello.c 档, </P>
            <P>#include <STDIO.H></P>
            <P>int main(int argc, char** argv) </P>
            <P>{ </P>
            <P>printf(``Hello, GNU!\n''); </P>
            <P>return 0; </P>
            <P>} </P>
            <P>接下来就要用 Autoconf 及 Automake 来帮我们产生 Makefile 档了, </P>
            <OL>
              <LI>用 autoscan 产生一个 configure.in 的雏型,执行 autoscan 後会产 </LI></OL>
            <P>生一个configure.scan 的档案,我们可以用它做为 configure.in </P>
            <P>档的蓝本。 </P>
            <P>% autoscan </P>
            <P>% ls </P>
            <P>configure.scan hello.c </P>
            <OL>
              <LI>编辑 configure.scan 档,如下所示,并且把它的档名改成 </LI></OL>
            <P>configure.in </P>
            <P>dnl Process this file with autoconf to produce a con </P>
            <P>figure script. </P>
            <P>AC_INIT(hello.c) </P>
            <P>AM_INIT_AUTOMAKE(hello, 1.0) </P>
            <P>dnl Checks for programs. </P>
            <P>AC_PROG_CC </P>
            <P>dnl Checks for libraries. </P>
            <P>dnl Checks for header files. </P>
            <P>dnl Checks for typedefs, structures, and compiler ch </P>
            <P>aracteristics. </P>
            <P>dnl Checks for library functions. </P>
            <P>AC_OUTPUT(Makefile) </P>
            <OL>
              <LI>执行 aclocal 和 autoconf ,分别会产生 aclocal.m4 及 configure 两 </LI></OL>
            <P>个档案 </P>
            <P>% aclocal </P>
            <P>% autoconf </P>
            <P>% ls </P>
            <P>aclocal.m4 configure configure.in hello.c </P>
            <OL>
              <LI>编辑 Makefile.am 档,内容如下 </LI></OL>
            <P>AUTOMAKE_OPTIONS= foreign </P>
            <P>bin_PROGRAMS= hello </P>
            <P>hello_SOURCES= hello.c </P>
            <OL>
              <LI>执行 automake --add-missing ,Automake 会根据 Makefile.am 档产生 
            </LI></OL>
            <P>一些档案,包含最重要的 Makefile.in </P>
            <P>% automake --add-missing </P>
            <P>automake: configure.in: installing `./install-sh' </P>
            <P>automake: configure.in: installing `./mkinstalldirs' </P>
            <P>automake: configure.in: installing `./missing' </P>
            <OL>
              <LI>最後执行 ./configure , </LI></OL>
            <P>% ./configure </P>
            <P>creating cache ./config.cache </P>
            <P>checking for a BSD compatible install... /usr/bin/in </P>
            <P>stall -c </P>
            <P>checking whether build environment is sane... yes </P>
            <P>checking whether make sets ${MAKE}... yes </P>
            <P>checking for working aclocal... found </P>
            <P>checking for working autoconf... found </P>
            <P>checking for working automake... found </P>
            <P>checking for working autoheader... found </P>
            <P>checking for working makeinfo... found </P>
            <P>checking for gcc... gcc </P>
            <P>checking whether the C compiler (gcc ) works... yes </P>
            <P>checking whether the C compiler (gcc ) is a cross-co mpiler... no 
            </P>
            <P>checking whether we are using GNU C... yes </P>
            <P>checking whether gcc accepts -g... yes </P>
            <P>updating cache ./config.cache </P>
            <P>creating ./config.status </P>
            <P>creating Makefile </P>
            <P>现在你的目录下已经产生了一个 Makefile 档,下个 ``make'' 指令就可 </P>
            <P>以开始编译 hello.c 成执行档,执行 ./hello 和 GNU 打声招呼吧! </P>
            <P>% make </P>
            <P>gcc -DPACKAGE=\"hello\" -DVERSION=\"1.0\" -I. -I. -g -O2 -c he 
            </P>
            <P>llo.c </P>
            <P>gcc -g -O2 -o hello hello.o </P>
            <P>% ./hello </P>
            <P>Hello! GNU! </P>
            <P>你还可以试试 ``make clean'',''make install'',''make dist'' 看看 </P>
            <P>会有什麽结果。你也可以把产生出来的 Makefile 秀给你的老板,让他从 </P>
            <P>此对你刮目相看 :-) </P>
            <OL>
              <LI>一探究竟 </LI></OL>
            <P>上述产生 Makefile 的过程和以往自行编写的方式非常不一样,舍弃传统 </P>
            <P>自行定义 make 的规则,使用 Automake 只需用到一些已经定义好的巨集 </P>
            <P>即可。我们把巨集及目标 (target) 写在 Makefile.am 档内,Automake </P>
            <P>读入 Makefile.am 档後会把这一串已经定义好的巨集展开并且产生对应的 </P>
            <P>Makefile.in 档, 然後再由 configure 这个 shell script 根据 </P>
            <P>Makefile.in 产生适合的 Makefile。 </P>
            <P>[Figure 1:利用 autoconf 及 automake 产生 Makefile 的流程] </P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -