📄 8.htm
字号:
<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">20558</span>
评论:
<span id="ArticleTitle1_ArticleTitle1_lblCommentCount">2</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"><STRONG>六、多行变量</STRONG><BR> <BR>还有一种设置变量值的方法是使用define关键字。使用define关键字设置变量的值可以有换行,这有利于定义一系列的命令(前面我们讲过“命令包”的技术就是利用这个关键字)。</FONT></P>
<P><FONT face="Courier New">define指示符后面跟的是变量的名字,而重起一行定义变量的值,定义是以endef关键字结束。其工作方式和“=”操作符一样。变量的值可以包含函数、命令、文字,或是其它变量。因为命令需要以[Tab]键开头,所以如果你用define定义的命令变量中没有以[Tab]键开头,那么make就不会把其认为是命令。</FONT></P>
<P><FONT face="Courier New">下面的这个示例展示了define的用法:</FONT></P>
<P><FONT face="Courier New"> define two-lines<BR> echo foo<BR> echo $(bar)<BR> endef</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>七、环境变量</STRONG></FONT></P>
<P><FONT face="Courier New">make运行时的系统环境变量可以在make开始运行时被载入到Makefile文件中,但是如果Makefile中已定义了这个变量,或是这个变量由make命令行带入,那么系统的环境变量的值将被覆盖。(如果make指定了“-e”参数,那么,系统环境变量将覆盖Makefile中定义的变量)</FONT></P>
<P><FONT face="Courier New">因此,如果我们在环境变量中设置了“CFLAGS”环境变量,那么我们就可以在所有的Makefile中使用这个变量了。这对于我们使用统一的编译参数有比较大的好处。如果Makefile中定义了CFLAGS,那么则会使用Makefile中的这个变量,如果没有定义则使用系统环境变量的值,一个共性和个性的统一,很像“全局变量”和“局部变量”的特性。</FONT></P>
<P><FONT face="Courier New">当make嵌套调用时(参见前面的“嵌套调用”章节),上层Makefile中定义的变量会以系统环境变量的方式传递到下层的Makefile中。当然,默认情况下,只有通过命令行设置的变量会被传递。而定义在文件中的变量,如果要向下层Makefile传递,则需要使用exprot关键字来声明。(参见前面章节)</FONT></P>
<P><FONT face="Courier New">当然,我并不推荐把许多的变量都定义在系统环境中,这样,在我们执行不用的Makefile时,拥有的是同一套系统变量,这可能会带来更多的麻烦。</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">当然,我样同样可以为某个目标设置局部变量,这种变量被称为“Target-specific Variable”,它可以和“全局变量”同名,因为它的作用范围只在这条规则以及连带规则中,所以其值也只在作用范围内有效。而不会影响规则链以外的全局变量的值。</FONT></P>
<P><FONT face="Courier New">其语法是:</FONT></P>
<P><FONT face="Courier New"> <target ...> : <variable-assignment></FONT></P>
<P><FONT face="Courier New"> <target ...> : overide <variable-assignment></FONT></P>
<P><FONT face="Courier New"><variable-assignment>可以是前面讲过的各种赋值表达式,如“=”、“:=”、“+=”或是“?=”。第二个语法是针对于make命令行带入的变量,或是系统环境变量。</FONT></P>
<P><FONT face="Courier New">这个特性非常的有用,当我们设置了这样一个变量,这个变量会作用到由这个目标所引发的所有的规则中去。如:</FONT></P>
<P><FONT face="Courier New"> prog : CFLAGS = -g<BR> prog : prog.o foo.o bar.o<BR> $(CC) $(CFLAGS) prog.o foo.o bar.o</FONT></P>
<P><FONT face="Courier New"> prog.o : prog.c<BR> $(CC) $(CFLAGS) prog.c</FONT></P>
<P><FONT face="Courier New"> foo.o : foo.c<BR> $(CC) $(CFLAGS) foo.c</FONT></P>
<P><FONT face="Courier New"> bar.o : bar.c<BR> $(CC) $(CFLAGS) bar.c<BR> <BR>在这个示例中,不管全局的$(CFLAGS)的值是什么,在prog目标,以及其所引发的所有规则中(prog.o foo.o bar.o的规则),$(CFLAGS)的值都是“-g”</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>九、模式变量</STRONG></FONT></P>
<P><FONT face="Courier New">在GNU的make中,还支持模式变量(Pattern-specific Variable),通过上面的目标变量中,我们知道,变量可以定义在某个目标上。模式变量的好处就是,我们可以给定一种“模式”,可以把变量定义在符合这种模式的所有目标上。</FONT></P>
<P><FONT face="Courier New">我们知道,make的“模式”一般是至少含有一个“%”的,所以,我们可以以如下方式给所有以[.o]结尾的目标定义目标变量:</FONT></P>
<P><FONT face="Courier New"> %.o : CFLAGS = -O</FONT></P>
<P><FONT face="Courier New">同样,模式变量的语法和“目标变量”一样:</FONT></P>
<P><FONT face="Courier New"> <pattern ...> : <variable-assignment></FONT></P>
<P><FONT face="Courier New"> <pattern ...> : override <variable-assignment></FONT></P>
<P><FONT face="Courier New">override同样是针对于系统环境传入的变量,或是make命令行指定的变量。<BR> </FONT></P>
<P><BR><FONT face="Courier New" size=4><STRONG>使用条件判断<BR>——————</STRONG></FONT></P>
<P><FONT face="Courier New">使用条件判断,可以让make根据运行时的不同情况选择不同的执行分支。条件表达式可以是比较变量的值,或是比较变量和常量的值。</FONT></P>
<P><FONT face="Courier New"><STRONG>一、示例</STRONG></FONT></P>
<P><FONT face="Courier New">下面的例子,判断$(CC)变量是否“gcc”,如果是的话,则使用GNU函数编译目标。</FONT></P>
<P><FONT face="Courier New"> libs_for_gcc = -lgnu<BR> normal_libs =</FONT></P>
<P><FONT face="Courier New"> foo: $(objects)<BR> ifeq ($(CC),gcc)<BR> $(CC) -o foo $(objects) $(libs_for_gcc)<BR> else<BR> $(CC) -o foo $(objects) $(normal_libs)<BR> endif</FONT></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -