📄 chap3_3.htm
字号:
<html>
<head>
<title>3.3 编译和链接Hello程序</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body link="#3973DE" alink="#3973DE">
<font SIZE="5"><b><div align="center"><center>
<table border="0" width="615" cellspacing="0" cellpadding="0">
<tr>
</b><td><div align="center"><center><table border="0" width="615" cellpadding="0" cellspacing="0" height="20">
<tr>
<td width="377" bgcolor="#15397D" height="20"></td>
<td width="238" bgcolor="#000000" height="20"><p align="right"></font><span style="text-decoration: none"><a href="../../index.htm"><font color="#FFFFFF">电脑报Visual
C++网络教程</font></a></span></td>
</tr>
</table>
</center></div><font FACE="Times New Roman" SIZE="4"><b><p ALIGN="CENTER"></b></font><font color="#3973DE" FACE="Times New Roman" SIZE="4">3.3 </font><font color="#3973DE"><font SIZE="4">编译和链接</font><font FACE="Times New Roman" SIZE="4">Hello</font><font SIZE="4">程序</font></font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY"></font><font SIZE="3">虽然我们到现在为止还没有写任何一行代码,但我们确实得到了一个完整的可运行的程序。要编译运行程序,可以选择</font><font FACE="Times New Roman" SIZE="3">Build-</font><font SIZE="3">(或按快捷键</font><font FACE="Times New Roman" SIZE="3">F7</font><font SIZE="3">),编译该程序。编译完后再选择</font><font FACE="Times New Roman" SIZE="3">Build-Execute Hello.exe</font><font SIZE="3">(或按快捷键</font><font FACE="Times New Roman" SIZE="3">CTRL+F5</font><font SIZE="3">),运行该程序。也可以直接按</font><font FACE="Times New Roman" SIZE="3">CTRL+F5</font><font SIZE="3">,系统提示是否编译,回答“</font><font FACE="Times New Roman" SIZE="3">Yes</font><font SIZE="3">”,</font><font FACE="Times New Roman" SIZE="3">Visual Studio</font><font SIZE="3">将自动编译链接并运行</font><font FACE="Times New Roman" SIZE="3">Hello.exe</font><font SIZE="3">程序。<br>
<b></p>
<div align="center"><center><table border="2" cellspacing="0" width="90%" bgcolor="#CACACA" cellpadding="3">
<tr>
<td width="100%"></b></font><font color="#000000" SIZE="3">提示:在</font><font color="#000000"><font FACE="Times New Roman" SIZE="3">Build</font><font SIZE="3">菜单下有</font><font FACE="Times New Roman" SIZE="3">Compile,Build,Rebuild All</font><font SIZE="3">三个菜单项用于编译程序。其中</font><font FACE="Times New Roman" SIZE="3">Compile</font><font SIZE="3">用于编译当前打开的活动文档;</font><font FACE="Times New Roman" SIZE="3">Build</font><font SIZE="3">只编译工程中上次修改过的文件,并链接程序生成可执行文件。如果以前没有作过编译,它会自动调用</font><font FACE="Times New Roman" SIZE="3">Rebuild All</font><font SIZE="3">操作,依次编译资源文件、源程序文件等;</font><font FACE="Times New Roman" SIZE="3">Rebuild All</font><font SIZE="3">不管文件是否作过修改,都会编译工程中的所有源文件。由于编译链接过程中会产生大量的中间文件和目标文件,它们占用许多硬盘空间,因此</font><font FACE="Times New Roman" SIZE="3">Visual Studio</font><font SIZE="3">在</font><font FACE="Times New Roman" SIZE="3">Build</font><font SIZE="3">下提供了</font><font FACE="Times New Roman" SIZE="3">Clean</font></font><font color="#000000" SIZE="3">菜单项用于清除这些中间文件。用户在完成一个工程后,应及时清理这些中间文件,否则硬盘很快会被耗尽。</font></td>
</tr>
</table>
</center></div><font FACE="Times New Roman" SIZE="3"><p ALIGN="JUSTIFY">Hello.exe</font><font SIZE="3">程序执行后,显示如图</font><font FACE="Times New Roman" SIZE="3">3.8</font><font SIZE="3">所示的窗口。窗口标题为</font><font FACE="Times New Roman" SIZE="3">Untitled-Hello</font><font SIZE="3">。现在我们要在窗口内显示“</font><font FACE="Times New Roman" SIZE="3">Hello,world</font><font SIZE="3">”字样,为此需要手工编辑代码。在类视图</font><font FACE="Times New Roman" SIZE="3">(ClassView)</font><font SIZE="3">中点</font><font FACE="Times New Roman" SIZE="3">CHelloView</font><font SIZE="3">前面的加号,展开</font><font FACE="Times New Roman" SIZE="3">CHelloView</font><font SIZE="3">树,显示它的类成员函数和数据成员。双击</font><font FACE="Times New Roman" SIZE="3">OnDraw</font><font SIZE="3">函数,</font><font FACE="Times New Roman" SIZE="3">Visual Studio</font><font SIZE="3">将打开</font><font FACE="Times New Roman" SIZE="3">HelloView.cpp</font><font SIZE="3">文件并将光标定位在</font><font FACE="Times New Roman" SIZE="3">OnDraw</font><font SIZE="3">函数定义开始处。在</font><font FACE="Times New Roman" SIZE="3">OnDraw</font><font SIZE="3">函数中手工加入代码,如下所示(黑体字为自己加入的代码)。</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="JUSTIFY">void CHelloView::OnDraw(CDC* pDC)</p>
<p ALIGN="JUSTIFY">{</p>
<p ALIGN="JUSTIFY">CHelloDoc* pDoc = GetDocument();</p>
<p ALIGN="JUSTIFY">ASSERT_VALID(pDoc);</p>
<p ALIGN="JUSTIFY">// TODO: add draw code for native data here</p>
<p ALIGN="JUSTIFY"><b>CString str("Hello,World!");</p>
<p ALIGN="JUSTIFY">pDC->TextOut(10,10,str);</b></p>
<p ALIGN="JUSTIFY">}</p>
<p ALIGN="JUSTIFY"></font><font SIZE="3">编译并运行该程序,弹出如图</font><font FACE="Times New Roman" SIZE="3">3.9</font><font SIZE="3">所示的窗口。在窗口左上角显示“</font><font FACE="Times New Roman" SIZE="3">Hello</font><font SIZE="3">,</font><font FACE="Times New Roman" SIZE="3">World</font><font SIZE="3">!”。</font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="center"></font><font SIZE="3"><img src="T3_9.gif" alt="T3_9.tif (271688 bytes)" WIDTH="578" HEIGHT="369"></font><font FACE="Times New Roman" SIZE="3"></p>
<p ALIGN="center"></font><font size="2">图<font FACE="Times New Roman">3.9 </font>显示“<font FACE="Times New Roman">Hello,World</font>!”的窗口<font FACE="Times New Roman"></p>
<p></font></font><font SIZE="3">到现在为止,我们只写了两行代码,就完成了</font><font FACE="Times New Roman" SIZE="3">SDK</font><font SIZE="3">下需要几百行程序才能完成的工作。这应当归功于</font><font FACE="Times New Roman" SIZE="3">Visual Studio</font><font SIZE="3">提供的</font><font FACE="Times New Roman" SIZE="3">AppWizard</font><font SIZE="3">以及</font><font FACE="Times New Roman" SIZE="3">MFC</font><font SIZE="3">框架在幕后所作的大量工作。为了更好的理解和设计基于</font><font FACE="Times New Roman" SIZE="3">MFC</font><font SIZE="3">框架的程序,我们分析一下</font><font FACE="Times New Roman" SIZE="3">MFC</font><font SIZE="3">框架所做的工作。</font><font FACE="Times New Roman" SIZE="3"></p>
</font><div align="center"><center><table border="0" cellpadding="0" cellspacing="0" width="615">
<tr>
<td><a href="chap3_2.htm">上一页</a></td>
<td><p align="right"><a href="chap3_4.htm">下一页</a></td>
</tr>
</table>
</center></div><font SIZE="5"><hr noshade color="#3973DE" size="1">
<p align="center"></font><font size="2" color="#000000">本教程由<a href="http://vcdynasty.yeah.net">Visual C++王朝(Where programmers come together)</a>协助制作<br>
未经许可,请勿以任何形式复制</font></td>
<b>
</tr>
</table>
</center></div>
<p ALIGN="CENTER"></b><font SIZE="5"> </font><font FACE="Times New Roman" SIZE="5"></p>
</font><font FACE="Times New Roman" SIZE="3">
<p></font><b><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
</font></b>
<p><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
<p></font> </p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -