📄 chap9_2.htm
字号:
<html><head><title>9.2静态连接库</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="GENERATOR" content="Microsoft FrontPage 3.0"><link rel="stylesheet" href="../../../cpcw.css"></head><body link="#3973DE" alink="#3973DE" background="../../bg.gif"><div align="center"><center> <table width="85%" border="0"> <tr bgcolor="#FFFFFF"> <td> <div align="center"> <center> </center> </div> <p align="CENTER"><b><font color="red">9.2静态连接库</font></b></p> <p> <b><font color="blue">9.2.1创建静态库</font></b></p> <p> 现在以一个简单的数学函数库为例介绍静态库的创建和使用。</p> <p> 要创建静态库,选择File->New菜单,弹出New对话框。选择Projects标签,在项目类型列表框中选择Win32 Static Library,在Name中输入mymath,表明要创建一个mymath.lib的静态库文件。</p> <p> 然后用Project->Add to Project->Files菜单往mymath工程中加入以下两个文件:</p> <p> 1.头文件(见清单9.1):定义了Summary和Factorial两个函数,分别用于完成求和与阶乘。注意这里使用C风格的函数,需要加入extern “C”关键字,表明它是C风格的外部函数。</p> <p> <b></b>清单9.1 头文件</p> <p>#ifndef _MYMATH_H</p> <p>#define _MYMATH_H</p> <p>extern “C”</p> <p>{</p> <p>int Summary(int n);</p> <p>int Factorial(int n);</p> <p>}</p> <p>#endif</p> <p>2.源文件:包含了Summary和Factorial函数的定义,见清单9.2。</p> <p> <b></b>清单9.2 源文件</p> <p>int Summary(int n)</p> <p>{</p> <p>int sum=0;</p> <p>int i;</p> <p>for(i=1;i<=n;i++)</p> <p>{</p> <p>sum+=i;</p> <p>}</p> <p>return sum;</p> <p>}</p> <p>int Factorial(int n)</p> <p>{</p> <p>int Fact=1;</p> <p>int i;</p> <p>for(i=1;i<=n;i++)</p> <p>{</p> <p>Fact=Fact*i;</p> <p>}</p> <p>return Fact;</p> <p>}</p> <p> </p> <p> 在Build菜单下,选择Build菜单下的Build mymath.lib。Visual C++编译链接工程,在mymath\debug目录下生成mymath.lib文件。至此,静态连接库生成的工作就做完了。下面用一个小程序来测试这个静态库。</p> <div align="center"> <center> <table border="2" cellpadding="2" cellspacing="0" width="100%" bgcolor="#80D6FF"> <tr> <td width="100%"> <b></b>提示:用户在交付最终静态连接库时,只需要提供.lib文件和头文件,不需要再提供库的源代码。</td> </tr> </table> </center> </div> <p><b> </b></p> <p><b><font color="blue">9.2.2测试静态库</font></b></p> <p><b> </b></p> <p><b> </b>用AppWizard生成一个基于对话框的应用程序test。打开test资源文件,修改IDD_TEST_DIALOG对话框资源,加入两个按钮。按钮ID和文字为:</p> <p> IDC_SUM “&Summary”</p> <p> IDC_FACTORIAL “&Factorial”</p> <p> 如图9-1所示。</p> <p align="center"> <img src="T9_1.gif" alt="T9_1.tif (84932 bytes)" width="376" height="203"></p> <p align="center"> 图9-1 修改test对话框</p> <p> 用ClassWizard为上述两个按钮Click事件生成消息处理函数OnSum和OnFactorial,并加入代码,修改后的OnSum和OnFactorial见清单9.3。</p> <p> 清单9.3 OnSum和OnFactorial函数定义</p> <p>void CTestDlg::OnSum() </p> <p>{</p> <p>// TODO: Add your control notification handler code here</p> <p>int nSum=Summary(10);</p> <p>CString sResult;</p> <p>sResult.Format("Sum(10)=%d",nSum);</p> <p>AfxMessageBox(sResult);</p> <p>}</p> <p>void CTestDlg::OnFactorial() </p> <p>{</p> <p>// TODO: Add your control notification handler code here</p> <p>int nFact=Factorial(10);</p> <p>CString sResult;</p> <p>sResult.Format("10!=%d",nFact);</p> <p>AfxMessageBox(sResult);</p> <p>}</p> <p>由于要使用mymath.lib中的函数,首先要将mymath.lib和mymath.h两个文件拷贝到test目录下。然后用Project->Add to Project->Files命令,将mymath.lib加入到工程中。</p> <p> 在testdlg.cpp文件头部,还要加入头文件mymath.h:</p> <p>#include "stdafx.h"</p> <p>#include "Test.h"</p> <p>#include "TestDlg.h"</p> <p> </p> <p>#include "mymath.h"</p> <p>#ifdef _DEBUG</p> <p>#define new DEBUG_NEW</p> <p>#undef THIS_FILE</p> <p>static char THIS_FILE[] = __FILE__;</p> <p>#endif</p> <p>编译运行test程序,点Factorial按钮,弹出如图9-2的消息框。</p> <p align="center"> <img src="T9_2.gif" alt="T9_2.tif (33706 bytes)" width="174" height="133"></p> <p align="center">图9-2 Test程序运行结果</p> <div align="center"> <center> <table border="0" cellpadding="0" cellspacing="0" width="615"> <tr> <td><a href="chap9_1.htm">上一页</a></td> <td> <p align="right"><a href="chap9_3.htm">下一页</a> </td> </tr> </table> <p><a href="http://www.cpcw.com">电脑报首页</a> <a href="../../index.htm">网络学院首页</a></p> </center> </div> <hr noshade color="#3973DE" size="1"> </td> </tr> </table> </center></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -