我正在寻找错误的来源,因为几个小时没有成功。我的项目包括两个子项目。第一个是dll,第二个是应用程序(exe)。 我简化了原始代码,这是dll的一部分:
#ifndef blub_base
#define blub_base
#include "SomeInterface.hpp"
#include "Object.hpp"
#include <map>
namespace a
{
namespace b
{
class __declspec(dllexport) CBase : public CSomeInterface
{
protected:
CBase() {}
public:
CBase(SomeDifferentObject* f_object_p) { /* concrete implementation in cpp */ }
~CBase() { /* concrete implementation in cpp */ }
bool initialize() { /* concrete implementation in cpp */ }
bool shutdown() { /* concrete implementation in cpp */ }
void foo() { /* concrete implementation in cpp */ }
virtual void blubblub(Object* f_object_p) { /* concrete implementation in cpp */ }
protected:
bool blub1(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const { /* concrete implementation in cpp */ }
bool blub2(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const { /* concrete implementation in cpp */ }
void blub3(Object* f_object_p) const { /* concrete implementation in cpp */ }
};
}
}
#endif
#ifndef blub
#define blub
#include "Base.hpp"
namespace a
{
namespace b
{
class __declspec(dllexport) CChild : public CBase
{
private:
CChild() {}
public:
CChild(SomeDifferentObject* f_object_p) { /* concrete implementation in cpp */ }
/// deconstructor
~CChild() { /* concrete implementation in cpp */ }
void blubblub(Object* f_object_p) override { /* concrete implementation in cpp */ }
protected:
bool blub1(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const override { /* concrete implementation in cpp */ }
bool blub2(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const override { /* concrete implementation in cpp */ }
void blub3(Object* f_object_p) const override { /* concrete implementation in cpp */ }
};
}
}
#endif
如果我试图在我的应用程序中实例化CChild对象,我会得到CChild类的所有函数的链接器错误:
错误75错误LNK2001:未解析的外部符号“public:virtual void __thiscall a :: b :: CChild :: blubblub(class a :: b :: Object *)”(?blubblub @ CChild @ b @ a @@ UAEXPAVObject @ 23 @@ Z)application.obj应用程序 错误74错误LNK2019:未解析的外部符号“public:virtual __thiscall a :: b :: CChild :: ~CChild(void)”(?? 1CChild @ b @ a @@ UAE @ XZ)在函数“public:virtual void”中引用* __thiscall a :: b :: CChild ::`vector deletion destructor'(unsigned int)“(?? _ ECChild @ b @ a @@ UAEPAXI @ Z)Application.obj Application 错误73错误LNK2019:未解析的外部符号“public:__thiscall a :: b :: CChild :: CChild(class a :: b :: SomeDifferentObject *)”(?? 0CChild @ b @ a @@ QAE @ PAVSomeDifferentObject @ 12 @在函数_main Application.obj Application中引用了@Z) 错误77错误LNK2001:未解析的外部符号“protected:virtual bool __thiscall a :: b :: CChild :: blub1(class Object *,class std :: map,class std :: allocator&gt;&gt;&amp;)const”( ?blub1 @ CChild @ b @ @@一个@ MBE_NPAVObject 23 @ AAV?$地图@ KIU?$ @少沃顿STD @@ V'$分配器@ U&$ @配对$$ CBKI @ STD @@@ 2 @@ std @@@ Z)Application.obj应用程序 错误76错误LNK2001:未解析的外部符号“protected:virtual bool __thiscall a :: b :: CChild :: blub2(class Object *,class std :: map,class std :: allocator&gt;&gt;&amp;)const”( ?blub2 @ CChild @ b @ @@一个@ MBE_NPAVObject 23 @ AAV?$地图@ KIU?$ @少沃顿STD @@ V'$分配器@ U&$ @配对$$ CBKI @ STD @@@ 2 @@ std @@@ Z)Application.obj应用程序 错误78错误LNK2001:未解析的外部符号“protected:virtual void __thiscall a :: b :: CChild :: blub3(class a :: b :: Object *)const”(?blub3 @ CChild @ b @ a @@ MBEXPAVObject @ 23 @@ Z)Application.obj申请
我正在使用Visual Studio,所有cpp文件都在项目中(多次检查)。例如,实现每个功能。 CChild :: CChild(SomeDifferentObject * f_object_p):CBase(f_object_p) { }
似乎找不到相关的cpp文件?!
非常感谢你的帮助!
亲切的问候, 波比
答案 0 :(得分:2)
它无法正常工作,因为始终会导出类。它们需要由dll项目导出,并由使用该DLL的项目导入。
要解决此问题,请添加头文件,例如ab_dll.h,然后添加:
#ifdef AB_DLL_EXPORT
#define AB_DLL_API __declspec(dllexport)
#else
#define AB_DLL_API __declspec(dllimport)
#endif
然后在你的课程中使用该宏:
class AB_DLL_API CBase : public CSomeInterface
{
//...
};
class AB_DLL_API CChild : public CBase
{
//...
};
同样在您的VS dll项目中,在AB_DLL_EXPORT
中添加PreprocessorDefinitions
,以便导出类。这样做的方式是,如果在项目中定义了AB_DLL_EXPORT
,那么将导出类,否则将导入类。