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++.
Thanks,
how to force global variable which define in a static library to initialize?
They are initialized when your program initially executes.
L. Spiro
The global variable defined in static library will not be initialized, Because I do not reference the global variable explicitly in the executable program.
In face, I just want to implement a function which execute a section of code before enter main() function, but the code in static library.
Dynamic crate of MFC just used global object initialization feature, but that not work when class define in static library.
why not define it as a static in the header and then initialise in the source file using namespace::variable = value syntax?
If you reference some function in same file as your global variable, then the variable will be initialized.
Yes, actually. we can initialize the global object defined in *.h file of static project through include *.h file in executable project.
But..... Can we link some *.h file of static library into executable project through link option? not through reference the object or function.
Can we work that out by link option? I am using Visual C++ 2008.
cheers.
But..... Can we link some *.h file of static library into executable project through link option? not through reference the object or function.
Can we work that out by link option? I am using Visual C++ 2008.
cheers.
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++.
This means that it's bad and wrong to rely on unreferenced globals in your static library to actually be constructed/linked...
To reliably force your global to be linked on any C++ compiler (i.e to do it the standard way), you've actually got to use the global inside your program, e.g.
//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();
}
}
One of my "favorite" issues that caused a lot of pain.
With VC++ you usually get away with actually referencing the variable somewhere (a dummy read in a destructor or whatever). GCC will actually delay it until the very first time the variable is actually accessed. So you can not rely on any side effects caused by the initialization to actually happen before main.
In short, trying something like this will usually not work:
For VC++, you might get away with a simple reference somewhere
For GCC we actually had to create static global objects that would have the desired side effect in the constructor. And based on all the weird rules about how a compiler may optimize, I wouldn't rely on even this to work for absolutely every compiler.
By the way, this method still failed on another project, where this code was in a static lib that was used to build a dll. Somewhere along the way the linker still decided to drop it. They moved the entire code to be a direct part of the dll.
I played around and tested a few methods, which ended up in a somewhat convoluted test:
http://festini.devic...factorytest.zip
But I guess the easiest solution is to simply not rely on any side effects of global static variable initialization.
With VC++ you usually get away with actually referencing the variable somewhere (a dummy read in a destructor or whatever). GCC will actually delay it until the very first time the variable is actually accessed. So you can not rely on any side effects caused by the initialization to actually happen before main.
In short, trying something like this will usually not work:
bool classXisRegistered = registerClassX();
For VC++, you might get away with a simple reference somewhere
SomeClass::~SomeClass
{
bool dummy = classXisRegistered;
}
For GCC we actually had to create static global objects that would have the desired side effect in the constructor. And based on all the weird rules about how a compiler may optimize, I wouldn't rely on even this to work for absolutely every compiler.
template
class Registrator
{
public:
Registrator() { registerClass(); }
};
static const Registrator registerClassX;
By the way, this method still failed on another project, where this code was in a static lib that was used to build a dll. Somewhere along the way the linker still decided to drop it. They moved the entire code to be a direct part of the dll.
I played around and tested a few methods, which ended up in a somewhat convoluted test:
http://festini.devic...factorytest.zip
But I guess the easiest solution is to simply not rely on any side effects of global static variable initialization.
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.
I approve your opinion,
now I
find
the most suitable way is that include *.h file of static library in executable project explicitly.
Thank you Trienco for your offer of examples.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement