一个最简单的静态连接库问题
小弟刚看了关于静态连接库,想照着书试一下,我按部就班的做了,但就是不成功,
还请各位多指点一下我,谢谢
先用Project->Add to Project->Files菜单往mymath工程中加入以下两个文件:
1.头文件(mymath.h)(见清单9.1):定义了Summary和Factorial两个函数,分别用于完成求和与阶乘。注意这里使用C风格的函数,需要加入extern “C”关键字,表明它是C风格的外部函数。
清单9.1 头文件
#ifndef _MYMATH_H
#define _MYMATH_H
extern "C"
{
int Summary(int n);
int Factorial(int n);
}
#endif
2.源文件(mymath.cpp):包含了Summary和Factorial函数的定义,见清单9.2。
清单9.2 源文件
int Summary(int n)
{
int sum=0;
int i;
for(i=1;i<=n;i++)
{
sum+=i;
}
return sum;
}
int Factorial(int n)
{
int Fact=1;
int i;
for(i=1;i<=n;i++)
{
Fact=Fact*i;
}
return Fact;
}
生成mymath.lib文件。下面用一个小程序来测试这个静态库。
9.2.2测试静态库
用AppWizard生成一个基于对话框的应用程序test。打开test资源文件,修改IDD_TEST_DIALOG对话框资源,加入两个按钮。按钮ID和文字为:
IDC_SUM “&Summary”
IDC_FACTORIAL “&Factorial”
清单9.3 OnSum和OnFactorial函数定义
void CTestDlg::OnSum()
{
// TODO: Add your control notification handler code here
int nSum=Summary(10);
CString sResult;
sResult.Format("Sum(10)=%d",nSum);
AfxMessageBox(sResult);
}
void CTestDlg::OnFactorial()
{
// TODO: Add your control notification handler code here
int nFact=Factorial(10);
CString sResult;
sResult.Format("10!=%d",nFact);
AfxMessageBox(sResult);
}
将mymath.lib和mymath.h两个文件拷贝到test目录下。然后用Project->Add to Project->Files命令,将mymath.lib加入到工程中。
在testdlg.cpp文件头部,加入头文件mymath.h:
编译运行test程序,提示错误
Linking...
ffDlg.obj : error LNK2001: unresolved external symbol _Summary
ffDlg.obj : error LNK2001: unresolved external symbol _Factorial
Debug/ff.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
ff.exe - 3 error(s), 0 warning(s)
不知是怎么错了啊,请大虾指点!!!