title: C++ global variable in multiple files
body: <p>I have a Class Button, that includes a class ButtonManager, which managers buttons.</p>
  
  <p>Then I have 2 functions that handle things, 1 is like login window with few buttons, the second one is the game menu itself, which also has buttons in it.</p>
  
  <p>However, the two windows take quite a lot of lines, and so I decided to split it to multiple .cpp files, where I just call stuff from main.</p>
  
  <p>The problem is, I need to include the button class in both .cpps, and main and the secondary cpp also include some <code>dummy.h</code> which contains the declaration of the common function rendering the menu.</p>
  
  <p>The main issue is, that the ButtonManager has a global variable and when compiling it says that the symbol is already defined.</p>
  
  <p>Example code:</p>
  
  <p>a.h(acts as if it was the Button Manager header file):</p>
  
  <pre><code>#ifndef _ABC_
  #define _ABC_
  
  struct A{
      int b;
  }a = A();
  
  #endif
  </code></pre>
  
  <p>side.h(lets say this is for main game window):</p>
  
  <pre><code>#ifndef _SIDE_H_
  #define _SIDE_H_
  
  int callSomething();
  
  #endif    //_SIDE_H_:
  </code></pre>
  
  <p>side.cpp:</p>
  
  <pre><code>#include "side.h"
  #include "abc.h"
  
  #include &lt;iostream&gt;
  
  int callSomething()
  {
      std::cout &lt;&lt; a.b &lt;&lt; "\n";
      return a.b;
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include "abc.h"
  #include "side.h"
  #include &lt;iostream&gt;
  
  int main()
  {
      callSomething();
      std::cin.get();
  }
  </code></pre>
  
  <p>When I try to compile it, the compiler complains:</p>
  
  <p><code>1&gt;side.obj : error LNK2005: "struct A a" (?a@@3UA@@A) already defined in DynamicDispatch.obj
  1&gt;H:\Microsoft Visual Studio 11.0\example\Debug\dynamicdispatch.exe : fatal error LNK1169: one or more multiply defined symbols found</code></p>
  
  <p>All help is appreciated</p>