相关疑难解决方法(0)

在模块之间共享内存

我想知道如何在不同的程序模块之间共享一些内存 - 比方说,我有一个主应用程序(exe),然后是一些模块(dll).它们都链接到同一个静态库.这个静态库将有一些管理器,它提供各种服务.我想要实现的是在所有应用程序模块之间共享此管理器,并在库初始化期间透明地执行此操作.在进程之间我可以使用共享内存,但我希望这只在当前进程中共享.你能想到一些跨平台的方式吗?可能使用boost库,如果它们提供了一些设施来执行此操作.

我现在只能想到的解决方案是使用相应操作系统的共享库,所有其他模块将在运行时链接到该库,并将管理器保存在那里.

编辑:澄清我实际需要的东西:

  1. 我需要找出,如果已经创建了共享管理器(下面的答案已经提供了一些方法)
  2. 获取指向管理器的指针(如果存在),或将指针设置在新创建的管理器对象的某处.

c++ memory module

9
推荐指数
1
解决办法
1万
查看次数

未解析的外部符号静态变量(标头中定义的方法使用的变量)

这是.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() 或 …

c++ linker

4
推荐指数
1
解决办法
4503
查看次数

跨dll使用静态类变量/函数

我需要帮助访问跨 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) …

c++ dll static shared-libraries

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×3

dll ×1

linker ×1

memory ×1

module ×1

shared-libraries ×1

static ×1