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

📄 7.htm

📁 学习MAkefile相关的好资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; y := $(x) bar<BR>&nbsp;&nbsp;&nbsp; x := foo</FONT></P>
<P><FONT face="Courier New">那么,y的值是“bar”,而不是“foo bar”。</FONT></P>
<P><FONT face="Courier New">上面都是一些比较简单的变量使用了,让我们来看一个复杂的例子,其中包括了make的函数、条件表达式和一个系统变量“MAKELEVEL”的使用:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; ifeq (0,${MAKELEVEL})<BR>&nbsp;&nbsp;&nbsp; cur-dir&nbsp;&nbsp; := $(shell pwd)<BR>&nbsp;&nbsp;&nbsp; whoami&nbsp;&nbsp;&nbsp; := $(shell whoami)<BR>&nbsp;&nbsp;&nbsp; host-type := $(shell arch)<BR>&nbsp;&nbsp;&nbsp; MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}<BR>&nbsp;&nbsp;&nbsp; endif</FONT></P>
<P><FONT face="Courier New">关于条件表达式和函数,我们在后面再说,对于系统变量“MAKELEVEL”,其意思是,如果我们的make有一个嵌套执行的动作(参见前面的“嵌套使用make”),那么,这个变量会记录了我们的当前Makefile的调用层数。</FONT></P>
<P><FONT face="Courier New">下面再介绍两个定义变量时我们需要知道的,请先看一个例子,如果我们要定义一个变量,其值是一个空格,那么我们可以这样来:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; nullstring :=<BR>&nbsp;&nbsp;&nbsp; space := $(nullstring) # end of the line</FONT></P>
<P><FONT face="Courier New">nullstring是一个Empty变量,其中什么也没有,而我们的space的值是一个空格。因为在操作符的右边是很难描述一个空格的,这里采用的技术很管用,先用一个Empty变量来标明变量的值开始了,而后面采用“#”注释符来表示变量定义的终止,这样,我们可以定义出其值是一个空格的变量。请注意这里关于“#”的使用,注释符“#”的这种特性值得我们注意,如果我们这样定义一个变量:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; dir := /foo/bar&nbsp;&nbsp;&nbsp; # directory to put the frobs in</FONT></P>
<P><FONT face="Courier New">dir这个变量的值是“/foo/bar”,后面还跟了4个空格,如果我们这样使用这样变量来指定别的目录——“$(dir)/file”那么就完蛋了。</FONT></P>
<P><FONT face="Courier New">还有一个比较有用的操作符是“?=”,先看示例:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; FOO ?= bar</FONT></P>
<P><FONT face="Courier New">其含义是,如果FOO没有被定义过,那么变量FOO的值就是“bar”,如果FOO先前被定义过,那么这条语将什么也不做,其等价于:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; ifeq ($(origin FOO), undefined)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FOO = bar<BR>&nbsp;&nbsp;&nbsp; endif</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>三、变量高级用法</STRONG></FONT></P>
<P><FONT face="Courier New">这里介绍两种变量的高级使用方法,第一种是变量值的替换。</FONT></P>
<P><FONT face="Courier New">我们可以替换变量中的共有的部分,其格式是“$(var:a=b)”或是“${var:a=b}”,其意思是,把变量“var”中所有以“a”字串“结尾”的“a”替换成“b”字串。这里的“结尾”意思是“空格”或是“结束符”。</FONT></P>
<P><FONT face="Courier New">还是看一个示例吧:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; foo := a.o b.o c.o<BR>&nbsp;&nbsp;&nbsp; bar := $(foo:.o=.c)</FONT></P>
<P><FONT face="Courier New">这个示例中,我们先定义了一个“$(foo)”变量,而第二行的意思是把“$(foo)”中所有以“.o”字串“结尾”全部替换成“.c”,所以我们的“$(bar)”的值就是“a.c b.c c.c”。</FONT></P>
<P><FONT face="Courier New">另外一种变量替换的技术是以“静态模式”(参见前面章节)定义的,如:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; foo := a.o b.o c.o<BR>&nbsp;&nbsp;&nbsp; bar := $(foo:%.o=%.c)</FONT></P>
<P><FONT face="Courier New">这依赖于被替换字串中的有相同的模式,模式中必须包含一个“%”字符,这个例子同样让$(bar)变量的值为“a.c b.c c.c”。 </FONT></P>
<P><FONT face="Courier New">第二种高级用法是——“把变量的值再当成变量”。先看一个例子:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; x = y<BR>&nbsp;&nbsp;&nbsp; y = z<BR>&nbsp;&nbsp;&nbsp; a := $($(x))</FONT></P>
<P><FONT face="Courier New">在这个例子中,$(x)的值是“y”,所以$($(x))就是$(y),于是$(a)的值就是“z”。(注意,是“x=y”,而不是“x=$(y)”)</FONT></P>
<P><FONT face="Courier New">我们还可以使用更多的层次:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; x = y<BR>&nbsp;&nbsp;&nbsp; y = z<BR>&nbsp;&nbsp;&nbsp; z = u<BR>&nbsp;&nbsp;&nbsp; a := $($($(x)))</FONT></P>
<P><FONT face="Courier New">这里的$(a)的值是“u”,相关的推导留给读者自己去做吧。</FONT></P>
<P><FONT face="Courier New">让我们再复杂一点,使用上“在变量定义中使用变量”的第一个方式,来看一个例子:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; x = $(y)<BR>&nbsp;&nbsp;&nbsp; y = z<BR>&nbsp;&nbsp;&nbsp; z = Hello<BR>&nbsp;&nbsp;&nbsp; a := $($(x))</FONT></P>
<P><FONT face="Courier New">这里的$($(x))被替换成了$($(y)),因为$(y)值是“z”,所以,最终结果是:a:=$(z),也就是“Hello”。</FONT></P>
<P><FONT face="Courier New">再复杂一点,我们再加上函数:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; x = variable1<BR>&nbsp;&nbsp;&nbsp; variable2 := Hello<BR>&nbsp;&nbsp;&nbsp; y = $(subst 1,2,$(x))<BR>&nbsp;&nbsp;&nbsp; z = y<BR>&nbsp;&nbsp;&nbsp; a := $($($(z)))</FONT></P>
<P><FONT face="Courier New">这个例子中,“$($($(z)))”扩展为“$($(y))”,而其再次被扩展为“$($(subst 1,2,$(x)))”。$(x)的值是“variable1”,subst函数把“variable1”中的所有“1”字串替换成“2”字串,于是,“variable1”变成“variable2”,再取其值,所以,最终,$(a)的值就是$(variable2)的值——“Hello”。(喔,好不容易)</FONT></P>
<P><FONT face="Courier New">在这种方式中,或要可以使用多个变量来组成一个变量的名字,然后再取其值:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; first_second = Hello<BR>&nbsp;&nbsp;&nbsp; a = first<BR>&nbsp;&nbsp;&nbsp; b = second<BR>&nbsp;&nbsp;&nbsp; all = $($a_$b)</FONT></P>
<P><FONT face="Courier New">这里的“$a_$b”组成了“first_second”,于是,$(all)的值就是“Hello”。</FONT></P>
<P><FONT face="Courier New">再来看看结合第一种技术的例子:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; a_objects := a.o b.o c.o<BR>&nbsp;&nbsp;&nbsp; 1_objects := 1.o 2.o 3.o</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; sources := $($(a1)_objects:.o=.c)</FONT></P>
<P><FONT face="Courier New">这个例子中,如果$(a1)的值是“a”的话,那么,$(sources)的值就是“a.c b.c c.c”;如果$(a1)的值是“1”,那么$(sources)的值是“1.c 2.c 3.c”。</FONT></P>
<P><FONT face="Courier New">再来看一个这种技术和“函数”与“条件语句”一同使用的例子:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; ifdef do_sort<BR>&nbsp;&nbsp;&nbsp; func := sort<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp; func := strip<BR>&nbsp;&nbsp;&nbsp; endif</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; bar := a d b g q c</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; foo := $($(func) $(bar))</FONT></P>
<P><FONT face="Courier New">这个示例中,如果定义了“do_sort”,那么:foo := $(sort a d b g q c),于是$(foo)的值就是“a b c d g q”,而如果没有定义“do_sort”,那么:foo := $(sort a d b g q c),调用的就是strip函数。</FONT></P>
<P><FONT face="Courier New">当然,“把变量的值再当成变量”这种技术,同样可以用在操作符的左边:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; dir = foo<BR>&nbsp;&nbsp;&nbsp; $(dir)_sources := $(wildcard $(dir)/*.c)<BR>&nbsp;&nbsp;&nbsp; define $(dir)_print<BR>&nbsp;&nbsp;&nbsp; lpr $($(dir)_sources)<BR>&nbsp;&nbsp;&nbsp; endef</FONT></P>
<P><FONT face="Courier New">这个例子中定义了三个变量:“dir”,“foo_sources”和“foo_print”。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>四、追加变量值</STRONG></FONT></P>
<P><FONT face="Courier New">我们可以使用“+=”操作符给变量追加值,如:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; objects = main.o foo.o bar.o utils.o<BR>&nbsp;&nbsp;&nbsp; objects += another.o</FONT></P>
<P><FONT face="Courier New">于是,我们的$(objects)值变成:“main.o foo.o bar.o utils.o another.o”(another.o被追加进去了)</FONT></P>
<P><FONT face="Courier New">使用“+=”操作符,可以模拟为下面的这种例子:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; objects = main.o foo.o bar.o utils.o<BR>&nbsp;&nbsp;&nbsp; objects := $(objects) another.o</FONT></P>
<P><FONT face="Courier New">所不同的是,用“+=”更为简洁。</FONT></P>
<P><FONT face="Courier New">如果变量之前没有定义过,那么,“+=”会自动变成“=”,如果前面有变量定义,那么“+=”会继承于前次操作的赋值符。如果前一次的是“:=”,那么“+=”会以“:=”作为其赋值符,如:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; variable := value<BR>&nbsp;&nbsp;&nbsp; variable += more</FONT></P>
<P><FONT face="Courier New">等价于:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; variable := value<BR>&nbsp;&nbsp;&nbsp; variable := $(variable) more</FONT></P>
<P><FONT face="Courier New">但如果是这种情况: </FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; variable = value<BR>&nbsp;&nbsp;&nbsp; variable += more</FONT></P>
<P><FONT face="Courier New">由于前次的赋值符是“=”,所以“+=”也会以“=”来做为赋值,那么岂不会发生变量的递补归定义,这是很不好的,所以make会自动为我们解决这个问题,我们不必担心这个问题。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>五、override 指示符</STRONG></FONT></P>
<P><FONT face="Courier New">如果有变量是通常make的命令行参数设置的,那么Makefile中对这个变量的赋值会被忽略。如果你想在Makefile中设置这类参数的值,那么,你可以使用“override”指示符。其语法是:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; override &lt;variable&gt; = &lt;value&gt;</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; override &lt;variable&gt; := &lt;value&gt;</FONT></P>
<P><FONT face="Courier New">当然,你还可以追加:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; override &lt;variable&gt; += &lt;more text&gt;</FONT></P>
<P><FONT face="Courier New">对于多行的变量定义,我们用define指示符,在define指示符前,也同样可以使用ovveride指示符,如:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; override define foo<BR>&nbsp;&nbsp;&nbsp; bar<BR>&nbsp;&nbsp;&nbsp; endef</FONT></P><FONT face="Courier New">
<P align=right><STRONG><FONT face="Courier New">&lt;-</FONT></STRONG><A href="http://www.csdn.net/develop/read_article.asp?id=20396"><FONT face="Courier New" color=#0000ff><STRONG>上一页</STRONG></FONT></A><STRONG><FONT face="Courier New">&nbsp; <A href="http://www.csdn.net/Develop/Read_Article.asp?Id=20464"><FONT color=#0000ff>下一页</FONT></A>-&gt;</FONT></STRONG></P>
<P><STRONG><FONT face="Courier New">(版权所有,转载时请注明作者和出处)</FONT></STRONG></P>
<P><BR></FONT>&nbsp;</P></span>
<br />
<div style="font-size: 14px; line-height: 25px;"><strong>作者Blog:</strong><a id="ArticleContent1_ArticleContent1_AuthorBlogLink" href="http://blog.csdn.net/haoel/" target="_blank">http://blog.csdn.net/haoel/</a></div>
<div style="font-size: 14px; line-height: 25px; color:#900;"><strong>相关文章</strong></div>
<table id="ArticleContent1_ArticleContent1_RelatedArticles" cellspacing="0" border="0">
	<tr>
		<td>
    <a href="/article/29/29472.shtm">标准C++类string的Copy-On-Write技术(三)</a>
  </td>
	</tr><tr>
		<td>
    <a href="/article/29/29470.shtm">标准C++类string的Copy-On-Write技术(二)</a>
  </td>
	</tr><tr>
		<td>
    <a href="/article/29/29469.shtm">标准C++类string的Copy-On-Write技术(一)</a>
  </td>
	</tr><tr>
		<td>
    <a href="/article/23/23559.shtm">以程序的方式操纵NTFS的文件权限(下)</a>
  </td>
	</tr><tr>
		<td>
    <a href="/article/23/23558.shtm">以程序的方式操纵NTFS的文件权限(中)</a>
  </td>
	</tr>
</table>
</td>
              </tr>
            </table>
            <a name="#Comment"></a>
            <table width="100%" border="0" cellpadding="0">
              <tr>
                <td>
                  



  <table cellSpacing=0 cellPadding=0 width="100%" align=center bgColor=#006699 border=0>
    <tr bgColor=#006699>
      <td id=white align=center width=556 bgColor=#006699>
      <font color=#ffffff >对该文的评论</font> </td>
    </tr>
  </table>
  
<div align=right>
<a id="CommnetList1_CommnetList1_Morelink" href="http://comment.csdn.net/Comment.aspx?c=2&amp;s=20438">【评论】</a>
<a id="CommnetList1_CommnetList1_Hyperlink1" href="javascript:window.close();">【关闭】</a>
<a href="mailto:webmaster@csdn.net">【报告bug】</a>
</div>
<br>
                </td>
              </tr>
            </table>
          </TD>
        </TR>
      </TABLE>
    </form>
    
<!-- 版权 -->
<hr width="770" noShade size="1" align="center">
<table cellspacing="0" cellpadding="0" width="500" border="0" align="center">
	<tr>
		<td valign="bottom" height="10" align="center">
			<a href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</a>
			-
			<a href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</a>
			-
			<a href="http://www.csdn.net/map/map.shtm">网站地图</a>
			-
			<a href="http://www.csdn.net/help/help.asp">帮助信息</a>
			-
			<a href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</a>
			-
			<a href="http://www.csdn.net/english">English</a>
		</td>
		<td align="center" rowspan="3"><a href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><img height="48" src="/images/biaoshi.gif" width="40" border="0"></a></td>
	</tr>
	<tr>
		<td valign="top" align="center">北京百联美达美数码科技有限公司 版权所有 京ICP证020026号</td>
	</tr>
	<tr align="center">
		<td valign="top"><font face="Verdana">Copyright &copy; CSDN.NET, Inc. All Rights Reserved</font></td>
	</tr>
	<tr><td height="15"></td></tr>
</table>
<!-- /版权 -->

    <script>
      document.write("<img src=http://count.csdn.net/count/pageview1.asp?columnid=4&itemid=11 border=0 width=0 height=0>");
    </script>
  </body>
</HTML>

⌨️ 快捷键说明

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