Suppose I have a X.DLL with the following in a header file:
extern __declspec(dllexport) int foobar;
This foobar global variable is then defined inside X.DLL in an implementation file:
int foobar = 1;
Now suppose that I have two DLLs, A.DLL and B.DLL, which are linked to X.DLL, and an application E.EXE which uses A.DLL and B.DLL:
+----> A.DLL ----+
| |
E.EXE ----+ +----> X.DLL
| |
+----> B.DLL ----+
In this situation, is the expression &foobar (address of foobar) guaranteed to have the same value when evaluated inside A.DLL and B.DLL? In other words, are X.DLL‘s global variables stored twice or is there only one instance of foobar in the entire process? Also, does it make a difference if the DLLs are linked at load time or through a call to LoadLibrary?
I’m also confused by this statement in GCC’s documentation for dllimport:
One drawback to using this attribute is that a pointer to a variable marked as
dllimportcannot be used as a constant address.
I don’t quite understand the implications of that statement considering my aforementioned situation.
There is only one copy. However, in general, doing this is an incredibly bad idea and you will run into a lot of problems.