静态库中的静态 class 成员变量未共享?
Static class member variable in static library not shared?
在 previous question 中,我描述了一个问题,即 class 的静态成员变量实际上对于不同的其他 class 具有不同的值,包括它们。
经过进一步研究,我发现包含 class 和静态成员变量的翻译单元被编译为静态库(.a 扩展名)。稍后编译和链接的其他翻译单元(我们称它们为插件,我正在一个名为 ADTF 的相当复杂的框架中工作)包括这个库。
我现在的问题是:是否期望 plugin1 和 plugin2 中的 classes 获得它们自己的 myGlobalBool?当我 运行 程序时,从 plugin1 修改 cMyLibraryClass::myGlobalBool 不会更改 plugin2 中的变量。如果是预期的,我需要做什么才能使变量在插件之间共享?请注意,我在 linux 下,关于 SO (here, here) 的一些其他问题似乎指出,对于 Windows .dll,这是预期的,但除此之外对我没有帮助。
我正在做的事情的例子(它比那个更复杂,错误可能在其他地方):
myLibrary.h
cMyLibraryClass
{
cMyLibraryClass();
static bool myGlobalBool;
// Other static variables and stuff
}
myLibrary.cpp
include myLibrary.h
bool cMyLibraryClass::myGlobalBool;
cMyLibraryClass::cMyLibraryClass()
{
// Constructor stuff
}
// Other function implementations of cMyLibraryClass
我最终得到
libMyLibrary.a
插件与以下选项链接:(我删除了路径和包含的所有其他库)
插件 1:
g++ -o plugin1.plb -shared -Wl,-Bsymbolic -Wl,--no-undefined -shared
plugin1.os -lmyLibrary
插件 2:
g++ -o plugin2.plb -shared -Wl,-Bsymbolic -Wl,--no-undefined -shared
plugin2.os -lmyLibrary
我很感激任何关于这里发生的事情的建议,我已经尝试了解了 2 天了。如果您觉得我忘记提供一些基本信息,请询问,我会尽快将其添加到问题中!谢谢:-)
问题:
If it is expected, what would I need to do make the variable shared across plugins?
使 myLibrary
成为动态库 (.so) 而不是静态库 (.a)。
所以,接受的答案是我的问题的解决方案。以防万一有人遇到与 ADTF(汽车数据和时间触发框架)相同的问题,当您知道在哪里查看时,解决此问题的方法实际上非常简单:
在你的扩展的主 SConsript 文件中,最后一行应该是这样的:
Extension(extensionEnv, name, sources=sources, static=True, install_headers=headers)
您只需要将 static 更改为 false。之后,进入 build/[linux, win]/[release, debug]/$yourextensionname 并删除其中的所有内容。同时删除扩展文件夹中所有剩余的 .a 文件,然后从头开始重建。就是这样:-)
在 previous question 中,我描述了一个问题,即 class 的静态成员变量实际上对于不同的其他 class 具有不同的值,包括它们。
经过进一步研究,我发现包含 class 和静态成员变量的翻译单元被编译为静态库(.a 扩展名)。稍后编译和链接的其他翻译单元(我们称它们为插件,我正在一个名为 ADTF 的相当复杂的框架中工作)包括这个库。
我现在的问题是:是否期望 plugin1 和 plugin2 中的 classes 获得它们自己的 myGlobalBool?当我 运行 程序时,从 plugin1 修改 cMyLibraryClass::myGlobalBool 不会更改 plugin2 中的变量。如果是预期的,我需要做什么才能使变量在插件之间共享?请注意,我在 linux 下,关于 SO (here, here) 的一些其他问题似乎指出,对于 Windows .dll,这是预期的,但除此之外对我没有帮助。
我正在做的事情的例子(它比那个更复杂,错误可能在其他地方):
myLibrary.h
cMyLibraryClass
{
cMyLibraryClass();
static bool myGlobalBool;
// Other static variables and stuff
}
myLibrary.cpp
include myLibrary.h
bool cMyLibraryClass::myGlobalBool;
cMyLibraryClass::cMyLibraryClass()
{
// Constructor stuff
}
// Other function implementations of cMyLibraryClass
我最终得到
libMyLibrary.a
插件与以下选项链接:(我删除了路径和包含的所有其他库)
插件 1:
g++ -o plugin1.plb -shared -Wl,-Bsymbolic -Wl,--no-undefined -shared plugin1.os -lmyLibrary
插件 2:
g++ -o plugin2.plb -shared -Wl,-Bsymbolic -Wl,--no-undefined -shared plugin2.os -lmyLibrary
我很感激任何关于这里发生的事情的建议,我已经尝试了解了 2 天了。如果您觉得我忘记提供一些基本信息,请询问,我会尽快将其添加到问题中!谢谢:-)
问题:
If it is expected, what would I need to do make the variable shared across plugins?
使 myLibrary
成为动态库 (.so) 而不是静态库 (.a)。
所以,接受的答案是我的问题的解决方案。以防万一有人遇到与 ADTF(汽车数据和时间触发框架)相同的问题,当您知道在哪里查看时,解决此问题的方法实际上非常简单:
在你的扩展的主 SConsript 文件中,最后一行应该是这样的:
Extension(extensionEnv, name, sources=sources, static=True, install_headers=headers)
您只需要将 static 更改为 false。之后,进入 build/[linux, win]/[release, debug]/$yourextensionname 并删除其中的所有内容。同时删除扩展文件夹中所有剩余的 .a 文件,然后从头开始重建。就是这样:-)