139 lines
10 KiB
HTML
139 lines
10 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html class="writer-html5" lang="zh-CN" >
|
|||
|
<head>
|
|||
|
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
|||
|
|
|||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|||
|
<title>概述 — 跟我一起写Makefile 1.0 文档</title>
|
|||
|
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
|||
|
<!--[if lt IE 9]>
|
|||
|
<script src="_static/js/html5shiv.min.js"></script>
|
|||
|
<![endif]-->
|
|||
|
|
|||
|
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
|
|||
|
<script src="_static/jquery.js"></script>
|
|||
|
<script src="_static/underscore.js"></script>
|
|||
|
<script src="_static/doctools.js"></script>
|
|||
|
<script src="_static/translations.js"></script>
|
|||
|
<script src="_static/js/theme.js"></script>
|
|||
|
<link rel="index" title="索引" href="genindex.html" />
|
|||
|
<link rel="search" title="搜索" href="search.html" />
|
|||
|
<link rel="next" title="makefile介绍" href="introduction.html" />
|
|||
|
<link rel="prev" title="跟我一起写Makefile" href="index.html" />
|
|||
|
</head>
|
|||
|
|
|||
|
<body class="wy-body-for-nav">
|
|||
|
<div class="wy-grid-for-nav">
|
|||
|
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
|||
|
<div class="wy-side-scroll">
|
|||
|
<div class="wy-side-nav-search" >
|
|||
|
<a href="index.html" class="icon icon-home"> 跟我一起写Makefile
|
|||
|
</a>
|
|||
|
<div class="version">
|
|||
|
1.0
|
|||
|
</div>
|
|||
|
<div role="search">
|
|||
|
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
|||
|
<input type="text" name="q" placeholder="Search docs" />
|
|||
|
<input type="hidden" name="check_keywords" value="yes" />
|
|||
|
<input type="hidden" name="area" value="default" />
|
|||
|
</form>
|
|||
|
</div>
|
|||
|
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
|
|||
|
<p class="caption" role="heading"><span class="caption-text">目录</span></p>
|
|||
|
<ul class="current">
|
|||
|
<li class="toctree-l1 current"><a class="current reference internal" href="#">概述</a><ul>
|
|||
|
<li class="toctree-l2"><a class="reference internal" href="#id2">关于程序的编译和链接</a></li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="introduction.html">makefile介绍</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="rules.html">书写规则</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="recipes.html">书写命令</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="variables.html">使用变量</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="conditionals.html">使用条件判断</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="functions.html">使用函数</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="invoke.html">make 的运行</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="implicit_rules.html">隐含规则</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="archives.html">使用make更新函数库文件</a></li>
|
|||
|
<li class="toctree-l1"><a class="reference internal" href="postscript.html">后序</a></li>
|
|||
|
</ul>
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</nav>
|
|||
|
|
|||
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
|
|||
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|||
|
<a href="index.html">跟我一起写Makefile</a>
|
|||
|
</nav>
|
|||
|
|
|||
|
<div class="wy-nav-content">
|
|||
|
<div class="rst-content">
|
|||
|
<div role="navigation" aria-label="Page navigation">
|
|||
|
<ul class="wy-breadcrumbs">
|
|||
|
<li><a href="index.html" class="icon icon-home"></a> »</li>
|
|||
|
<li>概述</li>
|
|||
|
<li class="wy-breadcrumbs-aside">
|
|||
|
<a href="_sources/overview.rst.txt" rel="nofollow"> View page source</a>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<hr/>
|
|||
|
</div>
|
|||
|
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
|||
|
<div itemprop="articleBody">
|
|||
|
|
|||
|
<section id="id1">
|
|||
|
<h1>概述<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h1>
|
|||
|
<p>什么是makefile?或许很多Windows的程序员都不知道这个东西,因为那些Windows的集成开发环境(integrated development environment,IDE)都为你做了这个工作,但我觉得要作一个好的和专业的程序员,makefile还是要懂。这就好像现在有这么多的HTML编辑器,但如果你想成为一个专业人士,你还是要了解HTML的标签的含义。特别在Unix下的软件编译,你就不能不自己写makefile了,会不会写makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。</p>
|
|||
|
<p>因为,makefile关系到了整个工程的编译规则。一个工程中的源文件不计其数,并且按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。</p>
|
|||
|
<p>makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。 make是一个命令工具,是一个解释makefile中指令的命令工具,一般来说,大多数的IDE都有这个命令,比如:Delphi的make,Visual C++的nmake,Linux下GNU的make。可见,makefile都成为了一种在工程方面的编译方法。</p>
|
|||
|
<p>现在讲述如何写makefile的文章比较少,这是我想写这篇文章的原因。当然,不同产商的make各不相同,也有不同的语法,但其本质都是在 “文件依赖性”上做文章,这里,我仅对GNU的make进行讲述,我的环境是RedHat Linux 8.0,make的版本是3.80。毕竟,这个make是应用最为广泛的,也是用得最多的。而且其还是最遵循于IEEE 1003.2-1992标准的(POSIX.2)。</p>
|
|||
|
<p>在这篇文档中,将以C/C++的源码作为基础,所以必然涉及一些关于C/C++的编译的知识。关于这方面的内容,还请各位查看相关的编译器的文档。这里所默认的编译器是UNIX下的GCC和CC。</p>
|
|||
|
<section id="id2">
|
|||
|
<h2>关于程序的编译和链接<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h2>
|
|||
|
<p>在此,我想多说关于程序编译的一些规范和方法。一般来说,无论是C还是C++,首先要把源文件编译成中间代码文件,在Windows下也就是 <code class="docutils literal notranslate"><span class="pre">.obj</span></code> 文件,UNIX下是 <code class="docutils literal notranslate"><span class="pre">.o</span></code> 文件,即Object File,这个动作叫做编译(compile)。然后再把大量的Object File合成执行文件,这个动作叫作链接(link)。</p>
|
|||
|
<p>编译时,编译器需要的是语法的正确,函数与变量的声明的正确。对于后者,通常是你需要告诉编译器头文件的所在位置(头文件中应该只是声明,而定义应该放在C/C++文件中),只要所有的语法正确,编译器就可以编译出中间目标文件。一般来说,每个源文件都应该对应于一个中间目标文件( <code class="docutils literal notranslate"><span class="pre">.o</span></code> 文件或
|
|||
|
<code class="docutils literal notranslate"><span class="pre">.obj</span></code> 文件)。</p>
|
|||
|
<p>链接时,主要是链接函数和全局变量。所以,我们可以使用这些中间目标文件( <code class="docutils literal notranslate"><span class="pre">.o</span></code> 文件或
|
|||
|
<code class="docutils literal notranslate"><span class="pre">.obj</span></code> 文件)来链接我们的应用程序。链接器并不管函数所在的源文件,只管函数的中间目标文件(Object File),在大多数时候,由于源文件太多,编译生成的中间目标文件太多,而在链接时需要明显地指出中间目标文件名,这对于编译很不方便。所以,我们要给中间目标文件打个包,在Windows下这种包叫“库文件”(Library File),也就是 <code class="docutils literal notranslate"><span class="pre">.lib</span></code> 文件,在UNIX下,是Archive File,也就是
|
|||
|
<code class="docutils literal notranslate"><span class="pre">.a</span></code> 文件。</p>
|
|||
|
<p>总结一下,源文件首先会生成中间目标文件,再由中间目标文件生成执行文件。在编译时,编译器只检测程序语法和函数、变量是否被声明。如果函数未被声明,编译器会给出一个警告,但可以生成Object File。而在链接程序时,链接器会在所有的Object File中找寻函数的实现,如果找不到,那到就会报链接错误码(Linker Error),在VC下,这种错误一般是: <code class="docutils literal notranslate"><span class="pre">Link</span> <span class="pre">2001错误</span></code> ,意思说是说,链接器未能找到函数的实现。你需要指定函数的Object File。</p>
|
|||
|
<p>好,言归正传,gnu的make有许多的内容,闲言少叙。</p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
|
|||
|
<a href="index.html" class="btn btn-neutral float-left" title="跟我一起写Makefile" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
|
|||
|
<a href="introduction.html" class="btn btn-neutral float-right" title="makefile介绍" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
|||
|
</div>
|
|||
|
|
|||
|
<hr/>
|
|||
|
|
|||
|
<div role="contentinfo">
|
|||
|
<p>© 版权所有 2014-2019, 作者:陈皓;排版:SeisMan.
|
|||
|
<span class="lastupdated">最后更新于 2022年5月22日.
|
|||
|
</span></p>
|
|||
|
</div>
|
|||
|
|
|||
|
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
|
|||
|
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
|
|||
|
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
|||
|
|
|||
|
|
|||
|
</footer>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
</div>
|
|||
|
<script>
|
|||
|
jQuery(function () {
|
|||
|
SphinxRtdTheme.Navigation.enable(true);
|
|||
|
});
|
|||
|
</script>
|
|||
|
|
|||
|
</body>
|
|||
|
</html>
|