构造函数初始化不调用库中的全球构造函数实例 [英] ctor init not calling the global ctor instances in library

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

问题描述

我正在开发使用SourceryGpp精简版的手臂应用程序和库。

I'm developing an application and a library using SourceryGpp lite for arm.

我不使用标准库或默认的启动文件。
于是打电话给全球点击率我做给下面code:

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

所以,问题是,我在定义静态库的一些全局实例,但它们的构建函数决不会被上面的code调用。奇怪的是,该应用程序的全局构建函数成功调用,任何人都知道这是为什么?

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.

有一个很好的解释<一个href=\"http://groups.google.com/group/comp.lang.c++/tree/browse_frm/thread/f00286e2a1302530/f8b3a645bd19a7fe?rnum=31&_done=%2Fgroup%2Fcomp.lang.c%2B%2B%2Fbrowse_frm%2Fthread%2Ff00286e2a1302530%2F6465945cc56cdf66%3Ftvc%3D1%26#doc_6c873869afac6b66\"相对=nofollow>这里(最终摘要<一个href=\"http://groups.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\"相对=nofollow>这里)

There's a good explanation here (final summary here)

您将有标准库提供的启动code同样的问题。

You would have the same problem with the standard library-provided startup code.

这篇关于构造函数初始化不调用库中的全球构造函数实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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