Subj : DLL access to a singleton in Borland BCB6 To : borland.public.cpp.borlandcpp From : rtw1992 Date : Tue Apr 27 2004 08:56 pm I have declared a singleton class in my main exe. Now I want to use a DLL which must also have access to the same singleton object. Problem is that I can't get it to compile, I don't see how to declare the singleton object in the DLL unit. Here's the cut down simplified code for the 2 projects that shows my problem. There are 4 files, unit1.cpp and unit1.h make up the main executable, and DLLUnit1.cpp and DLLUnit1.h make up the DLL. With the way the code is right now, when I build the DLL project I get [Linker Error] Unresolved external 'MySingleton::getInstance()' referenced from C:\DLLUNIT1.OBJ Any suggestions? //--------------------------------------------------- //Unit1.h class MySingleton { public: static MySingleton* getInstance(); void inc(){++count;} long getCount(){return count;} private: MySingleton(){count=0;} static MySingleton* mp_Instance; long count; }; //--------------------------------------------------- //Unit1.cpp #include #include #include "Unit1.h" #define _DLL_PROJECT_ #include "DLLUnit1.h" //This is where the object is declared in the main exe MySingleton* MySingleton::mp_Instance = NULL; MySingleton* MySingleton::getInstance() { if (NULL==mp_Instance) { mp_Instance= new MySingleton; } return mp_Instance; } int main(int argc, char* argv[]) { MyTestDLLFunction(); cout << MySingleton::getInstance()->getCount() << endl; MyTestDLLFunction(); cout << MySingleton::getInstance()->getCount() << endl; MySingleton::getInstance()->inc(); cout << MySingleton::getInstance()->getCount() << endl; return 0; } //--------------------------------------------------- //DLLUnit1.h #ifdef cplusplus extern "C" { #endif #ifdef _DLL_PROJECT_ __declspec(dllexport) void MyTestDLLFunction(); #else __declspec(dllimport) void MyTestDLLFunction(); #endif #ifdef cplusplus } #endif //--------------------------------------------------- //DLLUnit1.cpp #include "vcl.h" #include "unit1.h" #define _DLL_PROJECT_ #include "DLLunit1.h" // How do I declare the following to say that it will be declared // in the emain executable MySingleton* MySingleton::mp_Instance; void MyTestDLLFunction() { MySingleton::getInstance()->inc(); } .