本文介绍了如何在C ++中实现过程全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在.cpp文件中使用变量会导致变量全局可用,如下所示:

Normally using a variable in a .cpp file results in the variable being globally available, like this:

.h file:
extern int myGlobal;
void work();

.cpp file:
int myGlobal = 42;
void work(){ myGlobal++; }

当.cpp文件放在一个静态库和多个共享库或对静态库的可执行链接,每个都有自己的 myGlobal 副本。工作()将修改自己的版本的变量。

When the .cpp file is put in a static library and more than one shared library (DLL) or executable links against the static library, each one has its own copy of myGlobal. work() would modify its own version of the variable.

我的问题现在:有一种方法来获得一个过程范围的唯一变量或指向该变量的指针?类似什么线程本地存储将是线程范围的变量。它不必是平台独立的。如果它在Win32中工作的话,则为奖励点)

My question now: is there a way to get a process-wide unique variable or pointer to that variable? Similar what thread-local storage would be for thread-wide variables. It doesn't have to be platform independent. Bonus points if it works in Win32 :)

推荐答案

简单:使进程中的所有DLL链接到暴露变量。

Simple: make all the DLLs in the process link to a single DLL that exposes the variable.

这篇关于如何在C ++中实现过程全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:37