📄 3.htm
字号:
<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">23737</span>
评论:
<span id="ArticleTitle1_ArticleTitle1_lblCommentCount">7</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><STRONG><FONT face="Courier New" size=4>Makefile 总述<BR>———————</FONT></STRONG></P>
<P><STRONG><FONT face="Courier New">一、Makefile里有什么?</FONT></STRONG></P>
<P><FONT face="Courier New">Makefile里主要包含了五个东西:显式规则、隐晦规则、变量定义、文件指示和注释。</FONT></P>
<P><FONT face="Courier New">1、显式规则。显式规则说明了,如何生成一个或多的的目标文件。这是由Makefile的书写者明显指出,要生成的文件,文件的依赖文件,生成的命令。</FONT></P>
<P><FONT face="Courier New">2、隐晦规则。由于我们的make有自动推导的功能,所以隐晦的规则可以让我们比较粗糙地简略地书写Makefile,这是由make所支持的。</FONT></P>
<P><FONT face="Courier New">3、变量的定义。在Makefile中我们要定义一系列的变量,变量一般都是字符串,这个有点你C语言中的宏,当Makefile被执行时,其中的变量都会被扩展到相应的引用位置上。</FONT></P>
<P><FONT face="Courier New">4、文件指示。其包括了三个部分,一个是在一个Makefile中引用另一个Makefile,就像C语言中的include一样;另一个是指根据某些情况指定Makefile中的有效部分,就像C语言中的预编译#if一样;还有就是定义一个多行的命令。有关这一部分的内容,我会在后续的部分中讲述。</FONT></P>
<P><FONT face="Courier New">5、注释。Makefile中只有行注释,和UNIX的Shell脚本一样,其注释是用“#”字符,这个就像C/C++中的“//”一样。如果你要在你的Makefile中使用“#”字符,可以用反斜框进行转义,如:“\#”。</FONT></P>
<P><FONT face="Courier New">最后,还值得一提的是,在Makefile中的命令,必须要以[Tab]键开始。</FONT></P>
<P><BR><STRONG><FONT face="Courier New">二、Makefile的文件名</FONT></STRONG></P>
<P><FONT face="Courier New">默认的情况下,make命令会在当前目录下按顺序找寻文件名为“GNUmakefile”、“makefile”、“Makefile”的文件,找到了解释这个文件。在这三个文件名中,最好使用“Makefile”这个文件名,因为,这个文件名第一个字符为大写,这样有一种显目的感觉。最好不要用“GNUmakefile”,这个文件是GNU的make识别的。有另外一些make只对全小写的“makefile”文件名敏感,但是基本上来说,大多数的make都支持“makefile”和“Makefile”这两种默认文件名。</FONT></P>
<P><FONT face="Courier New">当然,你可以使用别的文件名来书写Makefile,比如:“Make.Linux”,“Make.Solaris”,“Make.AIX”等,如果要指定特定的Makefile,你可以使用make的“-f”和“--file”参数,如:make -f Make.Linux或make --file Make.AIX。</FONT></P>
<P><BR><STRONG><FONT face="Courier New">三、引用其它的Makefile</FONT></STRONG></P>
<P><FONT face="Courier New">在Makefile使用include关键字可以把别的Makefile包含进来,这很像C语言的#include,被包含的文件会原模原样的放在当前文件的包含位置。include的语法是:</FONT></P>
<P><FONT face="Courier New"> include <filename></FONT></P>
<P><FONT face="Courier New"> filename可以是当前操作系统Shell的文件模式(可以保含路径和通配符)</FONT></P>
<P><FONT face="Courier New">在include前面可以有一些空字符,但是绝不能是[Tab]键开始。include和<filename>可以用一个或多个空格隔开。举个例子,你有这样几个Makefile:a.mk、b.mk、c.mk,还有一个文件叫foo.make,以及一个变量$(bar),其包含了e.mk和f.mk,那么,下面的语句:</FONT></P>
<P><FONT face="Courier New"> include foo.make *.mk $(bar)</FONT></P>
<P><FONT face="Courier New"> 等价于:</FONT></P>
<P><FONT face="Courier New"> include foo.make a.mk b.mk c.mk e.mk f.mk</FONT></P>
<P><FONT face="Courier New">make命令开始时,会把找寻include所指出的其它Makefile,并把其内容安置在当前的位置。就好像C/C++的#include指令一样。如果文件都没有指定绝对路径或是相对路径的话,make会在当前目录下首先寻找,如果当前目录下没有找到,那么,make还会在下面的几个目录下找:</FONT></P>
<P><FONT face="Courier New"> 1、如果make执行时,有“-I”或“--include-dir”参数,那么make就会在这个参数所指定的目录下去寻找。<BR> 2、如果目录<prefix>/include(一般是:/usr/local/bin或/usr/include)存在的话,make也会去找。</FONT></P>
<P><FONT face="Courier New">如果有文件没有找到的话,make会生成一条警告信息,但不会马上出现致命错误。它会继续载入其它的文件,一旦完成makefile的读取,make会再重试这些没有找到,或是不能读取的文件,如果还是不行,make才会出现一条致命信息。如果你想让make不理那些无法读取的文件,而继续执行,你可以在include前加一个减号“-”。如:</FONT></P>
<P><FONT face="Courier New"> -include <filename><BR> 其表示,无论include过程中出现什么错误,都不要报错继续执行。和其它版本make兼容的相关命令是sinclude,其作用和这一个是一样的。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>四、环境变量 MAKEFILES</STRONG> </FONT></P>
<P><FONT face="Courier New">如果你的当前环境中定义了环境变量MAKEFILES,那么,make会把这个变量中的值做一个类似于include的动作。这个变量中的值是其它的Makefile,用空格分隔。只是,它和include不同的是,从这个环境变中引入的Makefile的“目标”不会起作用,如果环境变量中定义的文件发现错误,make也会不理。</FONT></P>
<P><FONT face="Courier New">但是在这里我还是建议不要使用这个环境变量,因为只要这个变量一被定义,那么当你使用make时,所有的Makefile都会受到它的影响,这绝不是你想看到的。在这里提这个事,只是为了告诉大家,也许有时候你的Makefile出现了怪事,那么你可以看看当前环境中有没有定义这个变量。</FONT></P>
<P><BR><STRONG><FONT face="Courier New">五、make的工作方式</FONT></STRONG></P>
<P><FONT face="Courier New">GNU的make工作时的执行步骤入下:(想来其它的make也是类似)</FONT></P>
<P><FONT face="Courier New"> 1、读入所有的Makefile。<BR> 2、读入被include的其它Makefile。<BR> 3、初始化文件中的变量。<BR> 4、推导隐晦规则,并分析所有规则。<BR> 5、为所有的目标文件创建依赖关系链。<BR> 6、根据依赖关系,决定哪些目标要重新生成。<BR> 7、执行生成命令。</FONT></P>
<P><FONT face="Courier New">1-5步为第一个阶段,6-7为第二个阶段。第一个阶段中,如果定义的变量被使用了,那么,make会把其展开在使用的位置。但make并不会完全马上展开,make使用的是拖延战术,如果变量出现在依赖关系的规则中,那么仅当这条依赖被决定要使用了,变量才会在其内部展开。</FONT></P>
<P><FONT face="Courier New">当然,这个工作方式你不一定要清楚,但是知道这个方式你也会对make更为熟悉。有了这个基础,后续部分也就容易看懂了。</FONT></P>
<P><FONT face="Courier New" color=#ff0000><STRONG></STRONG></FONT> </P>
<P align=right><STRONG><FONT face="Courier New"><-</FONT></STRONG><A href="http://www.csdn.net/Develop/read_article.asp?id=20026"><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=20141"><FONT color=#0000ff>下一页</FONT></A>-></FONT></STRONG></P>
<P><STRONG><FONT face="Courier New">(版权所有,转载时请注明作者和出处)</FONT></STRONG></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>
<table border="0" cellpadding="2" cellspacing="1" width="100%" align="center" bgcolor="#666666">
<tr>
<td colspan="3" bgcolor="#cccccc">
<span style="color:#990000;">
<img src="/images/ico_pencil.gif" hspace="1" height="16" width="16">
</span>
<span id="CommnetList1_CommnetList1_rpCommentList__ctl0_lblUserName">CSDN 网友</span>
<i>(
<span id="CommnetList1_CommnetList1_rpCommentList__ctl0_lblPostTime">2005-04-13</span>)</i>
</td>
</tr>
<tr>
<td colspan="3" width="532" bgcolor="#ffffff">
<span id="CommnetList1_CommnetList1_rpCommentList__ctl0_lblContent">谢谢您,在这里我学习到了知识,我敬佩您的专业精神和奉献精神.</span>
</td>
</tr>
</table>
<table border="0" cellpadding="2" cellspacing="1" width="100%" align="center" bgcolor="#666666">
<tr>
<td colspan="3" bgcolor="#cccccc">
<span style="color:#990000;">
<img src="/images/ico_pencil.gif" hspace="1" height="16" width="16">
</span>
<span id="CommnetList1_CommnetList1_rpCommentList__ctl1_lblUserName">CSDN 网友</span>
<i>(
<span id="CommnetList1_CommnetList1_rpCommentList__ctl1_lblPostTime">2005-04-07</span>)</i>
</td>
</tr>
<tr>
<td colspan="3" width="532" bgcolor="#ffffff">
<span id="CommnetList1_CommnetList1_rpCommentList__ctl1_lblContent">!!!!!!!!!!!!!!!!!!!达人!!!!!!!!!!!<br>真的有</span>
</td>
</tr>
</table>
<table border="0" cellpadding="2" cellspacing="1" width="100%" align="center" bgcolor="#666666">
<tr>
<td colspan="3" bgcolor="#cccccc">
<span style="color:#990000;">
<img src="/images/ico_pencil.gif" hspace="1" height="16" width="16">
</span>
<span id="CommnetList1_CommnetList1_rpCommentList__ctl2_lblUserName">neouza</span>
<i>(
<span id="CommnetList1_CommnetList1_rpCommentList__ctl2_lblPostTime">2004-12-15</span>)</i>
</td>
</tr>
<tr>
<td colspan="3" width="532" bgcolor="#ffffff">
<span id="CommnetList1_CommnetList1_rpCommentList__ctl2_lblContent">写得很明白,谢谢.</span>
</td>
</tr>
</table>
<table border="0" cellpadding="2" cellspacing="1" width="100%" align="center" bgcolor="#666666">
<tr>
<td colspan="3" bgcolor="#cccccc">
<span style="color:#990000;">
<img src="/images/ico_pencil.gif" hspace="1" height="16" width="16">
</span>
<span id="CommnetList1_CommnetList1_rpCommentList__ctl3_lblUserName">CSDN 网友</span>
<i>(
<span id="CommnetList1_CommnetList1_rpCommentList__ctl3_lblPostTime">2004-10-21</span>)</i>
</td>
</tr>
<tr>
<td colspan="3" width="532" bgcolor="#ffffff">
<span id="CommnetList1_CommnetList1_rpCommentList__ctl3_lblContent">写的很明白,佩服!!!!</span>
</td>
</tr>
</table>
<table border="0" cellpadding="2" cellspacing="1" width="100%" align="center" bgcolor="#666666">
<tr>
<td colspan="3" bgcolor="#cccccc">
<span style="color:#990000;">
<img src="/images/ico_pencil.gif" hspace="1" height="16" width="16">
</span>
<span id="CommnetList1_CommnetList1_rpCommentList__ctl4_lblUserName">CSDN 网友</span>
<i>(
<span id="CommnetList1_CommnetList1_rpCommentList__ctl4_lblPostTime">2004-10-20</span>)</i>
</td>
</tr>
<tr>
<td colspan="3" width="532" bgcolor="#ffffff">
<span id="CommnetList1_CommnetList1_rpCommentList__ctl4_lblContent">我顶</span>
</td>
</tr>
</table>
<div align=right>
<a id="CommnetList1_CommnetList1_Morelink" href="http://comment.csdn.net/Comment.aspx?c=2&s=20098">【评论】</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 + -