静态类成员变量在静态库中不共享? [英] Static class member variable in static library not shared?

查看:390
本文介绍了静态类成员变量在静态库中不共享?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一个问题中,我描述了一个问题,即静态成员变量类确实对不同的其他类包括它们有不同的值。

In a previous question, I described a problem that static member variables of a class did actually have different values for different other classes including them.

进一步研究发现,包含静态成员变量的类的翻译单元被编译为静态库(.a扩展名)。其他翻译单元(让我们称之为插件,我在一个名为ADTF的相当复杂的框架中工作)以后编译和链接包括这个库。

Upon further research, I found out that the translation unit containing the class with the static member variable gets compiled to a static library (.a extension). Other translation units (lets call them plugins, I am working in a rather complex framework named ADTF) that get compiled and linked later on include this library.

我的问题是现在:是否期望plugin1和plugin2中的类获得自己的myGlobalBool?当我运行程序时,从plugin1修改cMyLibraryClass :: myGlobalBool不会改变plugin2中的变量。如果预期,我需要做什么使变量共享跨插件?请注意,我在linux下,有关SO的其他问题(此处这里)似乎指出,对于Windows .dlls ,这是预期的,但对其他方面对我没有帮助。

My question is now: Is it expected that classes in plugin1 and plugin2 get their own myGlobalBool? When I run the program, modifying cMyLibraryClass::myGlobalBool from plugin1 does not change the variable in plugin2. If it is expected, what would I need to do make the variable shared across plugins? Note that I am under linux, some other questions on SO (here, here) seem to point out that for Windows .dlls, this is expected, but otherwise were not helpful for me.

我在做什么的例子(它比这更复杂,错误可能在别的地方) :

Example of what I'm doing (it is more complex than that and the error might be somewhere else):

myLibrary.h

myLibrary.h

cMyLibraryClass
{
    cMyLibraryClass();
    static bool myGlobalBool;
    // Other static variables and stuff
}

myLibrary.cpp



myLibrary.cpp

include myLibrary.h

bool cMyLibraryClass::myGlobalBool;

cMyLibraryClass::cMyLibraryClass()
{
    // Constructor stuff
}

// Other function implementations of cMyLibraryClass

我最终以


libMyLibrary.a

libMyLibrary.a

插件与以下选项链接:(我删除了路径和所有其他库)

The plugins get linked withthe following options: (I removed the paths and all the other libraries that get included)

插件1:


g ++ -o plugin1.plb -shared -Wl ,-Bsymbolic -Wl, - no-undefined -shared
plugin1.os -lmyLibrary

g++ -o plugin1.plb -shared -Wl,-Bsymbolic -Wl,--no-undefined -shared plugin1.os -lmyLibrary

插件2: p>

Plugin 2:


g ++ -o plugin2.plb -shared -Wl,-Bsymbolic -Wl, - no-undefined -shared
plugin2.os -lmyLibrary

g++ -o plugin2.plb -shared -Wl,-Bsymbolic -Wl,--no-undefined -shared plugin2.os -lmyLibrary

我很感激任何关于这里发生的建议,我已经试图了解2天了。如果你觉得我忘了提供一些基本的信息,只是问,我会尽快把它添加到问题,尽快!感谢: - )

I appreciate any suggestions on what is happening here, I've been trying to understand for 2 days now. If you feel like I forgot to provide some essential information, just ask and I'll try to add it to the question as soon as possible! Thanks :-)

推荐答案

问题:


如果预期,我需要做什么使变量共享跨插件?

If it is expected, what would I need to do make the variable shared across plugins?

使用 myLibrary 动态库(.a)。

这篇关于静态类成员变量在静态库中不共享?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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