xprogrammer

Hello,
I have a class that is exported from a DLL and has a static member:
class __declspec( dllexport ) MyClass
{
public:
MyClass();
static int CONSTANT;
};


The implementation of the class is in a separate cpp file:

#include "MyClass.h"

int MyClass::CONSTANT = 0;

MyClass::MyClass() { }

I try to use the class and the exported static member from a function in another project:

#include "MyClass.h"
int main()
{
MyClass a;
int i = MyClass::CONSTANT;
}


But when I try to build my solution I get a link error:
main.obj : error LNK2001: unresolved external symbol "public: static int MyClass::CONSTANT" ( CONSTANT@MyClass@@2HA)

I can use other members or methods in the class. Is there something special I need to be able to link the static members I checked the lib with dumpbin and I see the static member in the list of exported items.

Thanks.




Re: Visual C++ General Exporting static class members

Holger Grund

I'm almost certain that the compiler would emit and reference to __imp_ CONSTANT@MyClass@@2HA and the linker should consequently complain about a missing __declspec(dllimport) ...::CONSTANT".

So I supect you forgot to __declspec(dllimport) the class. Instance data members are obviously not exported, that's only a compiler layout thingie. Not sure though, why the default constructor would not result in a linker error while the constant (unless your example is not complete)

-hg





Re: Visual C++ General Exporting static class members

Aleksandr Tokarev

Try make a deffinition for your API, viz

in stdafx.h of the Dll you export from:

#define MYAPI __declspec( dllexport )

in MyClass.h or in common header for your API.

#ifndef MYAPI

#define MYAPI __declspec( dllimport )

#endif

and change class declaration

class MYAPI MyClass
{
public:
MyClass();
static int CONSTANT;
};


The implementation of the class is not changed:

#include "MyClass.h"

int MyClass::CONSTANT = 0;

MyClass::MyClass() { }

Use the class and the exported static member from a function in another project is not changed:

#include "MyClass.h"
int main()
{
MyClass a;
int i = MyClass::CONSTANT;
}






Re: Visual C++ General Exporting static class members

xprogrammer

Okay, there is one error in my code since I include the __declspec( dllexport ) in my header file and that header is included in both projects. So maybe both are trying to export the class. I made the changes suggested in Aleksandr's post, but I still get the same error.

The code posted in the original post is all the code I have in my example solution.

Holger: What did you mean by "Instance data members are obviously not exported"






Re: Visual C++ General Exporting static class members

Aleksandr Tokarev

#define MYAPI __declspec( dllexport )

need to be included only in one project. NOT int the both.

You have to define #define MYAPI __declspec( dllexport ) in stdafx.h of the DLL Projects which EXPORTS your class. And ONLY in that project.

INSERT

#ifndef MYAPI

#define MYAPI __declspec( dllimport )

#endif

before the class in the same header.




Re: Visual C++ General Exporting static class members

xprogrammer

Sorry, my bad. I had another error in my code. It does work as you presented.

Question:

If I leave the __dllspec( dllimport ) out, other members or methods link fine. Why do I have state __dllspec( dllimport ) to use the static members of the class, but not other members






Re: Visual C++ General Exporting static class members

Holger Grund

xprogrammer wrote:

Holger: What did you mean by "Instance data members are obviously not exported"

Nonstatic data members don't have relevant linkage. Runtime DLL linking basically resolves a named symbol to a fixed address. Nonstatic data members do not have a fixed address. The compiler translates member accesses to memory accesses with a given navigation scheme starting (usually just an offset) from the value of the this pointer.

I.e. the compiler does all the magic with nonstatic data members. The static linker and the OS program loader are not involved.

-hg





Re: Visual C++ General Exporting static class members

Aleksandr Tokarev

xprogrammer wrote:

Question:

If I leave the __dllspec( dllimport ) out, other members or methods link fine. Why do I have state __dllspec( dllimport ) to use the static members of the class, but not other members

I don't know why, even don't want know. I know only that it is not good idea to use dllexport without dllimport.