Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 zgysx   Members   -  Reputation: 109

Like
0Likes
Like

Posted 02 April 2012 - 09:57 PM

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, Posted Image

Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 12889

Like
1Likes
Like

Posted 03 April 2012 - 12:08 AM

They are initialized when your program initially executes.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 zgysx   Members   -  Reputation: 109

Like
0Likes
Like

Posted 03 April 2012 - 07:00 PM

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.

#4 bollµ   Members   -  Reputation: 354

Like
1Likes
Like

Posted 03 April 2012 - 08:35 PM

why not define it as a static in the header and then initialise in the source file using namespace::variable = value syntax?

a WIP 2d game engine: https://code.google.com/p/modulusengine/

English is not my first language, so do feel free to correct me :)


#5 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
1Likes
Like

Posted 03 April 2012 - 09:24 PM

If you reference some function in same file as your global variable, then the variable will be initialized.

#6 zgysx   Members   -  Reputation: 109

Like
0Likes
Like

Posted 03 April 2012 - 11:26 PM

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.

#7 Hodgman   Moderators   -  Reputation: 28595

Like
0Likes
Like

Posted 03 April 2012 - 11:29 PM

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


#8 Trienco   Crossbones+   -  Reputation: 2086

Like
1Likes
Like

Posted 04 April 2012 - 12:05 AM

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.
f@dzhttp://festini.device-zero.de

#9 zgysx   Members   -  Reputation: 109

Like
0Likes
Like

Posted 04 April 2012 - 08:07 PM

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.Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS