From: MFDonadeli on 7 Jan 2010 13:35 Every function in this class works ok with project dependencies. In this case I can access the Singleton if I remove the variable static CMySingleton* singleton and put on the Instance function like this: static CMySingleton* Instance() { static CMySingleton* singleton; if(!singleton) singleton = new CMySingleton(); return singleton; } but the problem in this case, is that this function create another instance of CMySingleton, even if static singleton var is already initialized.
From: Stephen Myers on 7 Jan 2010 13:52 Ajay Kalra wrote: > On Jan 7, 1:05 pm, MFDonadeli <mfdonad...(a)gmail.com> wrote: >> On 7 jan, 16:01, Ajay Kalra <ajayka...(a)yahoo.com> wrote: >> >> >> >>> On Jan 7, 12:54 pm, MFDonadeli <mfdonad...(a)gmail.com> wrote: >>>> Hi. >>>> I declare a singleton on a MFC extension DLL, like this: >>>> <pre> >>>> //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; >>>> </pre> >>>> Code 2: >>>> <pre> >>>> CMySingleton *a; >>>> a = CMySingleton::Instance(); >>>> </pre> >>>> 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: >>>> <pre> >>>> unresolved external symbol "private: static class CMySingleton* >>>> CMySingleton::singleton" (?singleton(a)CMySingleton@@0PAV1@A) >>>> </pre> >>>> Any idea? >>> Are you sure you are including the correct .lib file in MFC extesion >>> DLL where you code2 sits? >>> -- >>> Ajay >> Yes, via Project Dependencies on VS 2008... > > As Joe already pointed out, it doesnt mean much in your case. All you > are ensuring is the build order as such. You need to somehow link to > the library you are trying to use. > > How did this work for the Regular DLL for you? You would have the same > problem there as well. > > -- > Ajay I've got a large solution where project dependencies are added as linker inputs. This may vary based on project types, where regular DLL's are added and MFX Extensions are not. (I don't have any experience with MFC extensions.) Check the Porject Properties/Linker/Command Line to see if your library shows up. If not add the .lib as a linker input. Steve
From: Stephen Myers on 7 Jan 2010 13:55 Ajay Kalra wrote: > On Jan 7, 1:05 pm, MFDonadeli <mfdonad...(a)gmail.com> wrote: >> On 7 jan, 16:01, Ajay Kalra <ajayka...(a)yahoo.com> wrote: >> >> >> >>> On Jan 7, 12:54 pm, MFDonadeli <mfdonad...(a)gmail.com> wrote: >>>> Hi. >>>> I declare a singleton on a MFC extension DLL, like this: >>>> <pre> >>>> //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; >>>> </pre> >>>> Code 2: >>>> <pre> >>>> CMySingleton *a; >>>> a = CMySingleton::Instance(); >>>> </pre> >>>> 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: >>>> <pre> >>>> unresolved external symbol "private: static class CMySingleton* >>>> CMySingleton::singleton" (?singleton(a)CMySingleton@@0PAV1@A) >>>> </pre> >>>> Any idea? >>> Are you sure you are including the correct .lib file in MFC extesion >>> DLL where you code2 sits? >>> -- >>> Ajay >> Yes, via Project Dependencies on VS 2008... > > As Joe already pointed out, it doesnt mean much in your case. All you > are ensuring is the build order as such. You need to somehow link to > the library you are trying to use. > > How did this work for the Regular DLL for you? You would have the same > problem there as well. > > -- > Ajay I've got a large solution where project dependencies are added to the linker command line. This may vary based on project types, where regular DLL's are added and MFX Extensions are not. (I don't have any experience with MFC extensions.) Check the Project Properties/Linker/Command Line to see if your library shows up. In any case add the .lib as an explicit linker input. Steve
From: MFDonadeli on 7 Jan 2010 14:01 With project dependencies, I can access all the classes and functions on this DLL, and in the build line command the lib apper at the end of the command. And I think that if project dependencies is wrong the solution that I describe, wouldn't compile: In this case I can access the Singleton if I remove the variable static CMySingleton* singleton and put on the Instance function like this: static CMySingleton* Instance() { static CMySingleton* singleton; if(!singleton) singleton = new CMySingleton(); return singleton; } but the problem in this case, is that this function create another instance of CMySingleton, even if static singleton var is already initialized.
From: Ajay Kalra on 7 Jan 2010 16:13
On Jan 7, 1:35 pm, MFDonadeli <mfdonad...(a)gmail.com> wrote: > Every function in this class works ok with project dependencies. > > In this case I can access the Singleton if I remove the variable > static CMySingleton* singleton and put on the Instance function like > this: > > static CMySingleton* Instance() > { > static CMySingleton* singleton; > if(!singleton) > singleton = new CMySingleton(); > return singleton; > } > > but the problem in this case, is that this function create another > instance of CMySingleton, even if static singleton var is already > initialized. You are including somewhere the headerfile which defines this class. Thats why CMySingleton* works at compile time. At link time, it needs to go and look for it and if it doesnt find it, you will get the linker error. Bottom Line: You need the lib in which its defined/declared. -- Ajay |