Global Objects Constructor
-
Hi, I 'm experiencing the following problem with global objects. In a program with global objects at the beginning the constructors of these objects are executed and then the program enters in the main function. Everything its ok. Now I doing the following:
//-- foo.cpp #include "foo.h" ///< include my header file MyClass obj; ///< Global object its constructor should run before main //-- MyClass member functions follow MyClass::MyClass(void) { ... } ...
//-- main.cpp int main(int argc, char* argv[]) { //code here ... }
If the following code is compiled as one project everything is fine. Now the trick is that I compile foo.cpp as a static library (foo.lib) to which my main program links. Running step by step with the debugger (VS .NET 7.1) I found out that main is called first. Actually placing a breakpoint in my object's constructor I noticed that the constructor is never executed. Why does that happens? If I compile foo.cpp as a dynamic library (dll) will I have the same problem? Thank you, Themis -
Hi, I 'm experiencing the following problem with global objects. In a program with global objects at the beginning the constructors of these objects are executed and then the program enters in the main function. Everything its ok. Now I doing the following:
//-- foo.cpp #include "foo.h" ///< include my header file MyClass obj; ///< Global object its constructor should run before main //-- MyClass member functions follow MyClass::MyClass(void) { ... } ...
//-- main.cpp int main(int argc, char* argv[]) { //code here ... }
If the following code is compiled as one project everything is fine. Now the trick is that I compile foo.cpp as a static library (foo.lib) to which my main program links. Running step by step with the debugger (VS .NET 7.1) I found out that main is called first. Actually placing a breakpoint in my object's constructor I noticed that the constructor is never executed. Why does that happens? If I compile foo.cpp as a dynamic library (dll) will I have the same problem? Thank you, Themisif you examine 'obj' once the program gets into main, can you tell if the ctor has been called ? i'm wondering if the ctor happens before the debugger can see... i do global vars in static libs all the time, and don't have any problems. Cleek | Image Toolkits | Thumbnail maker
-
if you examine 'obj' once the program gets into main, can you tell if the ctor has been called ? i'm wondering if the ctor happens before the debugger can see... i do global vars in static libs all the time, and don't have any problems. Cleek | Image Toolkits | Thumbnail maker
If you set a breakpoint at the constructor the debugger will see it. If I use the global var in my main program then the constructor is called. However what I want is not to use this object. I dont even want to know its existance out of my .lib, I just want to execute sth just before main begins. Themis
-
Hi, I 'm experiencing the following problem with global objects. In a program with global objects at the beginning the constructors of these objects are executed and then the program enters in the main function. Everything its ok. Now I doing the following:
//-- foo.cpp #include "foo.h" ///< include my header file MyClass obj; ///< Global object its constructor should run before main //-- MyClass member functions follow MyClass::MyClass(void) { ... } ...
//-- main.cpp int main(int argc, char* argv[]) { //code here ... }
If the following code is compiled as one project everything is fine. Now the trick is that I compile foo.cpp as a static library (foo.lib) to which my main program links. Running step by step with the debugger (VS .NET 7.1) I found out that main is called first. Actually placing a breakpoint in my object's constructor I noticed that the constructor is never executed. Why does that happens? If I compile foo.cpp as a dynamic library (dll) will I have the same problem? Thank you, Themis -
Hi, I 'm experiencing the following problem with global objects. In a program with global objects at the beginning the constructors of these objects are executed and then the program enters in the main function. Everything its ok. Now I doing the following:
//-- foo.cpp #include "foo.h" ///< include my header file MyClass obj; ///< Global object its constructor should run before main //-- MyClass member functions follow MyClass::MyClass(void) { ... } ...
//-- main.cpp int main(int argc, char* argv[]) { //code here ... }
If the following code is compiled as one project everything is fine. Now the trick is that I compile foo.cpp as a static library (foo.lib) to which my main program links. Running step by step with the debugger (VS .NET 7.1) I found out that main is called first. Actually placing a breakpoint in my object's constructor I noticed that the constructor is never executed. Why does that happens? If I compile foo.cpp as a dynamic library (dll) will I have the same problem? Thank you, ThemisIf the
MyClass
ctor/dtor have no code, and no other code referencesobj
, then the linker will discard the global object because it's not being used. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Wizard needs food, badly!