隐式调用动态链接库的问题
一、创建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)