我有一个项目,我想使用DLL。
我将工厂函数导出到我的exe:
extern "C" __declspec(dllexport)
BaseInit* __cdecl CreateInterface( void )
{
return new Initializer;
}
这非常有效。在我的Init类中,我有一个方法来创建另一个我想在Initializer类中使用的类:
class IAnotherClass {
public:
virtual void TestFunction();
...
class AnotherClass : public IAnotherClass {
public:
void TestFunction();
...
class Initializer : public BaseInit
{
IAnotherClass* Create(void)
{
return new AnotherClass;
}
...
这似乎也有效。我得到一个非NULL指针。但是当试图从这个类(在我的exe程序中)调用TestFunction时,我得到:
LNK2001未解析的外部符号“public:virtual void __cdecl AnotherClass :: TestFunction(void)”(?TestFunction @ AnotherClass @@ UEAAXXZ)
void AnotherClass::TestFunction
-body在我的DLL项目中位于单独的.cpp -file中
我这样做错了,我实际上需要为每个不同的类实例使用单独的工厂函数吗?甚至可以这样做吗?
您需要将__declspec(dllexport)
添加到您希望在dll之外可用的每个类和函数,只要导出包含类,就不需要标记方法。
在类中注意,declspec介于class
和类名之间:
class __declspec(dllexport) Exported
{
};
你还需要一个定义的宏来切换__declspec(dllexport)
和__declspec(dllimport)
之间的标题,具体取决于你是在构建你的dll还是exe,例如:
#ifdef BUILDING_MYDLL
#define MYDLL_EXPORT __declspec(dllexport)
#else
#define MYDLL_EXPORT __declspec(dllimport)
#endif
class MYDLL_EXPORT Exported
{
};