Exporting public static data members VC++

Started by
8 comments, last by mallouk 18 years, 10 months ago
Hello everyone! I'm having a little problem exporting a static class data members on my dll. For example: ---- Color.h ---- namespace NameSpace { class __declspec(dllexport) Color { public: const static Color BLACK; }; } ---- Color.cpp ---- namespace NameSpace { const Color Color::BLACK(0.0f, 0.0f, 0.0f); } The problem is, when try to link this dll with any program I get an error looks like this: File.obj : error LNK2001: unresolved external symbol "public: static class NameSpace::Color const NameSpace::Color::BLACK" (?BLACK@Color@NameSpace@@2V12@B) I look at the msdn documentation and according to what's written there, the thing should work. I found no other clue. I even looked at other people's code and there's nothing different. Did anybody have the same problem? For a while I'm using #defines as a workaround... Thanks! William
Advertisement
Do you get this with a Debug build, too, or only with a Release build?
Kippesoep
Hello Kippesoep!

I have changed the code, but I'm almost sure that I got this error with both builds.

Thanks for the help.
Just to clarify: sometimes a static data member does not get exported properly when it is not being referenced by any code in the exporting module. Debug builds do not have this optimisation enabled.
Kippesoep
Thanks for the info! I will soon try that.

-William
Negative! That didn't work. And now I confirm that this happens on both release and debug builds.

Any other ideas are welcome...

William
Have you made sure the library's lib file is actually linked into your client project?
Kippesoep
Yes, it is linked. Thanks for the persistence! :)
Something odd is going on, then, because I tried with the code you posted and it works just fine. Try downloading this tool and load your .lib file into that. See if it really exports BLACK. In the "Undecorated" column, it should say public: static class NameSpace::Color const NameSpace::Color::BLACK and ?BLACK@Color@NameSpace@@2V12@B in the "Decorated" column.

If it's not there, your .lib file is incorrect. Try doing a "Rebuild all".
Kippesoep
Hello Kippesoep!!

I tried to use the program, but there was an error when I tried to load the lib. I tried to rebuild all several times before! Hey, there's really something odd going on. :) No problem, I don't want to bother you anymore with this issue. I have already solved it by replacing the static members with macros, and it's working fine for now. I'll get back to it in the near future..

I'm certain that this is one of these tiny problems which are strangely hard to find :) I have tried to change all compiler and linker options: I even compared these options with those of a library that has static members and works! Maybe should just create a new project and import the source files into it.

All right man... Thanks a lot!

Cheers!
William

This topic is closed to new replies.

Advertisement