Skip to main content
GameDev.net gamedev.net
🔒 Locked

variable initialization across static libraries

Started by ph33r Dec 7, 2006 at 1:30 PM 4 replies 6.6k views
Original Post
ph33r
ph33r
I have created a system that automatically registers variables into a factory based on the constructor of an instance. Unfortunately, as soon as I tried putting a global variable in a static library the main executable no longer recieves a registeration from that global variable. If I put the global variable in the main executable it will register itself in the factory. Is there something I'm missing?
StratBoy61
StratBoy61
Hello ph33r,

It is difficult to answer your question without seeing your code. Anyway, you might want to double-check your code, keeping in mind that each .obj file that you will generate will hold its global variables.
So for instance, if you have a global variable in a .h file that is used twice during compilation (and thus generates 2 .obj files), you will have two instances of this global variable (not good) in your program.

You might have the same kind of problem with your static library, as a static library is mainly an .obj file...
My two cents
StratBoy61
JohnBolton
JohnBolton
Just a guess... Perhaps the variable tries to register itself before the factory is constructed. The order in which modules are initialized (w.r.t. their "global" variables) is not something you can control or depend on (except for language extensions provided by some compilers).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
lexchou
lexchou
Why not use Singleton pattern to solve this problem?
Veni Vidi Vici
mattnewport
mattnewport
Are you using Visual C++? This is a common problem when trying to auto-register things using static variables. The linker strips out code that it thinks is unreferenced when linking a library and it doesn't realise that the static variable's constructor has side effects that are desired - it sees that the variable is not accessed and so strips it out, along with the constructor call. This doesn't happen when the object files are linked into the main executable. I think the only way to get around it is to trigger the initialization with explicit calls, either by using the Singleton pattern or by having an Init function for each module that is called from the main executable.
hplus0603
hplus0603
The rule is that your static/global objects will be initialized if and only if the linker can prove that the execution flow will reach the translation unit (C++ file) that the static object is found within. Further, that static object will be initialized before the execution hits any line of code in that translation unit, but not necessarily before main()/WinMain() is started. That's just the language, and it's a really annoying part of the language.

The common solutions are:
1) force a reference to the global variable, through some linker option or definition file
2) force a reference to the global variable, through explicitly taking the address of it in the main program and making sure that reference doesn't get optimized out
3) force a reference to a function in the file, through calling it from some reachable code (this function can be a "dummy" function)

Last, there are some slightly special rules about template global instances, which can at times be subverted to do your bidding, when regular static/global instances can't.
enum Bool { True, False, FileNotFound };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.