270 lines
23 KiB
HTML
Raw Permalink 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="使用变量" href="variables.html" />
<link rel="prev" title="书写规则" href="rules.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"><a class="reference internal" href="overview.html">概述</a></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 current"><a class="current reference internal" href="#">书写命令</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#id2">显示命令</a></li>
<li class="toctree-l2"><a class="reference internal" href="#id3">命令执行</a></li>
<li class="toctree-l2"><a class="reference internal" href="#id4">命令出错</a></li>
<li class="toctree-l2"><a class="reference internal" href="#make">嵌套执行make</a></li>
<li class="toctree-l2"><a class="reference internal" href="#id5">定义命令包</a></li>
</ul>
</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/recipes.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>每条规则中的命令和操作系统Shell的命令行是一致的。make会一按顺序一条一条的执行命令每条命令的开头必须以 <code class="docutils literal notranslate"><span class="pre">Tab</span></code> 键开头除非命令是紧跟在依赖规则后面的分号后的。在命令行之间中的空格或是空行会被忽略但是如果该空格或空行是以Tab键开头的那么make会认为其是一个空命令。</p>
<p>我们在UNIX下可能会使用不同的Shell但是make的命令默认是被 <code class="docutils literal notranslate"><span class="pre">/bin/sh</span></code> ——UNIX的标准Shell
解释执行的。除非你特别指定一个其它的Shell。Makefile中 <code class="docutils literal notranslate"><span class="pre">#</span></code> 是注释符很像C/C++中的
<code class="docutils literal notranslate"><span class="pre">//</span></code> ,其后的本行字符都被注释。</p>
<section id="id2">
<h2>显示命令<a class="headerlink" href="#id2" title="永久链接至标题"></a></h2>
<p>通常make会把其要执行的命令行在命令执行前输出到屏幕上。当我们用 <code class="docutils literal notranslate"><span class="pre">&#64;</span></code> 字符在命令行前那么这个命令将不被make显示出来最具代表性的例子是我们用这个功能来向屏幕显示一些信息。如:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@echo</span> <span class="n">正在编译XXX模块</span><span class="o">......</span>
</pre></div>
</div>
<p>当make执行时会输出“正在编译XXX模块……”字串但不会输出命令如果没有“&#64;那么make将输出:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">echo</span> <span class="n">正在编译XXX模块</span><span class="o">......</span>
<span class="n">正在编译XXX模块</span><span class="o">......</span>
</pre></div>
</div>
<p>如果make执行时带入make参数 <code class="docutils literal notranslate"><span class="pre">-n</span></code><code class="docutils literal notranslate"><span class="pre">--just-print</span></code> 那么其只是显示命令但不会执行命令这个功能很有利于我们调试我们的Makefile看看我们书写的命令是执行起来是什么样子的或是什么顺序的。</p>
<p>而make参数 <code class="docutils literal notranslate"><span class="pre">-s</span></code><code class="docutils literal notranslate"><span class="pre">--silent</span></code><code class="docutils literal notranslate"><span class="pre">--quiet</span></code> 则是全面禁止命令的显示。</p>
</section>
<section id="id3">
<h2>命令执行<a class="headerlink" href="#id3" title="永久链接至标题"></a></h2>
<p>当依赖目标新于目标时也就是当规则的目标需要被更新时make会一条一条的执行其后的命令。需要注意的是如果你要让上一条命令的结果应用在下一条命令时你应该使用分号分隔这两条命令。比如你的第一条命令是cd命令你希望第二条命令得在cd之后的基础上运行那么你就不能把这两条命令写在两行上而应该把这两条命令写在一行上用分号分隔。如</p>
<ul class="simple">
<li><p>示例一:</p></li>
</ul>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nf">exec</span><span class="o">:</span>
<span class="nb">cd</span> /home/hchen
<span class="nb">pwd</span>
</pre></div>
</div>
<ul class="simple">
<li><p>示例二:</p></li>
</ul>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nf">exec</span><span class="o">:</span>
<span class="nb">cd</span> /home/hchen<span class="p">;</span> <span class="nb">pwd</span>
</pre></div>
</div>
<p>当我们执行 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">exec</span></code>第一个例子中的cd没有作用pwd会打印出当前的Makefile目录而第二个例子中cd就起作用了pwd会打印出“/home/hchen”。</p>
<p>make一般是使用环境变量SHELL中所定义的系统Shell来执行命令默认情况下使用UNIX的标准Shell——/bin/sh来执行命令。但在MS-DOS下有点特殊因为MS-DOS下没有SHELL环境变量当然你也可以指定。如果你指定了UNIX风格的目录形式首先make会在SHELL所指定的路径中找寻命令解释器如果找不到其会在当前盘符中的当前目录中寻找如果再找不到其会在PATH环境变量中所定义的所有路径中寻找。MS-DOS中如果你定义的命令解释器没有找到其会给你的命令解释器加上诸如 <code class="docutils literal notranslate"><span class="pre">.exe</span></code>
<code class="docutils literal notranslate"><span class="pre">.com</span></code><code class="docutils literal notranslate"><span class="pre">.bat</span></code><code class="docutils literal notranslate"><span class="pre">.sh</span></code> 等后缀。</p>
</section>
<section id="id4">
<h2>命令出错<a class="headerlink" href="#id4" title="永久链接至标题"></a></h2>
<p>每当命令运行完后make会检测每个命令的返回码如果命令返回成功那么make会执行下一条命令当规则中所有的命令成功返回后这个规则就算是成功完成了。如果一个规则中的某个命令出错了命令退出码非零那么make就会终止执行当前规则这将有可能终止所有规则的执行。</p>
<p>有些时候命令的出错并不表示就是错误的。例如mkdir命令我们一定需要建立一个目录如果目录不存在那么mkdir就成功执行万事大吉如果目录存在那么就出错了。我们之所以使用mkdir的意思就是一定要有这样的一个目录于是我们就不希望mkdir出错而终止规则的运行。</p>
<p>为了做到这一点忽略命令的出错我们可以在Makefile的命令行前加一个减号 <code class="docutils literal notranslate"><span class="pre">-</span></code> 在Tab键之后标记为不管命令出不出错都认为是成功的。如</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>clean:
-rm -f *.o
</pre></div>
</div>
<p>还有一个全局的办法是给make加上 <code class="docutils literal notranslate"><span class="pre">-i</span></code> 或是 <code class="docutils literal notranslate"><span class="pre">--ignore-errors</span></code> 参数那么Makefile中所有命令都会忽略错误。而如果一个规则是以 <code class="docutils literal notranslate"><span class="pre">.IGNORE</span></code> 作为目标的,那么这个规则中的所有命令将会忽略错误。这些是不同级别的防止命令出错的方法,你可以根据你的不同喜欢设置。</p>
<p>还有一个要提一下的make的参数的是 <code class="docutils literal notranslate"><span class="pre">-k</span></code> 或是 <code class="docutils literal notranslate"><span class="pre">--keep-going</span></code> ,这个参数的意思是,如果某规则中的命令出错了,那么就终止该规则的执行,但继续执行其它规则。</p>
</section>
<section id="make">
<h2>嵌套执行make<a class="headerlink" href="#make" title="永久链接至标题"></a></h2>
<p>在一些大的工程中我们会把我们不同模块或是不同功能的源文件放在不同的目录中我们可以在每个目录中都书写一个该目录的Makefile这有利于让我们的Makefile变得更加地简洁而不至于把所有的东西全部写在一个Makefile中这样会很难维护我们的Makefile这个技术对于我们模块编译和分段编译有着非常大的好处。</p>
<p>例如我们有一个子目录叫subdir这个目录下有个Makefile文件来指明了这个目录下文件的编译规则。那么我们总控的Makefile可以这样书写</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nf">subsystem</span><span class="o">:</span>
<span class="nb">cd</span> subdir <span class="o">&amp;&amp;</span> <span class="k">$(</span>MAKE<span class="k">)</span>
</pre></div>
</div>
<p>其等价于:</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nf">subsystem</span><span class="o">:</span>
<span class="k">$(</span>MAKE<span class="k">)</span> -C subdir
</pre></div>
</div>
<p>定义$(MAKE)宏变量的意思是也许我们的make需要一些参数所以定义成一个变量比较利于维护。这两个例子的意思都是先进入“subdir”目录然后执行make命令。</p>
<p>我们把这个Makefile叫做“总控Makefile”总控Makefile的变量可以传递到下级的Makefile中如果你显示的声明但是不会覆盖下层的Makefile中所定义的变量除非指定了 <code class="docutils literal notranslate"><span class="pre">-e</span></code> 参数。</p>
<p>如果你要传递变量到下级Makefile中那么你可以使用这样的声明:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">export</span> <span class="o">&lt;</span><span class="n">variable</span> <span class="o">...&gt;</span><span class="p">;</span>
</pre></div>
</div>
<p>如果你不想让某些变量传递到下级Makefile中那么你可以这样声明:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">unexport</span> <span class="o">&lt;</span><span class="n">variable</span> <span class="o">...&gt;</span><span class="p">;</span>
</pre></div>
</div>
<p>如:</p>
<p>示例一:</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="k">export </span><span class="nv">variable</span> <span class="o">=</span> value
</pre></div>
</div>
<p>其等价于:</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nv">variable</span> <span class="o">=</span> value
<span class="k">export</span> <span class="nv">variable</span>
</pre></div>
</div>
<p>其等价于:</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="k">export </span><span class="nv">variable</span> <span class="o">:=</span> value
</pre></div>
</div>
<p>其等价于:</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nv">variable</span> <span class="o">:=</span> value
<span class="k">export</span> <span class="nv">variable</span>
</pre></div>
</div>
<p>示例二:</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="k">export </span><span class="nv">variable</span> <span class="o">+=</span> value
</pre></div>
</div>
<p>其等价于:</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nv">variable</span> <span class="o">+=</span> value
<span class="k">export</span> <span class="nv">variable</span>
</pre></div>
</div>
<p>如果你要传递所有的变量那么只要一个export就行了。后面什么也不用跟表示传递所有的变量。</p>
<p>需要注意的是,有两个变量,一个是 <code class="docutils literal notranslate"><span class="pre">SHELL</span></code> ,一个是 <code class="docutils literal notranslate"><span class="pre">MAKEFLAGS</span></code> 这两个变量不管你是否export其总是要传递到下层 Makefile中特别是 <code class="docutils literal notranslate"><span class="pre">MAKEFLAGS</span></code> 变量其中包含了make的参数信息如果我们执行“总控Makefile”时有make参数或是在上层 Makefile中定义了这个变量那么
<code class="docutils literal notranslate"><span class="pre">MAKEFLAGS</span></code> 变量将会是这些参数并会传递到下层Makefile中这是一个系统级的环境变量。</p>
<p>但是make命令中的有几个参数并不往下传递它们是 <code class="docutils literal notranslate"><span class="pre">-C</span></code> , <code class="docutils literal notranslate"><span class="pre">-f</span></code> , <code class="docutils literal notranslate"><span class="pre">-h</span></code>, <code class="docutils literal notranslate"><span class="pre">-o</span></code>
<code class="docutils literal notranslate"><span class="pre">-W</span></code> 有关Makefile参数的细节将在后面说明如果你不想往下层传递参数那么你可以这样来</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nf">subsystem</span><span class="o">:</span>
<span class="nb">cd</span> subdir <span class="o">&amp;&amp;</span> <span class="k">$(</span>MAKE<span class="k">)</span> <span class="nv">MAKEFLAGS</span><span class="o">=</span>
</pre></div>
</div>
<p>如果你定义了环境变量 <code class="docutils literal notranslate"><span class="pre">MAKEFLAGS</span></code> ,那么你得确信其中的选项是大家都会用到的,如果其中有
<code class="docutils literal notranslate"><span class="pre">-t</span></code> , <code class="docutils literal notranslate"><span class="pre">-n</span></code><code class="docutils literal notranslate"><span class="pre">-q</span></code> 参数,那么将会有让你意想不到的结果,或许会让你异常地恐慌。</p>
<p>还有一个在“嵌套执行”中比较有用的参数, <code class="docutils literal notranslate"><span class="pre">-w</span></code> 或是 <code class="docutils literal notranslate"><span class="pre">--print-directory</span></code> 会在make的过程中输出一些信息让你看到目前的工作目录。比如如果我们的下级make目录是“/home/hchen/gnu/make”如果我们使用 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">-w</span></code> 来执行,那么当进入该目录时,我们会看到:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>make: Entering directory `/home/hchen/gnu/make&#39;.
</pre></div>
</div>
<p>而在完成下层make后离开目录时我们会看到:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>make: Leaving directory `/home/hchen/gnu/make&#39;
</pre></div>
</div>
<p>当你使用 <code class="docutils literal notranslate"><span class="pre">-C</span></code> 参数来指定make下层Makefile时 <code class="docutils literal notranslate"><span class="pre">-w</span></code> 会被自动打开的。如果参数中有
<code class="docutils literal notranslate"><span class="pre">-s</span></code> <code class="docutils literal notranslate"><span class="pre">--slient</span></code> )或是 <code class="docutils literal notranslate"><span class="pre">--no-print-directory</span></code> ,那么, <code class="docutils literal notranslate"><span class="pre">-w</span></code> 总是失效的。</p>
</section>
<section id="id5">
<h2>定义命令包<a class="headerlink" href="#id5" title="永久链接至标题"></a></h2>
<p>如果Makefile中出现一些相同命令序列那么我们可以为这些相同的命令序列定义一个变量。定义这种命令序列的语法以 <code class="docutils literal notranslate"><span class="pre">define</span></code> 开始,以 <code class="docutils literal notranslate"><span class="pre">endef</span></code> 结束,如:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>define run-yacc
yacc $(firstword $^)
mv y.tab.c $@
endef
</pre></div>
</div>
<p>这里“run-yacc”是这个命令包的名字其不要和Makefile中的变量重名。在 <code class="docutils literal notranslate"><span class="pre">define</span></code>
<code class="docutils literal notranslate"><span class="pre">endef</span></code> 中的两行就是命令序列。这个命令包中的第一个命令是运行Yacc程序因为Yacc程序总是生成“y.tab.c”的文件所以第二行的命令就是把这个文件改改名字。还是把这个命令包放到一个示例中来看看吧。</p>
<div class="highlight-makefile notranslate"><div class="highlight"><pre><span></span><span class="nf">foo.c </span><span class="o">:</span> <span class="n">foo</span>.<span class="n">y</span>
<span class="k">$(</span>run-yacc<span class="k">)</span>
</pre></div>
</div>
<p>我们可以看见要使用这个命令包我们就好像使用变量一样。在这个命令包的使用中命令包“run-yacc”中的 <code class="docutils literal notranslate"><span class="pre">$^</span></code> 就是 <code class="docutils literal notranslate"><span class="pre">foo.y</span></code> <code class="docutils literal notranslate"><span class="pre">$&#64;</span></code> 就是 <code class="docutils literal notranslate"><span class="pre">foo.c</span></code> (有关这种以 <code class="docutils literal notranslate"><span class="pre">$</span></code>
开头的特殊变量我们会在后面介绍make在执行命令包时命令包中的每个命令会被依次独立执行。</p>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="rules.html" class="btn btn-neutral float-left" title="书写规则" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="variables.html" class="btn btn-neutral float-right" title="使用变量" 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>