0
votes

I am looking for the source of the error since a few hours without success. My project consists of two sub projects. The first one is a dll and the second one is an application (exe). I simplified my original code, which is part of the 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

If I am trying to instantiate a CChild object in my application I get linker errors for all functions of the CChild class:

Error 75 error LNK2001: unresolved external symbol "public: virtual void __thiscall a::b::CChild::blubblub(class a::b::Object *)" (?blubblub@CChild@b@a@@UAEXPAVObject@23@@Z) application.obj Application Error 74 error LNK2019: unresolved external symbol "public: virtual __thiscall a::b::CChild::~CChild(void)" (??1CChild@b@a@@UAE@XZ) referenced in function "public: virtual void * __thiscall a::b::CChild::`vector deleting destructor'(unsigned int)" (??_ECChild@b@a@@UAEPAXI@Z) Application.obj Application Error 73 error LNK2019: unresolved external symbol "public: __thiscall a::b::CChild::CChild(class a::b::SomeDifferentObject *)" (??0CChild@b@a@@QAE@PAVSomeDifferentObject@12@@Z) referenced in function _main Application.obj Application Error 77 error LNK2001: unresolved external symbol "protected: virtual bool __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 Application Error 76 error LNK2001: unresolved external symbol "protected: virtual bool __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 Application Error 78 error LNK2001: unresolved external symbol "protected: virtual void __thiscall a::b::CChild::blub3(class a::b::Object *)const " (?blub3@CChild@b@a@@MBEXPAVObject@23@@Z) Application.obj Application

I am using Visual Studio and all cpp files are in the project (checked many times). Each function is implemented e.g. CChild::CChild(SomeDifferentObject* f_object_p) : CBase(f_object_p) { }

It seems that the relevant cpp files are not found?!

Thank you very much for your help!

Kind regards, Bobby

1

1 Answers

2
votes

It is not working because classes are always exported. They need to be exported by dll project and imported by projects that use that dll.

To fix this, add header file, for example ab_dll.h and there:

#ifdef AB_DLL_EXPORT 
    #define AB_DLL_API __declspec(dllexport)
#else
    #define AB_DLL_API __declspec(dllimport)
#endif

Then use that macro in your classes:

class AB_DLL_API CBase : public CSomeInterface
{
//...
};

class AB_DLL_API CChild : public CBase
{
//...
};

Also in your VS dll project add AB_DLL_EXPORT in PreprocessorDefinitions so that the classes are exported. This works that way that if AB_DLL_EXPORT is defined in the project then classes will be exported, otherwise will be imported.