I have not reference that global variable explicitly in main() project.
I expect that implement reflect function in C++.
Thanks,
Posted 03 April 2012 - 12:08 AM
Posted 03 April 2012 - 07:00 PM
The global variable defined in static library will not be initialized, Because I do not reference the global variable explicitly in the executable program.They are initialized when your program initially executes.
L. Spiro
Posted 03 April 2012 - 08:35 PM
a WIP 2d game engine: https://code.google.com/p/modulusengine/
English is not my first language, so do feel free to correct me ![]()
Posted 03 April 2012 - 11:26 PM
Posted 03 April 2012 - 11:29 PM
There's no official way to do this according to the standard -- it depends on implementation-defined behaviour (i.e. every compiler is allowed to do it differently).how to force global variable which define in a static library to initialize?
I have not reference that global variable explicitly in main() project.
I expect that implement reflect function in C++.
//static_library.cpp
Reflection g_myGlobal( "foo", "bar" );
Reflection** GetReflectionTable()
{
static Reflection* table = { &g_myGlobal, NULL };
return table;
}
//main.cpp
int main()
{
// this forces g_myGlobal to be linked, because it's actually used by the program now.
for( Reflection** staticLibReflection = GetReflectionTable(); *staticLibReflection, ++staticLibReflection )
{
const Reflection& r = **staticLibReflection;
r.foo();
}
}
Posted 04 April 2012 - 12:05 AM
bool classXisRegistered = registerClassX();
SomeClass::~SomeClass
{
bool dummy = classXisRegistered;
}
template
class Registrator
{
public:
Registrator() { registerClass(); }
};
static const Registrator registerClassX;
Posted 04 April 2012 - 08:07 PM
I approve your opinion, now I find the most suitable way is that include *.h file of static library in executable project explicitly.One of my "favorite" issues that caused a lot of pain.
.....
But I guess the easiest solution is to simply not rely on any side effects of global static variable initialization.