运行时动态加载和单例
Runtime Dynamic Loading and Singletons
我有一个使用单例的程序。该程序在运行时加载共享对象库。该库还利用同一单身人士。问题是,当从库中访问单例时,创建了一个新实例。
该程序与-rdynamic
链接,我都使用-fPIC
,而加载则是这样的:
std::shared_ptr<Module> createModuleObject(const std::string& filename)
{
if (!fs::exists(filename))
throw std::runtime_error("Library not found: " + std::string(filename));
struct export_vtable* imports;
void *handle = dlopen(filename.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if (handle) {
imports = static_cast<export_vtable*>(dlsym(handle, "exports"));
if (imports)
return std::shared_ptr<Module>(imports->make());
else
throw std::runtime_error("Error trying to find exported function in library!");
} else
throw std::runtime_error("Error trying to load library: " + std::string(filename));
}
图书馆导出了这样的类:
Module* make_instance()
{
return new HelloWorld();
}
struct export_vtable
{
Module* (*make)(void);
};
struct export_vtable exports = { make_instance };
,该课程利用了Singleton。
这就是创建单例的方式(Configuration.cpp
):
std::unique_ptr<Configuration> Configuration::instance_(nullptr);
std::once_flag Configuration::onlyOnceFlag_;
Configuration& Configuration::instance()
{
if (instance_ == nullptr)
{
std::cout << "INSTANCE IS NULL, CREATING NEW ONE" << std::endl;
std::call_once(Configuration::onlyOnceFlag_,
[] {
Configuration::instance_.reset(new Configuration());
});
}
return *Configuration::instance_;
}
针对Configuration.cpp
的程序和库链接均链接。如果我从库中省略了这一点,则在尝试访问Singleton时会收到一个未定义的符号错误。
有人想知道如何解决这个问题吗?非常感谢!
这是我为https://github.com/kvahed/codeare/blob/master/src/src/core/core/reconstrategy.hpp求解。加载共享对象后,我将Global Singleton Workspace
的实例分配给DLL中的加载类。https://github.com/kvahed/codeare/tree/master/master/src/modules中的所有类均来自ReconStrategy
和共享对象。好消息是该代码是便携的。
构造这样的重新构造时,会发生这种情况:
ReconContext::ReconContext (const char* name) {
m_dlib = LoadModule ((char*)name);
if (m_dlib) {
create_t* create = (create_t*) GetFunction (m_dlib, (char*)"create");
m_strategy = create();
m_strategy->Name (name);
m_strategy->WSpace (&Workspace::Instance());
} else {
m_strategy = 0;
}
}
}
这里的关键行是m_strategy->WSpace (&Workspace::Instance());
相关文章:
- std::原子加载和存储都需要吗
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 为什么加载SDF会导致Mobilizer创建闭环错误
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- 如何使用tinyxml2从XML加载父实体和子实体
- 如何在C++中使用pybind11加载一个pickle python列表
- 为什么在单例中,检查类==空?
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- C++单例,不会为此文档加载任何符号
- 如何从QtQuickWidget加载qt快速UI表单
- 如何将项目共享"system calls"作为单例对象构建和链接到引导加载程序?
- 使用 C++ 和嵌入式单声道调用 C# DLL 时无法加载程序集系统
- 运行时动态加载和单例
- C++懒惰的单例挂在加载时
- 生成表单和加载时代码中出现Lambda错误
- 当我在win32项目c++应用程序中单击菜单项时,如何编写代码来加载一个特殊的对话框?
- 带有jQuery的CGI后表单未完全加载
- 如何将位图从文件加载到样例缓冲区
- 在运行时单击QTabWidget将内容加载到选项卡中