问题描述
我正在使用 SourceryGpp lite for arm 开发应用程序和库.
我没有使用标准库或默认启动文件.因此,要调用我正在执行的全局 ctrs 以下代码:
ldr r0,=__ctors_init__ldr r0,[r0]mov lr,pc比 r0
所以问题是我在静态库中定义了一些全局实例,但是上面的代码永远不会调用它们的构造函数.奇怪的是应用程序的全局ctors调用成功了,有谁知道为什么?
这是静态库和具有运行时初始化的全局变量的一个众所周知的问题.
大多数链接器将只包含实现主程序依赖所需的静态库组件.如果编译单元中没有任何对象被使用,链接器 永远不会添加编译单元作为一个整体,并且不会发生全局初始化的副作用.>
使用标准库提供的启动代码也会遇到同样的问题.
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 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.
这篇关于ctor init 不调用库中的全局 ctor 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!