隐式调用动态链接库的问题

czx605 2008-07-10 10:45:52
  一、创建Non-MFC DLL动态链接库

  1、打开File —> New —> Project选项,选择Win32 Dynamic-Link Library —>sample project

  —>工程名:DllDemo

  2、新建一个.h文件DllDemo.h
  #ifdef DllDemo_EXPORTS
  #define DllAPI __declspec(dllexport)
  #else
  #define DllAPI __declspec(dllimport)
  extern "C" //原样编译
  {
  DllAPI int __stdcall Max(int a,int b); //__stdcall使非C/C++语言内能够调用API
  }
  #endif
3、在DllDemo.cpp文件中导入DllDemo.h文件,并实现Max(int,int)函数
#include "DllDemo.h"
  DllAPI int __stdcall Max(int a,int b)
  {
  if(a==b)
  return NULL;
  else if(a>b)
  return a;
  else
  return b;
  }
4、编译程序生成动态连接库

  二、用.def文件创建动态连接库DllDemo.dll。

  1、删除DllDemo工程中的DllDemo.h文件。

  2、在DllDemo.cpp文件头,删除 #include DllDemo.h语句。

  3、向该工程中加入一个文本文件,命名为DllDemo.def并写入如下语句:

  LIBRARY MyDll

  EXPORTS

  Max@1

  4、编译程序生成动态连接库。

在第二步骤中 编译程序生成动态链接库时出现下面错误:
Creating library Debug/dllDemo.lib and object Debug/dllDemo.exp
dllDemo.exp : error LNK2001: unresolved external symbol Max@1
Debug/dllDemo.dll : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.me

dllDemo.dll - 1 error(s), 0 warning(s)

...全文
187 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
czx605 2008-07-10
  • 打赏
  • 举报
回复
我用的vc6
没看到工程设置->连接->常规->不产生.lib文件(没这个选项)
czx605 2008-07-10
  • 打赏
  • 举报
回复
我加了lib.def 之后就会出现组建错误
在debug文件夹里有.lib没有.Dll
artman 2008-07-10
  • 打赏
  • 举报
回复
Project setting\Link\general\ Doesn't Produce .Lib 不要打勾!

生成dll, 就会生成.lib
czx605 2008-07-10
  • 打赏
  • 举报
回复
我现在.dll文件产生不出来之产生一个.lib
czx605 2008-07-10
  • 打赏
  • 举报
回复
/* lib.h */

#ifndef LIB_H
#define LIB_H
extern "C" int add(int x,int y);
#endif

/* lib.cpp */

#include "lib.h"
int add(int x,int y)
{
return x + y;
}

/* lib.def */

LIBRARY dllTest
EXPORTS
add@1

建了一个Win32 Dynamic-Link Library工程dllTest,把以上3个文件添加进去后
编译没错,组建出现下面错误:
Linking...
Creating library Debug/dllTest.lib and object Debug/dllTest.exp
dllTest.exp : error LNK2001: unresolved external symbol add@1
Debug/dllTest.dll : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
artman 2008-07-10
  • 打赏
  • 举报
回复
隐式使用DLL需要导入相应DLL的.LIB到工程.
ouyh12345 2008-07-10
  • 打赏
  • 举报
回复
用def文件,就不需要DllAPI修饰了

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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