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

📄 make-04.html

📁 unix make 文档
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">
规则的中心思想是:<b>目标文件的内容是由依赖文件文件决定,依赖文件的任何一处改动,将导致目前已经存在的目标文件的内容过期。</b>规则的命令为重建目标提供了方法。这些命令运行在系统</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">shell</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">之上。</span></p>
<h2><a name="_gnu_make_4.3"><span style="font-size: 14pt" lang="en-us"><b>4</b></span><b><span lang="EN-US" style="font-size: 14.0pt">.3 </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><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">GNU 
make</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">1.
</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">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">2.
</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">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">order-only</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">A:B 
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">A</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">B</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">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">B</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">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">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">B</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">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: 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">
有时,需要定义一个这样的规则,在更新目标(<b>目标文件已经存在</b>)时只需要根据依赖文件中的部分来决定目标是否需要被重建,而不是在依赖文件的任何一个被修改后都重建目标。为了实现这一目的,相应的就需要对规则的依赖进行分类,一类是在这些依赖文件被更新后,需要更新规则的目标;另一类是更新这些依赖的,可不需要更新规则的目标。我们把第二类称为:“</span><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial">order-only</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">order-only</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">|</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">|</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">order-only</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 lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">
&nbsp;</span></p>
<p class="MsoNormal" style="text-indent: 12.1pt; margin-left: 29.95pt"><b><i>
<span lang="EN-US" style="font-size: 10.5pt; font-family: Arial">TARGETS : 
NORMAL-PREREQUISITES | ORDER-ONLY-PREREQUISITES</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">
&nbsp;</span></p>
<p class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph; 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">order-only</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">order-only</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">”依赖所实现的动作的一个超集)。</span></p>
<div style="mso-element:para-border-div;border:none;border-top:solid black 1.0pt;
mso-border-top-alt:solid black .5pt;padding:1.0pt 0cm 0cm 0cm">
	<p class="a">
	<span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">“</span><span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">order-only</span><span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">”依赖的使用举例:</span></p>
	<p class="a">
	<span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">&nbsp;&nbsp;&nbsp; LIBS = 
	libtest.a</span></p>
	<p class="a" style="text-indent: 21.0pt">
	<span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">foo : foo.c 
	| $(LIBS)</span></div>
<div style="mso-element:para-border-div;border:none;border-bottom:solid black 1.0pt;
mso-border-bottom-alt:solid black .5pt;padding:0cm 0cm 1.0pt 0cm">
	<p class="a0" style="text-indent:21.0pt">
	<span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(CC) 
	$(CFLAGS) $&lt; -o $@ $(LIBS)</span></p>
	<p class="a0" style="text-indent:21.0pt">
	<span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">make</span><span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">在执行这个规则时,如果目标文件“</span><span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">foo</span><span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">”已经存在。当“</span><span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">foo.c</span><span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">”被修改以后,目标“</span><span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">foo</span><span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">”将会被重建,但是当“</span><span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">libtest.a</span><span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">”被修改以后。将不执行规则的命令来重建目标“</span><span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">foo</span><span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">”。</span></p>
	<p class="a0" style="text-indent:21.0pt">
	<span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">
	就是说,规则中依赖文件</span><span lang="EN-US" style="font-size: 10.5pt; line-height: 150%">$(LIBS)</span><span style="font-size: 10.5pt; line-height: 150%; font-family: 楷体_GB2312">只有在目标文件不存在的情况下,才会参与规则的执行。当目标文件存在时此依赖不会参与规则的执行过程。</span></div>
<h2><a name="_gnu_make_4.4"><span style="font-size: 14pt" lang="en-us"><b>4</b></span><b><span lang="EN-US" style="font-size: 14.0pt">.4 </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 lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">
Maekfile</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">*</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">?</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">[…]</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">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">Linux</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">unix</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">Bourne 
shell</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">*.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">.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">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">Makefile</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">
1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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">make</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">Makefile</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 &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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">shell</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><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">wildcard</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><span lang="EN-US" style="font-size: 12.0pt; line-height: 150%; font-family: Arial; color: black">*</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">.</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">\</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\*bar</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">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*bar</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">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">B-SHELL</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">C</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">语言中的基本上相同。</span></p>

⌨️ 快捷键说明

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