static class instance in static lib
Hi all,
I'm trying to solve the following problem:
I've compiled a static library which contains classes for all kinds of services.
Some of these service need initialisation which I'd like to be performed automatically.
For this purpose I have added a class to
my library which does these initialisations
in its ctor. In the implementation file of
this class a create a single static instance of it.
I've added the library to one of my projects. To my surprise, the initialisation does not take place since the static instance of my initialiser class
is not being created.
Is that normal behaviour for static objects in libraries which are not being referenced from anywhere in the code ?
Best regards,
Matthias.
forcing the linker to add all static libraries code
It's probably not relevant anymore to those who were originally in this "conversation" but for other people (like me) who search this forum for answers:
The problem is of course with all linkers (at least the one I work with - WIN32 - Visual .NET, Unix & Linux - GNU) when using static libraries to create executables or shared libraries (dll/.so).
The linker is not using object files from these static libraries if there is no reference to symbols which are found in these files. Therefore, if a function/global variable is declared, and implemented in a static library, but never used in the objects (which are not found in the static libraries) that forms the executable/shared library (output file), it will not appear in the output linker file.
This is of course a good thing, for most cases, cause it reduces dramatically the size of the output file of the linker.
However, sometimes, like in the cases describe above, we would like to "force" the linker to add this code (I needed it to implement "reflection" for classes by their name). In this case I only found the solution in the Unix/Linux platforms (gcc 3.2) - u can simply use the "-z -allextract" before specifing the static libraries which u need to "force" all its content to be used. Later on u can use "-z -defaultextract" to return the linker to its normal behavior for the following libraries (p.s. when calling the compiler u need to add -XLinker before each of the flags so these options will be sent to the linker - "-XLinker -z -XLinker -allextract").
For some reason I failed to find the same solution in the Visual .NET linker.