📄 6.htm
字号:
<P><FONT face="Courier New"> echo 正在编译XXX模块......<BR> 正在编译XXX模块......</FONT></P>
<P><FONT face="Courier New">如果make执行时,带入make参数“-n”或“--just-print”,那么其只是显示命令,但不会执行命令,这个功能很有利于我们调试我们的Makefile,看看我们书写的命令是执行起来是什么样子的或是什么顺序的。</FONT></P>
<P><FONT face="Courier New">而make参数“-s”或“--slient”则是全面禁止命令的显示。</FONT></P>
<P><FONT face="Courier New"></FONT> </P>
<P><FONT face="Courier New"><STRONG>二、命令执行</STRONG></FONT></P>
<P><FONT face="Courier New">当依赖目标新于目标时,也就是当规则的目标需要被更新时,make会一条一条的执行其后的命令。需要注意的是,如果你要让上一条命令的结果应用在下一条命令时,你应该使用分号分隔这两条命令。比如你的第一条命令是cd命令,你希望第二条命令得在cd之后的基础上运行,那么你就不能把这两条命令写在两行上,而应该把这两条命令写在一行上,用分号分隔。如:</FONT></P>
<P><FONT face="Courier New"> 示例一:<BR> exec:<BR> cd /home/hchen<BR> pwd</FONT></P>
<P><FONT face="Courier New"> 示例二:<BR> exec:<BR> cd /home/hchen; pwd</FONT></P>
<P><FONT face="Courier New">当我们执行“make exec”时,第一个例子中的cd没有作用,pwd会打印出当前的Makefile目录,而第二个例子中,cd就起作用了,pwd会打印出“/home/hchen”。</FONT></P>
<P><FONT face="Courier New">make一般是使用环境变量SHELL中所定义的系统Shell来执行命令,默认情况下使用UNIX的标准Shell——/bin/sh来执行命令。但在MS-DOS下有点特殊,因为MS-DOS下没有SHELL环境变量,当然你也可以指定。如果你指定了UNIX风格的目录形式,首先,make会在SHELL所指定的路径中找寻命令解释器,如果找不到,其会在当前盘符中的当前目录中寻找,如果再找不到,其会在PATH环境变量中所定义的所有路径中寻找。MS-DOS中,如果你定义的命令解释器没有找到,其会给你的命令解释器加上诸如“.exe”、“.com”、“.bat”、“.sh”等后缀。</FONT></P>
<P><BR><BR><FONT face="Courier New"><STRONG>三、命令出错</STRONG></FONT></P>
<P><FONT face="Courier New">每当命令运行完后,make会检测每个命令的返回码,如果命令返回成功,那么make会执行下一条命令,当规则中所有的命令成功返回后,这个规则就算是成功完成了。如果一个规则中的某个命令出错了(命令退出码非零),那么make就会终止执行当前规则,这将有可能终止所有规则的执行。</FONT></P>
<P><FONT face="Courier New">有些时候,命令的出错并不表示就是错误的。例如mkdir命令,我们一定需要建立一个目录,如果目录不存在,那么mkdir就成功执行,万事大吉,如果目录存在,那么就出错了。我们之所以使用mkdir的意思就是一定要有这样的一个目录,于是我们就不希望mkdir出错而终止规则的运行。</FONT></P>
<P><FONT face="Courier New">为了做到这一点,忽略命令的出错,我们可以在Makefile的命令行前加一个减号“-”(在Tab键之后),标记为不管命令出不出错都认为是成功的。如:</FONT></P>
<P><FONT face="Courier New"> clean:<BR> -rm -f *.o</FONT></P>
<P><FONT face="Courier New">还有一个全局的办法是,给make加上“-i”或是“--ignore-errors”参数,那么,Makefile中所有命令都会忽略错误。而如果一个规则是以“.IGNORE”作为目标的,那么这个规则中的所有命令将会忽略错误。这些是不同级别的防止命令出错的方法,你可以根据你的不同喜欢设置。</FONT></P>
<P><FONT face="Courier New">还有一个要提一下的make的参数的是“-k”或是“--keep-going”,这个参数的意思是,如果某规则中的命令出错了,那么就终目该规则的执行,但继续执行其它规则。</FONT></P>
<P><BR><BR><FONT face="Courier New"><STRONG>四、嵌套执行make</STRONG></FONT></P>
<P><FONT face="Courier New">在一些大的工程中,我们会把我们不同模块或是不同功能的源文件放在不同的目录中,我们可以在每个目录中都书写一个该目录的Makefile,这有利于让我们的Makefile变得更加地简洁,而不至于把所有的东西全部写在一个Makefile中,这样会很难维护我们的Makefile,这个技术对于我们模块编译和分段编译有着非常大的好处。</FONT></P>
<P><FONT face="Courier New">例如,我们有一个子目录叫subdir,这个目录下有个Makefile文件,来指明了这个目录下文件的编译规则。那么我们总控的Makefile可以这样书写:</FONT></P>
<P><FONT face="Courier New"> subsystem:<BR> cd subdir && $(MAKE)</FONT></P>
<P><FONT face="Courier New">其等价于:</FONT></P>
<P><FONT face="Courier New"> subsystem:<BR> $(MAKE) -C subdir</FONT></P>
<P><FONT face="Courier New">定义$(MAKE)宏变量的意思是,也许我们的make需要一些参数,所以定义成一个变量比较利于维护。这两个例子的意思都是先进入“subdir”目录,然后执行make命令。</FONT></P>
<P><FONT face="Courier New">我们把这个Makefile叫做“总控Makefile”,总控Makefile的变量可以传递到下级的Makefile中(如果你显示的声明),但是不会覆盖下层的Makefile中所定义的变量,除非指定了“-e”参数。</FONT></P>
<P><FONT face="Courier New">如果你要传递变量到下级Makefile中,那么你可以使用这样的声明:</FONT></P>
<P><FONT face="Courier New"> export <variable ...></FONT></P>
<P><FONT face="Courier New">如果你不想让某些变量传递到下级Makefile中,那么你可以这样声明: </FONT></P>
<P><FONT face="Courier New"> unexport <variable ...></FONT></P>
<P><FONT face="Courier New">如:<BR> <BR> 示例一:</FONT></P>
<P><FONT face="Courier New"> export variable = value</FONT></P>
<P><FONT face="Courier New"> 其等价于:</FONT></P>
<P><FONT face="Courier New"> variable = value<BR> export variable</FONT></P>
<P><FONT face="Courier New"> 其等价于:</FONT></P>
<P><FONT face="Courier New"> export variable := value</FONT></P>
<P><FONT face="Courier New"> 其等价于:</FONT></P>
<P><FONT face="Courier New"> variable := value<BR> export variable</FONT></P>
<P><FONT face="Courier New"> 示例二:</FONT></P>
<P><FONT face="Courier New"> export variable += value</FONT></P>
<P><FONT face="Courier New"> 其等价于:</FONT></P>
<P><FONT face="Courier New"> variable += value<BR> export variable</FONT></P>
<P><FONT face="Courier New">如果你要传递所有的变量,那么,只要一个export就行了。后面什么也不用跟,表示传递所有的变量。</FONT></P>
<P><FONT face="Courier New">需要注意的是,有两个变量,一个是SHELL,一个是MAKEFLAGS,这两个变量不管你是否export,其总是要传递到下层Makefile中,特别是MAKEFILES变量,其中包含了make的参数信息,如果我们执行“总控Makefile”时有make参数或是在上层Makefile中定义了这个变量,那么MAKEFILES变量将会是这些参数,并会传递到下层Makefile中,这是一个系统级的环境变量。</FONT></P>
<P><FONT face="Courier New">但是make命令中的有几个参数并不往下传递,它们是“-C”,“-f”,“-h”“-o”和“-W”(有关Makefile参数的细节将在后面说明),如果你不想往下层传递参数,那么,你可以这样来:</FONT></P>
<P><FONT face="Courier New"> subsystem:<BR> cd subdir && $(MAKE) MAKEFLAGS=</FONT></P>
<P><FONT face="Courier New">如果你定义了环境变量MAKEFLAGS,那么你得确信其中的选项是大家都会用到的,如果其中有“-t”,“-n”,和“-q”参数,那么将会有让你意想不到的结果,或许会让你异常地恐慌。</FONT></P>
<P><FONT face="Courier New">还有一个在“嵌套执行”中比较有用的参数,“-w”或是“--print-directory”会在make的过程中输出一些信息,让你看到目前的工作目录。比如,如果我们的下级make目录是“/home/hchen/gnu/make”,如果我们使用“make -w”来执行,那么当进入该目录时,我们会看到:</FONT></P>
<P><FONT face="Courier New"> make: Entering directory `/home/hchen/gnu/make'.</FONT></P>
<P><FONT face="Courier New">而在完成下层make后离开目录时,我们会看到:</FONT></P>
<P><FONT face="Courier New"> make: Leaving directory `/home/hchen/gnu/make'</FONT></P>
<P><FONT face="Courier New">当你使用“-C”参数来指定make下层Makefile时,“-w”会被自动打开的。如果参数中有“-s”(“--slient”)或是“--no-print-directory”,那么,“-w”总是失效的。</FONT></P>
<P><BR><BR><FONT face="Courier New"><STRONG>五、定义命令包</STRONG></FONT></P>
<P><FONT face="Courier New">如果Makefile中出现一些相同命令序列,那么我们可以为这些相同的命令序列定义一个变量。定义这种命令序列的语法以“define”开始,以“endef”结束,如:</FONT></P>
<P><FONT face="Courier New"> define run-yacc<BR> yacc $(firstword $^)<BR> mv y.tab.c $@<BR> endef</FONT></P>
<P><FONT face="Courier New">这里,“run-yacc”是这个命令包的名字,其不要和Makefile中的变量重名。在“define”和“endef”中的两行就是命令序列。这个命令包中的第一个命令是运行Yacc程序,因为Yacc程序总是生成“y.tab.c”的文件,所以第二行的命令就是把这个文件改改名字。还是把这个命令包放到一个示例中来看看吧。</FONT></P>
<P><FONT face="Courier New"> foo.c : foo.y<BR> $(run-yacc)</FONT></P>
<P><FONT face="Courier New">我们可以看见,要使用这个命令包,我们就好像使用变量一样。在这个命令包的使用中,命令包“run-yacc”中的“$^”就是“foo.y”,“$@”就是“foo.c”(有关这种以“$”开头的特殊变量,我们会在后面介绍),make在执行命令包时,命令包中的每个命令会被依次独立执行。</P>
<P align=right><STRONG><FONT face="Courier New"><-</FONT></STRONG><A href="http://www.csdn.net/Develop/read_article.asp?id=20272"><FONT face="Courier New" color=#0000ff><STRONG>上一页</STRONG></FONT></A><STRONG><FONT face="Courier New"> <A href="http://www.csdn.net/develop/read_article.asp?id=20438"><FONT color=#0000ff>下一页</FONT></A>-></FONT></STRONG></P>
<P><STRONG><FONT face="Courier New">(版权所有,转载时请注明作者和出处)</FONT></STRONG></P>
<P><BR><!--内容结束//--></FONT></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&s=20396">【评论】</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 © 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 + -