I'm developing an application and a library using SourceryGpp lite for arm.
I'm not using standard libs or default start files. So to call the global ctrs i'm doing to following code:
ldr r0,=__ctors_init__
ldr r0,[r0]
mov lr,pc
bx r0
So the problem is that I'm defining some global instances in the static library, but the their ctors are never called by the above code. The weird thing is that the global ctors of the application are successfully called, anyone knows why?
This is a well known problem with static libraries and global variables with runtime initialization.
Most linkers will only include components of the static library that are needed to fulfill a dependency of the main program. If none of the objects in the compilation unit are used, the linker
removesnever adds the compilation unit as a whole, and side effects of global initialization do not occur.There's a good explanation here (final summary here)
You would have the same problem with the standard library-provided startup code.
The standard explicitly permits to defer static objects initialization (C++98, [basic.start.init]):
(latest C++0x draft has a bit different wording.)
So if you don't use some translation unit at-all, all the objects defined there may be removed completely.