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

📄 11.htm

📁 学习MAkefile相关的好资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; sources = foo.c bar.c<BR>&nbsp;&nbsp;&nbsp; ifneq ( $(MAKECMDGOALS),clean)<BR>&nbsp;&nbsp;&nbsp; include $(sources:.c=.d)<BR>&nbsp;&nbsp;&nbsp; endif</FONT></P>
<P><FONT face="Courier New">基于上面的这个例子,只要我们输入的命令不是“make clean”,那么makefile会自动包含“foo.d”和“bar.d”这两个makefile。</FONT></P>
<P><FONT face="Courier New">使用指定终极目标的方法可以很方便地让我们编译我们的程序,例如下面这个例子:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; .PHONY: all<BR>&nbsp;&nbsp;&nbsp; all: prog1 prog2 prog3 prog4</FONT></P>
<P><FONT face="Courier New">从这个例子中,我们可以看到,这个makefile中有四个需要编译的程序——“prog1”, “prog2”, “prog3”和 “prog4”,我们可以使用“make all”命令来编译所有的目标(如果把all置成第一个目标,那么只需执行“make”),我们也可以使用“make prog2”来单独编译目标“prog2”。</FONT></P>
<P><FONT face="Courier New">即然make可以指定所有makefile中的目标,那么也包括“伪目标”,于是我们可以根据这种性质来让我们的makefile根据指定的不同的目标来完成不同的事。在Unix世界中,软件发布时,特别是GNU这种开源软件的发布时,其makefile都包含了编译、安装、打包等功能。我们可以参照这种规则来书写我们的makefile中的目标。</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp; “all”<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个伪目标是所有目标的目标,其功能一般是编译所有的目标。<BR>&nbsp;&nbsp;&nbsp;&nbsp; “clean”<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个伪目标功能是删除所有被make创建的文件。<BR>&nbsp;&nbsp;&nbsp;&nbsp; “install”<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个伪目标功能是安装已编译好的程序,其实就是把目标执行文件拷贝到指定的目标中去。<BR>&nbsp;&nbsp;&nbsp;&nbsp; “print”<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个伪目标的功能是例出改变过的源文件。<BR>&nbsp;&nbsp;&nbsp;&nbsp; “tar”<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个伪目标功能是把源程序打包备份。也就是一个tar文件。<BR>&nbsp;&nbsp;&nbsp;&nbsp; “dist”<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个伪目标功能是创建一个压缩文件,一般是把tar文件压成Z文件。或是gz文件。<BR>&nbsp;&nbsp;&nbsp;&nbsp; “TAGS”<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个伪目标功能是更新所有的目标,以备完整地重编译使用。<BR>&nbsp;&nbsp;&nbsp;&nbsp; “check”和“test”<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这两个伪目标一般用来测试makefile的流程。</FONT></P>
<P><FONT face="Courier New">当然一个项目的makefile中也不一定要书写这样的目标,这些东西都是GNU的东西,但是我想,GNU搞出这些东西一定有其可取之处(等你的UNIX下的程序文件一多时你就会发现这些功能很有用了),这里只不过是说明了,如果你要书写这种功能,最好使用这种名字命名你的目标,这样规范一些,规范的好处就是——不用解释,大家都明白。而且如果你的makefile中有这些功能,一是很实用,二是可以显得你的makefile很专业(不是那种初学者的作品)。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>四、检查规则</STRONG></FONT></P>
<P><FONT face="Courier New">有时候,我们不想让我们的makefile中的规则执行起来,我们只想检查一下我们的命令,或是执行的序列。于是我们可以使用make命令的下述参数:</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; “-n”<BR>&nbsp;&nbsp;&nbsp; “--just-print”<BR>&nbsp;&nbsp;&nbsp; “--dry-run”<BR>&nbsp;&nbsp;&nbsp; “--recon”<BR>&nbsp;&nbsp;&nbsp; 不执行参数,这些参数只是打印命令,不管目标是否更新,把规则和连带规则下的命令打印出来,但不执行,这些参数对于我们调试makefile很有用处。</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; “-t”<BR>&nbsp;&nbsp;&nbsp; “--touch”<BR>&nbsp;&nbsp;&nbsp; 这个参数的意思就是把目标文件的时间更新,但不更改目标文件。也就是说,make假装编译目标,但不是真正的编译目标,只是把目标变成已编译过的状态。</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; “-q”<BR>&nbsp;&nbsp;&nbsp; “--question”<BR>&nbsp;&nbsp;&nbsp; 这个参数的行为是找目标的意思,也就是说,如果目标存在,那么其什么也不会输出,当然也不会执行编译,如果目标不存在,其会打印出一条出错信息。</FONT></P>
<P><FONT face="Courier New">&nbsp;&nbsp;&nbsp; “-W &lt;file&gt;”<BR>&nbsp;&nbsp;&nbsp; “--what-if=&lt;file&gt;”<BR>&nbsp;&nbsp;&nbsp; “--assume-new=&lt;file&gt;”<BR>&nbsp;&nbsp;&nbsp; “--new-file=&lt;file&gt;”<BR>&nbsp;&nbsp;&nbsp; 这个参数需要指定一个文件。一般是是源文件(或依赖文件),Make会根据规则推导来运行依赖于这个文件的命令,一般来说,可以和“-n”参数一同使用,来查看这个依赖文件所发生的规则命令。</FONT></P>
<P><FONT face="Courier New">另外一个很有意思的用法是结合“-p”和“-v”来输出makefile被执行时的信息(这个将在后面讲述)。</FONT></P>
<P><BR><FONT face="Courier New"><STRONG>五、make的参数</STRONG></FONT></P>
<P><FONT face="Courier New">下面列举了所有GNU make 3.80版的参数定义。其它版本和产商的make大同小异,不过其它产商的make的具体参数还是请参考各自的产品文档。</FONT></P>
<P><FONT face="Courier New">“-b”<BR>“-m”<BR>这两个参数的作用是忽略和其它版本make的兼容性。</FONT></P>
<P><FONT face="Courier New">“-B”<BR>“--always-make”<BR>认为所有的目标都需要更新(重编译)。</FONT></P>
<P><FONT face="Courier New">“-C &lt;dir&gt;”<BR>“--directory=&lt;dir&gt;”<BR>指定读取makefile的目录。如果有多个“-C”参数,make的解释是后面的路径以前面的作为相对路径,并以最后的目录作为被指定目录。如:“make –C ~hchen/test –C prog”等价于“make –C ~hchen/test/prog”。</FONT></P>
<P><FONT face="Courier New">“—debug[=&lt;options&gt;]”<BR>输出make的调试信息。它有几种不同的级别可供选择,如果没有参数,那就是输出最简单的调试信息。下面是&lt;options&gt;的取值:<BR>&nbsp;&nbsp;&nbsp; a —— 也就是all,输出所有的调试信息。(会非常的多)<BR>&nbsp;&nbsp;&nbsp; b —— 也就是basic,只输出简单的调试信息。即输出不需要重编译的目标。<BR>&nbsp;&nbsp;&nbsp; v —— 也就是verbose,在b选项的级别之上。输出的信息包括哪个makefile被解析,不需要被重编译的依赖文件(或是依赖目标)等。<BR>&nbsp;&nbsp;&nbsp; i —— 也就是implicit,输出所以的隐含规则。<BR>&nbsp;&nbsp;&nbsp; j —— 也就是jobs,输出执行规则中命令的详细信息,如命令的PID、返回码等。<BR>&nbsp;&nbsp;&nbsp; m —— 也就是makefile,输出make读取makefile,更新makefile,执行makefile的信息。</FONT></P>
<P><FONT face="Courier New">“-d”<BR>相当于“--debug=a”。</FONT></P>
<P><FONT face="Courier New">“-e”<BR>“--environment-overrides”<BR>指明环境变量的值覆盖makefile中定义的变量的值。</FONT></P>
<P><FONT face="Courier New">“-f=&lt;file&gt;”<BR>“--file=&lt;file&gt;”<BR>“--makefile=&lt;file&gt;”<BR>指定需要执行的makefile。</FONT></P>
<P><FONT face="Courier New">“-h”<BR>“--help”<BR>显示帮助信息。</FONT></P>
<P><FONT face="Courier New">“-i”<BR>“--ignore-errors”<BR>在执行时忽略所有的错误。</FONT></P>
<P><FONT face="Courier New">“-I &lt;dir&gt;”<BR>“--include-dir=&lt;dir&gt;”<BR>指定一个被包含makefile的搜索目标。可以使用多个“-I”参数来指定多个目录。</FONT></P>
<P><FONT face="Courier New">“-j [&lt;jobsnum&gt;]”<BR>“--jobs[=&lt;jobsnum&gt;]”<BR>指同时运行命令的个数。如果没有这个参数,make运行命令时能运行多少就运行多少。如果有一个以上的“-j”参数,那么仅最后一个“-j”才是有效的。(注意这个参数在MS-DOS中是无用的)</FONT></P>
<P><FONT face="Courier New">“-k”<BR>“--keep-going”<BR>出错也不停止运行。如果生成一个目标失败了,那么依赖于其上的目标就不会被执行了。</FONT></P>
<P><FONT face="Courier New">“-l &lt;load&gt;”<BR>“--load-average[=&lt;load]”<BR>“—max-load[=&lt;load&gt;]”<BR>指定make运行命令的负载。</FONT></P>
<P><FONT face="Courier New">“-n”<BR>“--just-print”<BR>“--dry-run”<BR>“--recon”<BR>仅输出执行过程中的命令序列,但并不执行。</FONT></P>
<P><FONT face="Courier New">“-o &lt;file&gt;”<BR>“--old-file=&lt;file&gt;”<BR>“--assume-old=&lt;file&gt;”<BR>不重新生成的指定的&lt;file&gt;,即使这个目标的依赖文件新于它。</FONT></P>
<P><FONT face="Courier New">“-p”<BR>“--print-data-base”<BR>输出makefile中的所有数据,包括所有的规则和变量。这个参数会让一个简单的makefile都会输出一堆信息。如果你只是想输出信息而不想执行makefile,你可以使用“make -qp”命令。如果你想查看执行makefile前的预设变量和规则,你可以使用“make –p –f /dev/null”。这个参数输出的信息会包含着你的makefile文件的文件名和行号,所以,用这个参数来调试你的makefile会是很有用的,特别是当你的环境变量很复杂的时候。</FONT></P>
<P><FONT face="Courier New">“-q”<BR>“--question”<BR>不运行命令,也不输出。仅仅是检查所指定的目标是否需要更新。如果是0则说明要更新,如果是2则说明有错误发生。</FONT></P>
<P><FONT face="Courier New">“-r”<BR>“--no-builtin-rules”<BR>禁止make使用任何隐含规则。</FONT></P>
<P><FONT face="Courier New">“-R”<BR>“--no-builtin-variabes”<BR>禁止make使用任何作用于变量上的隐含规则。</FONT></P>
<P><FONT face="Courier New">“-s”<BR>“--silent”<BR>“--quiet”<BR>在命令运行时不输出命令的输出。</FONT></P>
<P><FONT face="Courier New">“-S”<BR>“--no-keep-going”<BR>“--stop”<BR>取消“-k”选项的作用。因为有些时候,make的选项是从环境变量“MAKEFLAGS”中继承下来的。所以你可以在命令行中使用这个参数来让环境变量中的“-k”选项失效。</FONT></P>
<P><FONT face="Courier New">“-t”<BR>“--touch”<BR>相当于UNIX的touch命令,只是把目标的修改日期变成最新的,也就是阻止生成目标的命令运行。</FONT></P>
<P><FONT face="Courier New">“-v”<BR>“--version”<BR>输出make程序的版本、版权等关于make的信息。</FONT></P>
<P><FONT face="Courier New">“-w”<BR>“--print-directory”<BR>输出运行makefile之前和之后的信息。这个参数对于跟踪嵌套式调用make时很有用。</FONT></P>
<P><FONT face="Courier New">“--no-print-directory”<BR>禁止“-w”选项。</FONT></P>
<P><FONT face="Courier New">“-W &lt;file&gt;”<BR>“--what-if=&lt;file&gt;”<BR>“--new-file=&lt;file&gt;”<BR>“--assume-file=&lt;file&gt;”<BR>假定目标&lt;file&gt;需要更新,如果和“-n”选项使用,那么这个参数会输出该目标更新时的运行动作。如果没有“-n”那么就像运行UNIX的“touch”命令一样,使得&lt;file&gt;的修改时间为当前时间。</FONT></P>
<P><FONT face="Courier New">“--warn-undefined-variables”<BR>只要make发现有未定义的变量,那么就输出警告信息。</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=20788"><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=21025"><FONT color=#0000ff>下一页</FONT></A>-&gt;</FONT></STRONG></P>
<P><STRONG><FONT face="Courier New">(版权所有,转载时请注明作者和出处)</FONT></STRONG></P>
<P><BR>&nbsp;</P></FONT></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">flinymengw4u</span>
          <i>(
            <span id="CommnetList1_CommnetList1_rpCommentList__ctl0_lblPostTime">2004-03-30</span>)</i>
        </td>
      </tr>
      <tr>
        <td colspan="3" width="532" bgcolor="#ffffff">
          <span id="CommnetList1_CommnetList1_rpCommentList__ctl0_lblContent">请问如何把脚本编译成2进制文件</span>
        </td>
      </tr>
    </table>
  
<div align=right>
<a id="CommnetList1_CommnetList1_Morelink" href="http://comment.csdn.net/Comment.aspx?c=2&amp;s=20912">【评论】</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 + -