I have this library which involves some static initialization code which needs to run before main()
. It all works well if you just compile all of the translation units together, but it doesn't work if I provide a static library (.a
file) and have users link their application against it - the linker simply ignores the symbols which do my static initialization.
Alternative,y I can make the linker pick up everything in the static library, if I specify the -Wl,--whole-archive
option to GCC, i.e. specify the --whole-archive
option to GNU link.
But is there some middle ground? Can I mark some symbols and make the linker always pick them up for the executable, while the rest of the symbols are added only when needed?
Motivation: I use some static blocks to register classes in a factory; I want to make my code available as a (non-dynamic) library, without the user code having to perform any "magic incantation" for the factory to be populated.
Some related questions:
You can force the linker to keep a given function (and naturally, all code that is called from this function). Add
-u my_function
to the link command. Many build systems let static libraries to 'export' the build settings to those who use them. E.g., for Android ndk-build framework, you can specify something likein your module Android.mk. People reuse it by adding to their Android.mk the simple statement
N.B. For this approach to work,
my_function()
cannot be declaredstatic
. If some symbol is declared as static at the file scope, then the linker guess not know it by name at all. Luckily, if it is referenced in some code that the linker decides to keep, then it will also not get stripped off. Furthermore, unless you make a special effort, the linker will strip or keep whole compilation units (a.k.a. C files). Thus, it is usually enough to "anchor" a dummy function to keep many functions and data.