Dynamic-linked DLL needs to share a global variable with its caller.

Posted by Fabian Wickborn on Stack Overflow See other posts from Stack Overflow or by Fabian Wickborn
Published on 2010-05-12T14:06:15Z Indexed on 2010/05/12 16:24 UTC
Read the original article Hit count: 143

I have a static library libStatic that defines a global variable like this

Header file libStatic/globals.h:

extern int globvar;

Code file libStatic/globals.cpp:

int globvar = 42;

The DLL libDynamic and the executable runner are using this global variable. Furtheron, libDynamic is linked at run-time into runner (via LoadLibrary(), GetProcAddress(), and the works...)

I understand this will lead to globvar being created twice, once in the heap of runner and once in the heap of libDynamic, which is of course very undesirable.

Is there a good away around this? How can I ensure that libDynamic and runner are using the same globvar?

© Stack Overflow or respective owner

Related posts about c++

Related posts about dynamic-linking