静态变量初始化代码永远不会被调用

发布于 2024-08-15 06:02:26 字数 161 浏览 6 评论 0原文

我有一个使用我制作的静态库的应用程序。库中的一个 .cpp 文件有一个静态变量声明,其构造函数调用单例上的一个函数来执行某些操作,例如添加一个字符串。

现在,当我从应用程序中使用该库时,我的单例似乎不包含任何应该添加的字符串的痕迹。

我肯定错过了一些东西,但我不知道是什么..

I've got an application that's using a static library I made. One .cpp file in the library has a static variable declaration, whose ctor calls a function on a singleton that does something- e.g. adds a string.

Now when I use that library from the application, my singleton doesn't seem to contain any traces of the string that was supposed to be added.

I'm definitely missing something but I don't know what..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-08-22 06:02:26

如果静态库中有一个对象没有在应用程序中明确使用。然后链接器不会将该对象从库中拉到应用程序中。

静态库和动态库有很大的区别。

动态库:
在编译时,不会从动态库中提取任何内容。添加额外的代码以在运行时显式加载和解析符号。在运行时,整个库被加载,因此对象初始值设定项被调用(尽管何时是实现细节)。

静态库的处理方式非常不同:
当您链接静态库时,它会将库中定义的应用程序中未定义的所有项目拉入应用程序。重复此操作,直到库不再有可以解析的依赖项。这样做的副作用是未显式使用的对象/函数不会从库中提取(因此不会直接访问未直接访问的全局变量)。

If you have an object in a static library that is not EXPLICITLY used in the application. Then the linker will not pull that object from the lib into the application.

There is a big difference between static and dynamic libraries.

Dynamic Library:
At compile time nothing is pulled from the dynamic library. Extra code is added to explicitly load and resolve the symbols at run-time. At run time the whole library is loaded and thus object initializers are called (though when is implementation detail).

Static libraries are handled very differently:
When you link against a static library it pulls all the items that are not defined in application that are defined in the library into the application. This is repeated until there are no more dependencies that the library can resolve. The side effect of this is that objects/functions not explicitly used are not pulled form the library (thus global variables that are not directly accessed will not be pulled).

橘虞初梦 2024-08-22 06:02:26

我对此的记忆有点模糊,但您可能会遇到初始化顺序问题。无法保证不同文件中的静态变量初始化程序被调用的顺序,因此,如果在初始化库中的静态变量时您的单例尚未初始化,则可能会产生您所看到的效果。

我解决这些问题的方法是使用某种显式的 init 函数来执行这些操作,并在 main 或其他内容的开头调用该函数。您也许可以调整向编译器(或实际上是链接器)提供目标文件和库参数的顺序,因为这也对我有用,但该解决方案有点脆弱,因为它不仅取决于使用特定的链接器,但也可能是特定的版本。

My memory of this is a bit hazy, but you might be getting hit with an initialization order problem. There are no guarantees in which order static variable initializers in different files get called, so if your singleton isn't initialized yet when your static variable in the library is being initialized, that might produce the effect you're seeing.

The way I've gotten around these problems is to have some sort of an explicit init function that does this stuff and that I call at the start of main or something. You might be able to fiddle with the order in which you give the object file and library arguments to the compiler (or linker, actually) because that's also worked for me, but that solution is a bit fragile because it depends not only on using the specific linker but probably also the specific version.

眼角的笑意。 2024-08-22 06:02:26

重构执行静态初始化的类,以便它们不依赖于任何其他此类类。即让每个类的初始化独立、自足。

Refactor the classes doing static initialization so they do not depend on any other such classes. That is, make each class's initialization independent and self-sufficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文