一个最简单的静态连接库问题

lj1006 2003-10-09 01:38:36
小弟刚看了关于静态连接库,想照着书试一下,我按部就班的做了,但就是不成功,
还请各位多指点一下我,谢谢

先用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)

不知是怎么错了啊,请大虾指点!!!
...全文
40 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lj1006 2003-10-12
  • 打赏
  • 举报
回复
多谢!
lop5712 2003-10-11
  • 打赏
  • 举报
回复
你后面出现的错误是编译错误,不是连接错误了,因为你把mymath.h移到了最顶上所致
D:\My Documents\c++\临时\ff\ffDlg.cpp(177) : error C2065: 'Summary' : undeclared identifier
D:\My Documents\c++\临时\ff\ffDlg.cpp(189) : error C2065: 'Factorial' : undeclared identifier

因为你开启了“预编译头文件”编译选项,向导缺省帮你开的,工程中所有的源文件(.c/.cpp)都必须以#include "stdafx.h"为第一句话,之前的任何命令(包括预编译命令)都等于没有,而你的#include "mymath.h"在其之前,导致这个错误。
将#include "mymath.h"移到#include "stdafx.h"之后即可
lj1006 2003-10-10
  • 打赏
  • 举报
回复
to lop5712(LOP):十分谢谢你的帮助,我真的很感激,但我是象你那样写的,编译lib文件时没有错,当我测试那个lib文件时总是那个错误,我好郁闷,是不是在测试程序中要加入什么语句啊!再帮帮小弟!
lop5712 2003-10-09
  • 打赏
  • 举报
回复
楼主有没有搞错,我没有任何问题,一次连接通过,去掉extern "C"就失败,楼主是否是改成如下:
extern "C" int Summary(int n)

{

int sum=0;

int i;

for(i=1;i<=n;i++)

{

sum+=i;

}

return sum;

}

extern "C" int Factorial(int n)

{

int Fact=1;

int i;

for(i=1;i<=n;i++)

{

Fact=Fact*i;

}

return Fact;

}

lop5712 2003-10-09
  • 打赏
  • 举报
回复
真的??!!!
我帮你回去试一下
lj1006 2003-10-09
  • 打赏
  • 举报
回复
我在test 目录中的 mymath.h 中加入 1 行
#pragma comment(lib, _mymath.lib) 为什么还是不行啊

Compiling...
ffDlg.cpp
D:\My Documents\c++\临时\ff\ffDlg.cpp(177) : error C2065: 'Summary' : undeclared identifier
D:\My Documents\c++\临时\ff\ffDlg.cpp(189) : error C2065: 'Factorial' : undeclared identifier
Error executing cl.exe.

ff.exe - 2 error(s), 0 warning(s)
xjy521 2003-10-09
  • 打赏
  • 举报
回复
关注
lj1006 2003-10-09
  • 打赏
  • 举报
回复
我在在 test 目录中的 mymath.h 中加入 1 行
#pragma comment(lib, mymath.lib) 也不行啊
lj1006 2003-10-09
  • 打赏
  • 举报
回复
to lop5712(LOP) 多谢这位大虾的赐教,但是我在mymath.cpp中加入extern "C"后还是有错

:\My Documents\c++\临时\ff\ffDlg.cpp(177) : error C2065: 'Summary' : undeclared identifier
D:\My Documents\c++\临时\ff\ffDlg.cpp(189) : error C2065: 'Factorial' : undeclared identifier
Error executing cl.exe.

ff.exe - 2 error(s), 0 warning(s)

究竟是为什么啊
awant2k 2003-10-09
  • 打赏
  • 举报
回复

#pragma comment(lib, "mymath.lib")
awant2k 2003-10-09
  • 打赏
  • 举报
回复
也可以在 test 目录中的 mymath.h 中加入 1 行
#pragma comment(lib, _mymath.lib)
awant2k 2003-10-09
  • 打赏
  • 举报
回复
在项目的 Project Settings ==> Link ==> Object/library module: 后面加上 mymath.lib
mfc168 2003-10-09
  • 打赏
  • 举报
回复
gz
lop5712 2003-10-09
  • 打赏
  • 举报
回复
To ruihuahan(飞不起来的笨鸟)
可以那样做,VC同意将.lib文件包进工程,然后自动将其添加到Linker的命令行中
ruihuahan 2003-10-09
  • 打赏
  • 举报
回复
然后用Project->Add to Project->Files命令,将mymath.lib加入到工程中。
----这里错误,应该在project setting中设置
lop5712 2003-10-09
  • 打赏
  • 举报
回复
当然出错,extren "C"不是这样用的

extern "C" 是告诉编译器后面所接的函数的名字在编译阶段不要修改,称做C风格是相对于C++说的,因为C++下可以函数重载,所以编译器会根据函数的原型修改函数名。

楼主在声明时使用extern "C",定义时却没用,所以编译器生成的函数名字被修改,不是原来的Summary和Factorial,但连接时,由于楼主的.h文件中说明是extern "C",所以编译器就以未改变的名字去连接,而实际是变了的,所以找不到名字

楼主只需在函数的定义前也加上extern "C" 即可
lj1006 2003-10-09
  • 打赏
  • 举报
回复
我将在testdlg.cpp文件中头文件mymath.h移到最顶上,就提示错误

--------------------Configuration: ff - Win32 Debug--------------------
Compiling...
ffDlg.cpp
D:\My Documents\c++\临时\ff\ffDlg.cpp(177) : error C2065: 'Summary' : undeclared identifier
D:\My Documents\c++\临时\ff\ffDlg.cpp(189) : error C2065: 'Factorial' : undeclared identifier
Error executing cl.exe.

ff.exe - 2 error(s), 0 warning(s)

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧