ctor init 不调用库中的全局 ctor 实例 [英] ctor init not calling the global ctor instances in library

查看:30
本文介绍了ctor init 不调用库中的全局 ctor 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SourceryGpp lite for arm 开发应用程序和库.

我没有使用标准库或默认启动文件.因此,要调用我正在执行的全局 ctrs 以下代码:

ldr r0,=__ctors_init__ldr r0,[r0]mov lr,pc比 r0

所以问题是我在静态库中定义了一些全局实例,但是上面的代码永远不会调用它们的构造函数.奇怪的是应用程序的全局ctors调用成功了,有谁知道为什么?

解决方案

这是静态库和具有运行时初始化的全局变量的一个众所周知的问题.

大多数链接器将只包含实现主程序依赖所需的静态库组件.如果编译单元中没有任何对象被使用,链接器删除 永远不会添加编译单元作为一个整体,并且不会发生全局初始化的副作用.>

有一个很好的解释 href="http://www.hofollow"google.com/group/comp.lang.c++/tree/browse_frm/thread/f00286e2a1302530/11b0813b69f018a0?rnum=71&_done=%2Fgroup%2Fcomp.lang.c%2B%2B%2Fbrowse_frm%2Fthread%2Ff00286e2a1302530%2F6465945cc56cdf66%3Ftvc%3D1%26#doc_aabc09f9eb7a2f27" rel="nofollow">这里)

使用标准库提供的启动代码也会遇到同样的问题.

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 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.

这篇关于ctor init 不调用库中的全局 ctor 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆