2

In a header file, I have a namespace that stores some extern pointers to use as global variables. This header file is then included in a few .cpp files that use its implementation. When I compile, I get the error for each global:

LNK2005 "void * Globals::gHandle" already defined in Engine.obj 

How the variables are written in the header:

namespace Globals
{
    extern HANDLE gHandle = nullptr;
    extern void* processBaseAddress = nullptr;
    extern void* smBaseAddress = nullptr;
}

The header file is using #ifndef so I shouldn't be included more than once. Some functions either get or set the values of these global variables.

w0f
  • 908
  • 9
  • 23
  • 3
    Look [here](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c) - basically declaration should be in the header then definition in some .cpp – mpiatek Feb 19 '17 at 22:38
  • 1
    An `extern` declaration that also has an initializer is a **definition**. – Pete Becker Feb 19 '17 at 23:10
  • Hmm, that seemed to alleviate most of my problems. All extern variables in one header file work fine now, but in a separate header file, there's an extern that's giving me trouble. When I try to declare it with an initial variable it says it "cannot be defined in the current scope" – w0f Feb 19 '17 at 23:26
  • Nevermind, was accidentally declaring the type with the initialization. I'm still getting unresolved external symbol errors though. – w0f Feb 19 '17 at 23:32

0 Answers0