我想知道如何在不同的程序模块之间共享一些内存 - 比方说,我有一个主应用程序(exe),然后是一些模块(dll).它们都链接到同一个静态库.这个静态库将有一些管理器,它提供各种服务.我想要实现的是在所有应用程序模块之间共享此管理器,并在库初始化期间透明地执行此操作.在进程之间我可以使用共享内存,但我希望这只在当前进程中共享.你能想到一些跨平台的方式吗?可能使用boost库,如果它们提供了一些设施来执行此操作.
我现在只能想到的解决方案是使用相应操作系统的共享库,所有其他模块将在运行时链接到该库,并将管理器保存在那里.
编辑:澄清我实际需要的东西:
这是.h:
class Logger
{
private:
static int mTresholdSeverity;
public:
static __declspec(dllexport) void log(const char* message);
static __declspec(dllexport) void logFormat(const char* format, ...);
static __declspec(dllexport) int getTresholdSeverity() { return mTresholdSeverity; }
static __declspec(dllexport) void setTresholdSeverity(int tresholdSeverity) { mTresholdSeverity = tresholdSeverity; }
};
Run Code Online (Sandbox Code Playgroud)
和.cpp:
#include "Logger.h"
#include <cstdarg>
int Logger::mTresholdSeverity = 200;
void Logger::log(const char* message)
{
//...
}
void Logger::logFormat(const char* format, ...)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
错误 LNK2001:无法解析的外部符号“私有:静态 int TransformationViewer_Utility_Logging::Logger::mTresholdSeverity”(?mTresholdSeverity@Logger@TransformationViewer_Utility_Logging@@0HA) ...
显然,mTresholdSeverity被初始化了。如果我注释掉 getTresholdSeverity() 和 setTresholdSeverity() 或者将它们的定义移动到 .cpp 文件中,该错误就会被删除。
当头文件中定义的静态方法(getTresholdSeverity() 或 …
我需要帮助访问跨 DLL/主程序的全局函数。我有一个班级基地
基数
#ifdef MAIN_DLL
#define DECLSPEC __declspec(dllexport)
#else
#define DECLSPEC __declspec(dllimport)
#endif
class Base {
private:
DECLSPEC static Filesystem * filesystem;
DECLSPEC static Logger * logger;
DECLSPEC static System * system;
public:
static void setFilesystem(Filesystem * filesystem_);
static void setApplication(Application * application_);
static void setLogger(Logger * logger_);
static void setSystem(System * system_);
static Filesystem * fs() { return filesystem; }
static Logger * log() { return logger; }
static System * sys() { return system; }
};
Run Code Online (Sandbox Code Playgroud)
main.cpp(主应用程序)(此处预定义了 MAIN_DLL) …