I declare a singleton on a MFC extension DLL, like this:
//header file: SingleTon.h class AFX_EXT_CLASS CMySingleton { public: static CMySingleton* Instance() { if(!singleton) singleton = new CMySingleton(); return singleton; } int a; // Other non-static member functions private: CMySingleton() {}; // Private constructor CMySingleton(const CMySingleton&); // Prevent copy-construction CMySingleton& operator=(const CMySingleton&); // Prevent assignment virtual ~CMySingleton() {}; static CMySingleton* singleton; }; And in a cpp file I code the following line: CMySingleton* CMySingleton::singleton = NULL;
Code 2:
CMySingleton *a; a = CMySingleton::Instance();
The problem is when I code "code 2" in a Regular Dll, all works fine, but when I code "code 2" in another MFC extension DLL gives an error:
unresolved external symbol "private: static class CMySingleton* CMySingleton::singleton" (?singleton@CMySingleton@@0PAV1@A)
I check correctly all the dependencies, via Project Dependencies.
Any idea?
Best Answer
The problem is in the AFX_EXT_CLASS macro.
Extension dll defines _AFXEXT and your class is exported, and main app (or a regular dll) doesn't define it so it's imported. But your second extension dll also defines _AFXEXT and your class declaration uses dllimport instead of dllexport and you get a linker error. The solution is to create your own macro for both dlls and use them instead of AFX_EXT_CLASS:
Create EXTENSION_TWO and EXT_CLASS_TWO for your second dll. Define EXTENSION_ONE only in your first extension dll project, and EXTENSION_TWO only in your second extension dll project.