让 Loki Singleton 在 VS 2008 C++ 中的 DLL 中工作
我很确定这个问题并不新鲜,而且很难解决。 希望我对后者的看法是错误的。
我正在尝试在我的程序中使用来自 Modern C++ Design 的 Loki::Singleton 。
但是,我似乎无法让它跨 DLL 工作。 我想我知道为什么会发生这种情况:模板化代码在每个源模块中实例化,因此每个模块都有自己的全局变量,而不是一个全局变量。
显然,这使得 Singleton 变得非常非单一。
有什么办法可以绕过这种行为吗?
I'm pretty sure this problem isn't new, and pretty sure it's hard to solve. Hopefully I'm wrong about the latter.
I'm trying to use the Loki::Singleton from Modern C++ Design in a program of mine.
However, I can't seem to get it to work across DLLs. I think I know why this is happening: the templated code gets instantiated in every source module, so instead of there being one global variable, each module has its own.
Obviously, this makes the Singleton very much non-single.
Is there any way to get around this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 Loki 源目录中看到它们有一个特定的 SingletonDLL 目录 正在测试中,看起来他们使用导出的、显式实例化的模板(可以工作)。 希望其中包含您想要的代码。
I see in the Loki source directory that they have a specific SingletonDLL directory under test, looks like they use an exported, explicitly instantiated template (which would work). Hopefully that contains the code you want.
请注意,这不会解决问题。 显式实例化和导出的单例应该可以解决问题...
-Rick
查看 #pragma data_seg 这里基本上,您需要在代码的共享部分中声明单例的实例。 默认情况下,静态数据的作用域为 dll。
使用模板可能会很棘手,但这是这里的成功之路,不涉及传递/复制静态数据。
Note this is not going to address the question. An explicitly instantiated and exported singleton should do the trick...
-Rick
Check out #pragma data_seg here basically, you need to declare an instance of the singleton in a shared section of your code. By default statics are scoped to the dll.
It may get tricky with templates, but this is the path to success here that doesn't involve passing / copying static data around.
您可能是正确的,每个 DLL 都有自己的单例实例。 我不太熟悉 Loki 实现和 源代码 弄清楚并不是很有趣。
可能的解决方案是:
You are probably correct that each DLL has its own instance of the singleton. I'm not that familiar with the Loki implementation and the source code isn't a lot of fun to figure out.
Possible solutions are: