My knowledge is a bit fuzzy in terms of how linking a DLL works but I'm observing a change to a static member variable in an executable that doesn't change the same static member variable in a DLL. Here's the scenario I have:
main.cpp is statically linked to mylib.lib. Within mylib.lib, I have the following class:
// foo.h
class Foo
{
public:
static int m_global;
Foo();
~Foo();
};
and
// foo.cpp
#include "foo.h"
int Foo::m_global = 5;
I also have a DLL that links to mylib.lib with the following:
//testdll.h
#define MATHLIBRARY_API __declspec(dllimport)
void MATHLIBRARY_API printFoo();
and
// testdll.cpp
#include "testdll.h"
#include <iostream>
void printFoo() {
std::cout << Foo::m_global << std::endl;
}
Finally, in main.cpp of my executable
// main.cpp
#include <iostream>
#include "testdll.h"
#include "foo.h"
int main() {
std::cout << Foo::m_global << std::endl;
Foo::m_global = 7;
std::cout << Foo::m_global << std::endl;
printMutiply();
return 0;
}
My expected output is 5, 7, 7. However, I'm seeing 5, 7, 5 which is telling me that the static member variable change isn't being seen by the DLL. Why is this so? And how can I make the DLL see changes in the static member variable made in the executable??