2

Following situation with Visual Studio: Since I have merged two former programs (just copied the files together and removed the main functions) into a DLL to use now from another program, I have a very strange effect:

The same static(!) variable is not actually the same in different files. I can write to it in one file and print it correctly but it will have still its init value when printed from a different file. It seems like somehow I have two separate .. memory ranges?.. inside the same DLL although all "extern C" functions are callable as supposed to. I do not spawn processes or stuff like that.

Question, how can I go about debugging that? How does visual studio select the CPPs to compile and link to a DLL?

Huge thanks in advance!

DragonGamer
  • 834
  • 3
  • 9
  • 27
  • 1
    static variables that are not declared within a class (global) are always different between different source files. DLL or EXE, doesn't matter. With that in mind, could you show the code where the variable is declared, defined, and referenced. Otherwise, it's guesswork to figure out what's wrong. – selbie Mar 26 '20 at 00:09
  • @selbie they are inside a namespace. Thought name spaces are there for exactly this reason. It has to be specifically in a class to be same over all source files? – DragonGamer Mar 26 '20 at 00:22
  • 1
    namespaces exist so that common variable names don't collide at compile and link time. static variables in a namespace get the same link treatment as static variables in the global namespace. – selbie Mar 26 '20 at 00:31
  • In other words, move your static variable from namespace to a class and pick one source file to define the instance of that variable and it should work. – selbie Mar 26 '20 at 00:32
  • @selbie Right, right! That worked. Gosh, why am I encountering this now for the first time after nearly two years of C++? Will keep that in mind from now on. Makes sense too; I had a misconception of namespaces then. Thank you! – DragonGamer Mar 26 '20 at 00:37

0 Answers0