I have run into a similar problem before, although then I had defined the registering code in a separate LIB project. It turns out that the linker optimized away my objects! My solution was to reference these "registration objects" in a function. I then called the function from my application. Maybe something like that is happening here? Try turning off whole program optimization and see what happens. You might have to do something to keep the optimizer from treating this objects as dead code. (I wish Visual C++ had attributes for marking code as non-dead...)
Update:
Short of finding a clean way of marking objects as non dead, I had to touch the code in both projects. In the LIB project, I defined functions rather than global objects. In the app project, I defined functions that called these functions.
What you can do is this:
// Registrations.cpp
#ifdef EXPORT_REGISTRATIONS
#define REGISTRATION_CODE(theClass) \
void register_##theClass##_function() { \
// Code for registering your class \
}
#else
#define REGISTRATION_CODE(theClass) \
// Declare a function prototype for the function in LIB \
void register_##theClass##_function(); \
struct theClass##importer { \
theClass##importer() { \
// Call into LIB \
register_##theClass##_function(); \
} \
} g_##theClass##importerInstance; \
#endif
REGISTRATION_CODE(MyClass);
REGISTRATION_CODE(MyOtherClass);
Then in the LIB project, make sure EXPORT_REGISTRATIONS
is defined. This will generate the functions that perform the actual registration that you intend to do. In the app project, make sure EXPORT_REGISTRATIONS
isn't defined. #include "<path to lib project>\Registrations.cpp"
in the app project. This will generate global objects (like your original ones) that call into the functions defined in the LIB project.
This is how I solved my problem. Your mileage may vary. :)