【问题标题】:linker errors: DLL and inheritance链接器错误:DLL 和继承
【发布时间】:2015-10-07 17:19:52
【问题描述】:

我一直在寻找错误的根源,因为几个小时没有成功。我的项目由两个子项目组成。第一个是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@@ UAXPAVObject@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::`向量删除析构函数'(unsigned int)" (??_ECChild@b@a@@UAEPAXI@Z) Application.obj 应用程序 错误 73 错误 LNK2019:无法解析的外部符号“public: __thiscall a::b::CChild::CChild(class a::b::SomeDifferentObject *)”(??0CChild@b@a@@QAE@PAVSomeDifferentObject@12@ @Z) 在函数 _main Application.obj Application 中引用 错误 77 错误 LNK2001:无法解析的外部符号“受保护:虚拟布尔 __thiscall a::b::CChild::blub1(class Object *,class std::map,class std::allocator > > &)const” (?blub1@ CChild@b@a@@MBE_NPAVObject@23@AAV?$map@KIU?$less@K@std@@V?$allocator@U?$pair@$$CBKI@std@@@2@@std@@ @Z) Application.obj 应用程序 错误 76 错误 LNK2001:未解析的外部符号“受保护:虚拟布尔 __thiscall a::b::CChild::blub2(class Object *,class std::map,class std::allocator > > &)const” (?blub2@ CChild@b@a@@MBE_NPAVObject@23@AAV?$map@KIU?$less@K@std@@V?$allocator@U?$pair@$$CBKI@std@@@2@@std@@ @Z) Application.obj 应用程序 错误 78 错误 LNK2001:未解析的外部符号“受保护:虚拟 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文件?!

非常感谢您的帮助!

亲切的问候, 鲍比

【问题讨论】:

    标签: c++ inheritance virtual dllexport


    【解决方案1】:

    它不起作用,因为总是导出类。它们需要由 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 in PreprocessorDefinitions 以便导出类。这样做的方式是,如果在项目中定义了AB_DLL_EXPORT,那么类将被导出,否则将被导入。

    【讨论】:

    • 感谢您的回复。我已经实施了您的建议,但这并没有导致解决方案:(在查看 MyDll.vcxproj 后我发现,Child.hpp 和 Child.cpp 不包含在 ItemGroup 中。奇怪的是它们被列在 MyDll.vcxproj.filters . 手动添加这两个文件后,链接器错误消失了。看来这是一个VS的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2012-01-15
    • 1970-01-01
    相关资源
    最近更新 更多