• Advertisement
Sign in to follow this  

Exporting public static data members VC++

This topic is 4652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Hello Kippesoep!

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

Thanks for the help.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Negative! That didn't work. And now I confirm that this happens on both release and debug builds.

Any other ideas are welcome...

William

Share this post


Link to post
Share on other sites
Have you made sure the library's lib file is actually linked into your client project?

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement