我在一个dll里面有一个类,我想在另一个项目中使用它。我读this tutorial有关如何做到这一点,我的伪代码看起来像这样使用dll里面的类
interface.h
#ifdef EXPORT
#define SOMEAPI __declspec(dllexport)
#else
#define SOMEAPI __declspec(dllimport)
#endif
struct ISomeInterface
{
virtual void SomeMethod()=0;
};
typedef ISomeInterface* SOMEHANDLE;
#define EXTERN_C extern "C"
EXTERN_C SOMEAPI SOMEHANDLE WINAPI CreateSomething(VOID);
然后,我有SomeDLL.dll
它实现ISomeInterface
和CreateSomething
。
当我尝试在我的客户端使用这个时,我得到链接错误。客户端是这样的:
Client.cpp
#include "interface.h"
SOMEHANDLE h = ::CreateSomething(); // Linker error here: Unresolved external
我可以解决这个通过添加DLL项目作为客户端项目的VC的依赖性++。那么一切都很好。
问题是如果我想使用这个独立的dll(现在就是这种情况)?那么我该如何摆脱链接器错误?
您可能需要提供一个静态库,允许在运行时加载DLL并为客户端封装接口。看看[这里](http://stackoverflow.com/questions/3360828/plugin-pattern-with-dll-how-can-i-extract-plugin-interface-from-dll) –