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

📄 make-06.html

📁 unix make 文档
💻 HTML
📖 第 1 页 / 共 5 页
字号:
第六章:</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; 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">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">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">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">
1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>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">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><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">define</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></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">
3.<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">:</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">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">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">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: -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">
4.<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">foo</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</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</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">objects</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">CFLAGS</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">
5.<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">$&lt;</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></p>
</span>
<h2><a name="_gnu_make_6.1"><span style="font-size: 14pt" lang="en-us"><b>6</b></span><b><span lang="EN-US" style="font-size: 14.0pt">.1<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7.0pt; font-family: Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></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">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">$</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">VARIABLE_NAME</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">${ 
VARIABLE_NAME }</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)
</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}</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</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">$</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">.o</span><span style="font-size: 12.0pt; line-height: 150%; font-family: 楷体_GB2312; color: black">文件的列表:</span></p>
<p class="MsoNormal" style="text-indent: 20.5pt">
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial; color: black">&nbsp;</span></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 29.95pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">objects = 
program.o foo.o utils.o</span></i></b></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 29.95pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">program : 
$(objects)</span></i></b></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 58.2pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">cc -o program 
$(objects)</span></i></b></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 29.95pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">&nbsp;</span></i></b></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 29.95pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">$(objects) : 
defs.h</span></i></b></p>
<p class="MsoNormal" style="text-indent: 20.6pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">&nbsp;</span></i></b></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></p>
<p class="MsoNormal" style="text-indent: 20.5pt">
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial; color: black">&nbsp;</span></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 29.95pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">foo = c</span></i></b></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 29.95pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">prog.o : 
prog.$(foo)</span></i></b></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 58.2pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">$(foo) $(foo) 
-$(foo) prog.$(foo)</span></i></b></p>
<p class="MsoNormal" style="text-indent: 20.5pt">
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">&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></p>
<p class="MsoNormal" style="text-indent: 20.5pt">
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial; color: black">&nbsp;</span></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 29.95pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">prog.c : prog.c</span></i></b></p>
<p class="MsoNormal" style="text-indent: 13.85pt; margin-left: 58.2pt"><b><i>
<span lang="EN-US" style="font-size: 12.0pt; font-family: Arial">cc -c prog.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">
&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">

⌨️ 快捷键说明

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