Static data in DLL

My problem is very similar to this one : the class in the DLL has a static member... In this case, the static member is of type QString (type QT) and provides a name for the class. I give the normal export at the class level: __declspec(dllexport)

.

When I link a DLL to my class with another project and try to compile it, I get an "unresolved external symbol" error for static data. I checked two things:

  • Dumpbin definitely tells the static data member to be exported by the compiled DLL.
  • In fact, the static data member doesn't seem to be used in the application that is reporting the error.

HEADER file (.h) in DLL:

class __declspec(dllexport) MyClass {
public: 
   virtual ~MyClass();
   static const QString JUST_A_NAME;    
};

      

FEATURES (.cpp) file in DLL:

#include "MyClass.h"

MyClass::~MyClass() { }
const QString MyClass::JUST_A_NAME("call_me_al");

      

Contrary to the already mentioned post , I have avoided methods that need to be inline like. the implementation is clearly not included in the header. The QString type (see line 83 ff.) Contains several inline strings. Could this cause an error?

EDIT: I added an import statement to the header of my application. It is located before it turns on. HEADER file (.h) in APPLICATION:

class __declspec(dllimport) MyClass {
public: 
   virtual ~MyClass();
   static const QString JUST_A_NAME;    
};

      

The error remains unchanged:

error LNK2001: non resolved external symbol ""public: static class QString const MyClass::JUST_A_NAME" (?JUST_A_NAME@MyClass@@2VQString@@B)". <name of .obj file from application>

      

+3


source to share


1 answer


In your application header file, you need to do two things.

  • When exporting, declare the definition __declspec(dllexport)

  • When importing, declare the definition __declspec(dllimport)

You obviously cannot do this at the same time.

What you need to do is define a macro like this:

#ifdef __COMPILING_MYLIB
#define MYLIBAPI __declspec(dllimport)
#else
#define MYLIBAPI __declspec(dllexport)
#endif

      



Then declare your export like this:

// mylib.h
class MYLIBAPI MyClass {
public: 
   virtual ~MyClass();
   static const QString JUST_A_NAME;    
};

      

Then when you compile MYLIB, you pass it to the -D__COMPLING_MYLIB

compiler which runs #if

above.

So, when compiling the library itself, the header file declares things as an export, but when compiling things that will use the library, they are declared as an import.

+9


source







All Articles