Sign in to follow this  
zgysx

how to force global variable which define in a static library to initialize?

Recommended Posts

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, [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
[quote name='YogurtEmperor' timestamp='1333433289' post='4927791']
They are initialized when your program initially executes.


L. Spiro
[/quote]
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='zgysx' timestamp='1333425474' post='4927766']
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++.[/quote]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).
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]i.e to do it the standard way[/i]), you've actually got to [i]use[/i] the global inside your program, e.g.
[code]//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();
}
}[/code]

Share this post


Link to post
Share on other sites
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:
[code]
bool classXisRegistered = registerClassX();
[/code]

For VC++, you might get away with a simple reference somewhere
[code]
SomeClass::~SomeClass
{
bool dummy = classXisRegistered;
}
[/code]

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.

[code]
template
class Registrator
{
public:
Registrator() { registerClass(); }
};

static const Registrator registerClassX;
[/code]

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:
[url="http://festini.device-zero.de/factorytest.zip"]http://festini.devic...factorytest.zip[/url]

But I guess the easiest solution is to simply not rely on any side effects of global static variable initialization.

Share this post


Link to post
Share on other sites
[quote name='Trienco' timestamp='1333519518' post='4928117']
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.
[/quote]
I approve your opinion, [size=4]now I[/size][size=4] find[/size][size=4] the most suitable way is that include *.h file of static library in executable project explicitly.[/size]
[size=4]Thank you Trienco[/size] for your offer of examples.[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this