Jump to content
  • Advertisement
Sign in to follow this  
zgysx

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

This topic is 2294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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, smile.png

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
If you reference some function in same file as your global variable, then the variable will be initialized.

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

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++.
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.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();
}
}

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:

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.

Share this post


Link to post
Share on other sites

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.biggrin.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!