Jump to content
  • Advertisement
Sign in to follow this  
jjd

Problem w/ static const member variable in DLL

This topic is 3776 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

I'm sure there is a simple solution for this problem but I've had trouble finding something on the web. I'm getting linker errors when I try to use a static member variable from a class exported to a DLL. For example,
// foo.h
class MY_API Foo
{
public:
    void moofa() {}

public:
    static const int bar;
};

// foo.cpp
const int Foo::bar = 0;


All is good if I create an instance and call moofa(), but if I try to reference Foo::bar I get a linking error (unable to find symbol). What do I need to do to export this static member variable correctly? [edit] If anyone knows a good source of info relating to DLLs, I'd really like to know (NB: that excludes MSDN)

Share this post


Link to post
Share on other sites
Advertisement
Well with static constants that are of integral type, you can initialize them directly in the class declaration. Eg:

// foo.h
class MY_API Foo
{
public:
void moofa() {}

public:
static const int bar = 0;
};


Then you do not need to define it in any cpp file.

I don't know if that will fix your linking problem though, but since the definition has moved from the source to the header, it might.

Share this post


Link to post
Share on other sites
Thanks for the help, but I think I've found the problem. The problem was in the way I was declaring the API macro. I was using

// export.h
#ifdef _USRDLL
#define MY_API_EXPORT __declspec(dllexport)
#define MY_API_IMPORT __declspec(dllimport)
#ifdef MYDLL_EXPORTS
#define MY_API MY_API_EXPORT
#else
#define MY_API MY_API_IMPORT
#endif
#else
#define MY_API
#define MY_API_EXPORT
#define MY_API_IMPORT
#endif

// foo.h
class MY_API Foo
{
};


Because I want to be able to create either a dynamic or static library. I wasn't defining _USRDLL in the project that imported the DLL. When I do that, it works. Why this was only a problem for the static member variable, I don't know. The member functions seemed to work fine.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!