468,512 Members | 1,459 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Putting a singleton in a DLL

I'm getting errors about a singleton when I put it into a dll. The dll compiles without any problems. However, when I try to link it to another dll I get the following error.

libFoo-Bar error LNK2001: unresolved external symbol "private: static class MSGTMS:ObjectManager * MSGTMS::ObjectPostManager::m_instance" (?m_instance@ObjectManager@MSGTMS@@0PAV12@A)

The Singleton class looks like this
Expand|Select|Wrap|Line Numbers
  1. #define LIBMSGTM_Managers_EXPORT __declspec (dllexport)
  3. namespace MSGTMS
  4. {
  6. class LIBMSGTM_Managers_EXPORT ObjectManager
  7. {
  8. public:
  9.     static ObjectManager* instance()
  10.     {
  11.         if (m_instance==0)
  12.             m_instance=new ObjectManager;
  13.         return m_instance;
  14.     }
  16.     void destroy()
  17.     {
  18.         if (m_instance!=0)
  19.             delete m_instance;
  20.     }
  22.     bool updateFPStatus(const unsigned int FPindex, FencePostStatus& fpStatus);
  23.     bool updateFP(const unsigned int FPindex, FencePost& fp);
  25.     bool isFPActive(const unsigned int FP);
  26.     bool getFP(const unsigned int FPindex, FencePost& fp);
  27.     bool getUpdatedStatus(bool* arrayEightFencePosts);
  29.     bool getUpdatedPosition(bool* arrayEightFencePosts);
  31.     bool deleteFP(const unsigned int FPindex);
  33. protected:
  34.     ObjectManager();
  35.     ~ObjectManager();
  36.     Object m_Object[8];
  39. private:
  40.     static ObjectManager* m_instance;
  41.     CRITICAL_SECTION cs;  //For multithreading protection
  43. };
  44. }//namespace MSGTMS
  46. Implementation
  48. namespace MSGTMS
  49. {
  50. ObjectManager* ObjectManager::m_instance=0;
  52. //Constructor Destructor and other things implemented
  54. }

Should I be doing something else to a static variable in a dll?
Jan 26 '08 #1
1 4129
9,207 Expert Mod 8TB
static ObjectManager* m_instance;
You are not exporting the m_instance.

The error is that m_instance should not be a member of this singleton class.

Instead the m_instance should be in an anonymous namespace accessible onbly by the Instance() function. You export the Instance function().

There is an article in the C++ HowTos forum on singleton classes that shows the correct code setup.

BTW: m_ does not mean a member variable. It just identifies that the name of the variable starts with m_. Avoid naming conventions that lead to assumptions.
Jan 27 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Tim Clacy | last post: by
10 posts views Thread by E. Robert Tisdale | last post: by
1 post views Thread by Jim Strathmeyer | last post: by
3 posts views Thread by Alicia Roberts | last post: by
7 posts views Thread by Ethan | last post: by
3 posts views Thread by Harry | last post: by
5 posts views Thread by Pelle Beckman | last post: by
3 posts views Thread by stevewilliams2004 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.