📄 make-04.html
字号:
margin-right:0pt;
margin-bottom:12.0pt;
margin-left:36.0pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:-36.0pt;
page-break-after:avoid;
tab-stops:list 36.0pt;
text-autospace:none;
font-size:12.0pt;
font-family:Arial;
font-weight:normal}
-->
</style>
</head>
<body>
<h1 align="center" style="text-align: center; text-indent: 0pt; margin-left: 9.9pt">
<a name="chapter_04"><span style="font-size: 18.0pt; font-family: 楷体_GB2312">
第四章:</span><span lang="EN-US" style="font-size: 18.0pt">Makefile</span><span style="font-size: 18.0pt; font-family: 楷体_GB2312">的规则</span></a></h1>
<span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; color: #222222">
<hr size="1" width="100%" align="center">
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">
本章我们将讨论</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">Makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">的一个重要内容,规则。熟悉规则对于书写</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">Makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">至关重要。</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">Makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">中,规则描述了在何种情况下使用什么命令来重建一个特定的文件,此文件被称为规则“目标”(通常规则中的目标只有一个)。规则中出目标之外的罗列的其它文件称为“目标”的依赖,而规则的命令是用来更新或者创建此规则的目标。</span></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">除了</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">的“终极目标”所在的规则以外,其它规则的顺序在</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">文件中没有意义。“终极目标”就是当没有使用</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">make
</span>
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">
命令行指定具体目标时,</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">make</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">默认的更新的哪一个目标。它是</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">文件中第一个规则的目标。如果在</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">中第一个规则有多个目标的话,那么多个目标中的第一个将会被作为</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">make</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">的“终极目标”。有两种情况的例外:</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">1.
</span>
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">
目标名以点号“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">.</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">”开始的并且其后不存在斜线“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">/</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">”(“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">./</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">”被认为是当前目录;“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">../</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">”被认为是上一级目录);</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">2.
</span>
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">
模式规则的目标。当这两种目标所在的规则是</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">Makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">的第一个规则时,它们并不会被作为“终极目标”。</span></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">
“终极目标”是执行</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">make</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">的唯一目的,其所在的规则作为第一个被执行的规则。而其它的规则是在完成重建“终极目标”的过程中被连带出来的。所以这些目标所在规则在</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">Makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312">中的顺序无关紧要。</span></p>
<span style="font-size: 12.0pt; font-family: 楷体_GB2312">因此,我们书写的</span><span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">makefile</span><span style="font-size: 12.0pt; font-family: 楷体_GB2312">的第一个规则应该就是重建整个程序或者多个程序的依赖关系和执行命令的描述。</span></span><h2><a name="_gnu_make_4.1"><span style="font-size: 14pt" lang="en-us"><b>4</b></span><b><span lang="EN-US" style="font-size: 14.0pt">.1 </span></b><span style="font-size: 14.0pt; font-family: 楷体_GB2312">一个例子</span></a></h2>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">
我们来看一个规则的例子:</span></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
<span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">
</span></p>
<p class="MsoNormal" style="text-indent: 41.2pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">foo.o : foo.c
defs.h # module for twiddling the frobs</span></i></b></p>
<p class="MsoNormal" style="text-indent: 41.2pt; margin-left: 30.9pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">cc -c -g foo.c</span></i></b></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
<span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">
</span></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">
这是一个典型的规则。看到这个例子,大家应该能够说出这个规则的各个部分之间的关系。不过我们还是要把这个例子拿出来讨论。目的是让我们更加明确地理解</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">Makefile</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">的规则。本例第一行中,文件“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">foo.o</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”是规则需要重建的文件,而“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">foo.c</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”和“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">defs.h</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”是重建“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">foo.o</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”所要使用的文件。我们把规则所需要重建的文件称为规则的“目标”(</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">foo.o</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">),而把重新目标所需要的文件称为规则的“依赖”(或者目标的依赖)。规则中的第二行“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">cc
-c -g foo.c</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”是规则的“命令”。它描述了如何使用规则中的依赖文件重建目标。</span></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">
而且,上面的规则告诉我们了两件事:</span></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: -21.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other; margin-left: 39.0pt">
<span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">
1.<span style="font:7.0pt "Times New Roman""> </span></span>
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">
如何确定目标文件是否过期(需要重建目标),过期是指目标文件不存在或者目标文件“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">foo.o</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”在时间戳上比依赖文件中的任何一个(“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">foo.c</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”或者“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">defs.h</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”)“老”。</span></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: -21.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other; margin-left: 39.0pt">
<span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">
2.<span style="font:7.0pt "Times New Roman""> </span></span>
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">
如何重建目标文件“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">foo.o</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”。这个规则中使用</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">cc</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">编译器。规则的命令中没有明确的使用到依赖文件“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">defs.h</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”。我们假设在源文件“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">foo.c</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”中已经包含了此头文件。这也是为什么它作为目标依赖出现的原因。</span></p>
<h2><a name="_gnu_make_4.2"><span style="font-size: 14pt" lang="en-us"><b>4</b></span><b><span lang="EN-US" style="font-size: 14.0pt">.2 </span></b><span style="font-size: 14.0pt; font-family: 楷体_GB2312">规则语法</span></a></h2>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; text-indent: 24.0pt; line-height: 150%; text-autospace: ideograph-numeric ideograph-other">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -