Sign in to follow this  
mallouk

Exporting public static data members VC++

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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this