📄 7.htm
字号:
<tr>
<td align="right"><a href="/articlelist.aspx?c=6" >Java</a> (<a href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=6">rss</a>) </td>
</tr>
<tr>
<td align="right"><a href="/articlelist.aspx?c=5" >Perl</a> (<a href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=5">rss</a>) </td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" width="95%" border="0" style="margin-bottom: 4px;" align="center">
<tr>
<td align="right" style="border-bottom: 1px dashed #B0C4DE; color: #0033ff;"><font style="font-size: 8pt;">综合</font></td>
</tr>
<tr>
<td align="right"><a href="/articlelist.aspx?c=9" >其他开发语言</a> (<a href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=9">rss</a>) </td>
</tr>
<tr>
<td align="right"><a href="/articlelist.aspx?c=11" >文件格式</a> (<a href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=11">rss</a>) </td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" width="95%" border="0" style="margin-bottom: 4px;" align="center">
<tr>
<td align="right" style="border-bottom: 1px dashed #B0C4DE; color: #0033ff;"><font style="font-size: 8pt;">企业开发</font></td>
</tr>
<tr>
<td align="right"><a href="/articlelist.aspx?c=10" >游戏开发</a> (<a href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=10">rss</a>) </td>
</tr>
<tr>
<td align="right"><a href="/articlelist.aspx?c=12" >网站制作技术</a> (<a href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=12">rss</a>) </td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" width="95%" border="0" style="margin-bottom: 4px;" align="center">
<tr>
<td align="right" style="border-bottom: 1px dashed #B0C4DE; color: #0033ff;"><font style="font-size: 8pt;">数据库</font></td>
</tr>
<tr>
<td align="right"><a href="/articlelist.aspx?c=8" >数据库开发</a> (<a href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=8">rss</a>) </td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" width="95%" border="0" align="center">
<tr>
<td align="right" style="border-bottom: 1px dashed #B0C4DE; color: #0033ff;"><font style="font-size: 8pt;">软件工程</font></td>
</tr>
<tr>
<td align="right"><a href="/articlelist.aspx?c=13" >其他</a> (<a href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=13">rss</a>) </td>
</tr>
</table>
</td>
</tr>
</table>
<br>
<table width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#000000">
<tr>
<td valign="top" bgcolor="#EDEDED">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td height="27" align="right" background="/images/top.gif"><font class="fonttitle">积极原创作者 </font></td>
</tr>
</table>
<table width="100%" cellpadding="2" cellspacing="1" border="0">
<tr><td align="right"><a href='/user/qiaoshaojie'>qiaoshaojie</a> <font color="#FF0000">(1)</font></td></tr>
<tr><td align="right"><a href='/user/yoqi'>yoqi</a> <font color="#FF0000">(1)</font></td></tr>
<tr><td align="right"><a href='/user/eliry'>eliry</a> <font color="#FF0000">(2)</font></td></tr>
<tr><td align="right"><a href='/user/fasttalk'>fasttalk</a> <font color="#FF0000">(23)</font></td></tr>
<tr><td align="right"><a href='/user/BennyMavis'>BennyMavis</a> <font color="#FF0000">(4)</font></td></tr>
<tr><td align="right"><a href='/user/james999'>james999</a> <font color="#FF0000">(31)</font></td></tr>
<tr><td align="right"><a href='/user/MasterLRC'>MasterLRC</a> <font color="#FF0000">(12)</font></td></tr>
<tr><td align="right"><a href='/user/fatalerror99'>fatalerror99</a> <font color="#FF0000">(1)</font></td></tr>
<tr><td align="right"><a href='/user/ihavenoidea'>ihavenoidea</a> <font color="#FF0000">(1)</font></td></tr>
<tr><td align="right"><a href='/user/ire_wang'>ire_wang</a> <font color="#FF0000">(2)</font></td></tr>
</table>
</td>
</tr>
</table>
</TD>
<TD vAlign="top" width="786">
<table width="100%" border="0" cellpadding="4">
<tr>
<td width="10" height="19"></td>
<td height="19">
<table width="100%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td>
<a href="http://www.csdn.net/">CSDN</a> - <a href="http://dev.csdn.net/">文档中心</a>
- <font color="#003399">
<a id="ArticleTitle1_ArticleTitle1_hlClass" href="/articlelist.aspx?c=13">其他</a></font>
</td>
<TD align="right">
阅读:<span id="ArticleTitle1_ArticleTitle1_lblReadCount">19825</span>
评论:
<span id="ArticleTitle1_ArticleTitle1_lblCommentCount">0</span>
<a href="#Comment">参与评论</a>
</TD>
</tr>
</table>
<table width="100%" border="0" cellspacing="3" cellpadding="3" bgcolor="#eeeeee">
<tr>
<td width="60" height="0" nowrap></td>
<td></td>
</tr>
<tr>
<td height="16" align="center" nowrap bgcolor="#003399"><font color="#ffffff">标题</font></td>
<td><b>
<span id="ArticleTitle1_ArticleTitle1_lblTitle">跟我一起写 Makefile(七)</span></b>
选择自 <a id="ArticleTitle1_ArticleTitle1_AuthorLink" href="/user/haoel">haoel</a> 的 Blog
</td>
</tr>
<tr>
<td height="16" align="center" bgcolor="#003399"><font color="#ffffff">关键字</font></td>
<td width="500">
<span id="ArticleTitle1_ArticleTitle1_lblKeywords">C C++ make makefile</span></td>
</tr>
<tr>
<td height="16" align="center" bgcolor="#003399"><font color="#ffffff">出处</font></td>
<td>
<span id="ArticleTitle1_ArticleTitle1_lblSource"></span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="10"></td>
<td><span id="ArticleContent1_ArticleContent1_lblContent"><P><FONT face="Courier New" size=4><STRONG>使用变量<BR>————</STRONG></FONT></P>
<P><FONT face="Courier New">在Makefile中的定义的变量,就像是C/C++语言中的宏一样,他代表了一个文本字串,在Makefile中执行的时候其会自动原模原样地展开在所使用的地方。其与C/C++所不同的是,你可以在Makefile中改变其值。在Makefile中,变量可以使用在“目标”,“依赖目标”,“命令”或是Makefile的其它部分中。</FONT></P>
<P><FONT face="Courier New">变量的命名字可以包含字符、数字,下划线(可以是数字开头),但不应该含有“:”、“#”、“=”或是空字符(空格、回车等)。变量是大小写敏感的,“foo”、“Foo”和“FOO”是三个不同的变量名。传统的Makefile的变量名是全大写的命名方式,但我推荐使用大小写搭配的变量名,如:MakeFlags。这样可以避免和系统的变量冲突,而发生意外的事情。</FONT></P>
<P><FONT face="Courier New">有一些变量是很奇怪字串,如“$<”、“$@”等,这些是自动化变量,我会在后面介绍。</FONT></P>
<P><FONT face="Courier New"><STRONG>一、变量的基础</STRONG></FONT></P>
<P><FONT face="Courier New">变量在声明时需要给予初值,而在使用时,需要给在变量名前加上“$”符号,但最好用小括号“()”或是大括号“{}”把变量给包括起来。如果你要使用真实的“$”字符,那么你需要用“$$”来表示。</FONT></P>
<P><FONT face="Courier New">变量可以使用在许多地方,如规则中的“目标”、“依赖”、“命令”以及新的变量中。先看一个例子:</FONT></P>
<P><FONT face="Courier New"> objects = program.o foo.o utils.o<BR> program : $(objects)<BR> cc -o program $(objects)</FONT></P>
<P><FONT face="Courier New"> $(objects) : defs.h</FONT></P>
<P><FONT face="Courier New">变量会在使用它的地方精确地展开,就像C/C++中的宏一样,例如:</FONT></P>
<P><FONT face="Courier New"> foo = c<BR> prog.o : prog.$(foo)<BR> $(foo)$(foo) -$(foo) prog.$(foo)</FONT></P>
<P><FONT face="Courier New">展开后得到:</FONT></P>
<P><FONT face="Courier New"> prog.o : prog.c<BR> cc -c prog.c</FONT></P>
<P><FONT face="Courier New">当然,千万不要在你的Makefile中这样干,这里只是举个例子来表明Makefile中的变量在使用处展开的真实样子。可见其就是一个“替代”的原理。</FONT></P>
<P><FONT face="Courier New">另外,给变量加上括号完全是为了更加安全地使用这个变量,在上面的例子中,如果你不想给变量加上括号,那也可以,但我还是强烈建议你给变量加上括号。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>二、变量中的变量</STRONG></FONT></P>
<P><FONT face="Courier New">在定义变量的值时,我们可以使用其它变量来构造变量的值,在Makefile中有两种方式来在用变量定义变量的值。</FONT></P>
<P><FONT face="Courier New">先看第一种方式,也就是简单的使用“=”号,在“=”左侧是变量,右侧是变量的值,右侧变量的值可以定义在文件的任何一处,也就是说,右侧中的变量不一定非要是已定义好的值,其也可以使用后面定义的值。如:</FONT></P>
<P><FONT face="Courier New"> foo = $(bar)<BR> bar = $(ugh)<BR> ugh = Huh?</FONT></P>
<P><FONT face="Courier New"> all:<BR> echo $(foo)</FONT></P>
<P><FONT face="Courier New">我们执行“make all”将会打出变量$(foo)的值是“Huh?”( $(foo)的值是$(bar),$(bar)的值是$(ugh),$(ugh)的值是“Huh?”)可见,变量是可以使用后面的变量来定义的。</FONT></P>
<P><FONT face="Courier New">这个功能有好的地方,也有不好的地方,好的地方是,我们可以把变量的真实值推到后面来定义,如:</FONT></P>
<P><FONT face="Courier New"> CFLAGS = $(include_dirs) -O<BR> include_dirs = -Ifoo -Ibar</FONT></P>
<P><FONT face="Courier New">当“CFLAGS”在命令中被展开时,会是“-Ifoo -Ibar -O”。但这种形式也有不好的地方,那就是递归定义,如:</FONT></P>
<P><FONT face="Courier New"> CFLAGS = $(CFLAGS) -O</FONT></P>
<P><FONT face="Courier New"> 或:</FONT></P>
<P><FONT face="Courier New"> A = $(B)<BR> B = $(A)</FONT></P>
<P><FONT face="Courier New">这会让make陷入无限的变量展开过程中去,当然,我们的make是有能力检测这样的定义,并会报错。还有就是如果在变量中使用函数,那么,这种方式会让我们的make运行时非常慢,更糟糕的是,他会使用得两个make的函数“wildcard”和“shell”发生不可预知的错误。因为你不会知道这两个函数会被调用多少次。</FONT></P>
<P><FONT face="Courier New">为了避免上面的这种方法,我们可以使用make中的另一种用变量来定义变量的方法。这种方法使用的是“:=”操作符,如:</FONT></P>
<P><FONT face="Courier New"> x := foo<BR> y := $(x) bar<BR> x := later</FONT></P>
<P><FONT face="Courier New">其等价于:</FONT></P>
<P><FONT face="Courier New"> y := foo bar<BR> x := later</FONT></P>
<P><FONT face="Courier New">值得一提的是,这种方法,前面的变量不能使用后面的变量,只能使用前面已定义好了的变量。如果是这样:</FONT></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -