I have a configuration that has a library and a console application. They are pretty barebones at the moment. I am using Visual Studio 2010 and the library and console application are both statically linked to the Runtime. The console application also links to the library.
In the library, I can add this code in a source file:
class MyClass
{
public:
MyClass()
{
printf("MyClass loaded\n");
}
};
class MyClass2
{
public:
static MyClass my_class;
};
MyClass MyClass2::my_class;
Now, my understanding is that my_class should be initialized at some point before main(). However, it never happens (as I do not get the printed message).
I can, however, get it to initialize using two different methods:
- Put the code in the console application instead. Doing that will invoke the printf() statement for certain.
- Modify MyClass2 to include a static function that is called from a global variable in the library and use that global variable in main() in the console application.
Example for #2 above:
Library file:
class MyClass
{
public:
MyClass()
{
printf("MyClass loaded\n");
}
};
class MyClass2
{
public:
static MyClass my_class;
static int Ping();
};
MyClass MyClass2::my_class;
int my_global = MyClass2::Ping();
Console application file:
extern int my_global;
int main()
{
printif("%d", my_global);
}
Is Windows trying to help me by delay loading the linked in library's static variables?? Or is there some compiler setting I have set? This behavior was totally unexpected by me.