views:

286

answers:

3

I've got an application that's using a static library I made. One .cpp file in the library has a static variable declaration, whose ctor calls a function on a singleton that does something- e.g. adds a string.

Now when I use that library from the application, my singleton doesn't seem to contain any traces of the string that was supposed to be added.

I'm definitely missing something but I don't know what..

Thanks, B.

+1  A: 

My memory of this is a bit hazy, but you might be getting hit with an initialization order problem. There are no guarantees in which order static variable initializers in different files get called, so if your singleton isn't initialized yet when your static variable in the library is being initialized, that might produce the effect you're seeing.

The way I've gotten around these problems is to have some sort of an explicit init function that does this stuff and that I call at the start of main or something. You might be able to fiddle with the order in which you give the object file and library arguments to the compiler (or linker, actually) because that's also worked for me, but that solution is a bit fragile because it depends not only on using the specific linker but probably also the specific version.

jk
Thanks for your quick answer. It's definitely no order problem as I've already sorted that out. The init is not convenient as I want to declare an arbitrary number of such variables in an arbitrary number of cpp files, and in the runtime process the *generated* list.
B.
A: 

Refactor the classes doing static initialization so they do not depend on any other such classes. That is, make each class's initialization independent and self-sufficient.

wallyk
The point is, the static variables only exist to register (in their ctor) a static function -defined in a cpp file- to a list, so that I can choose one of them later in the runtime. So there is no concept of independece in this context - I think.
B.
+3  A: 

If you have an object in a static library that is not EXPLICITLY used in the application. Then the linker will not pull that object from the lib into the application.

There is a big difference between static and dynamic libraries.

Dynamic Library:
At compile time nothing is pulled from the dynamic library. Extra code is added to explicitly load and resolve the symbols at run-time. At run time the whole library is loaded and thus object initializers are called (though when is implementation detail).

Static libraries are handled very differently:
When you link against a static library it pulls all the items that are not defined in application that are defined in the library into the application. This is repeated until there are no more dependencies that the library can resolve. The side effect of this is that objects/functions not explicitly used are not pulled form the library (thus global variables that are not directly accessed will not be pulled).

Martin York
You nailed it, thanks. Indeed it wasn't explicitly used, but I *thought* that it will keep the code as it was in debug mode.
B.