Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DLL export and inheritance in C++

I'm trying to export a class and its base class from a DLL like this:

#ifdef MY_EXPORTS
    #define DECLSPEC_TEST __declspec(dllexport)
#else
    #define DECLSPEC_TEST __declspec(dllimport)
#endif


class DECLSPEC_TEST BaseClass
{
  // stuff.
};

class DECLSPEC_TEST DerivedClass : public BaseClass
{
  // This class only has a constructor which initializes the class differently.

};

But I try to use this class in another DLL, I keep getting an error:

error LNK2019: unresolved external symbol 
"__declspec(dllimport) public: __thiscall DerivedClass::DerivedClass(void)"
 (__imp_??0DerivedClass@@QAE@XZ) referenced in function 
"public: __thiscall SomeOtherClass::SomeOtherClass(void)" (??0SomeOtherClass@@QAE@XZ)  

I also looked at my exporting DLL with PE Explorer and I can't see the derived class in the exports list.

When I try to use the base class in my other DLL it works fine.

What am I doing wrong?

like image 977
Idov Avatar asked Jul 09 '11 07:07

Idov


2 Answers

there are two ways to load the DLL. The first is to reference one or more symbols from the DLL (your classname, for example), supply an appropriate import .LIB and let the linker figure everything out.

The second is to explicitly load the DLL via LoadLibrary.

Either approach works fine for C-level function exports. You can either let the linker handle it or call GetProcAddress as you noted.

But when it comes to exported classes, typically only the first approach is used, i.e., implicitly link to the DLL.

like image 142
kunal Avatar answered Sep 28 '22 00:09

kunal


Ok, I don't know how to explain this but I put the implementation of the derived class' constructor in a CPP file instead of within the class definition and the error went away...
thanks everybody :)

like image 23
Idov Avatar answered Sep 28 '22 00:09

Idov