ctor init not calling the global ctor instances in library
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 applica开发者_运维问答tion 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 removes never 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]):
It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.
(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.
精彩评论