how-to-write-makefile/overview.html

139 lines
10 KiB
HTML
Raw Normal View History

<!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>概述 &mdash; 跟我一起写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> &raquo;</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 environmentIDE都为你做了这个工作但我觉得要作一个好的和专业的程序员makefile还是要懂。这就好像现在有这么多的HTML编辑器但如果你想成为一个专业人士你还是要了解HTML的标签的含义。特别在Unix下的软件编译你就不能不自己写makefile了会不会写makefile从一个侧面说明了一个人是否具备完成大型工程的能力。</p>
<p>因为makefile关系到了整个工程的编译规则。一个工程中的源文件不计其数并且按类型、功能、模块分别放在若干个目录中makefile定义了一系列的规则来指定哪些文件需要先编译哪些文件需要后编译哪些文件需要重新编译甚至于进行更复杂的功能操作因为makefile就像一个Shell脚本一样其中也可以执行操作系统的命令。</p>
<p>makefile带来的好处就是——“自动化编译”一旦写好只需要一个make命令整个工程完全自动编译极大的提高了软件开发的效率。 make是一个命令工具是一个解释makefile中指令的命令工具一般来说大多数的IDE都有这个命令比如Delphi的makeVisual C++的nmakeLinux下GNU的make。可见makefile都成为了一种在工程方面的编译方法。</p>
<p>现在讲述如何写makefile的文章比较少这是我想写这篇文章的原因。当然不同产商的make各不相同也有不同的语法但其本质都是在 “文件依赖性”上做文章这里我仅对GNU的make进行讲述我的环境是RedHat Linux 8.0make的版本是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>&#169; 版权所有 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>