title: Unresolved linker error with extern'ed global variable in multiple source files
body: <p>I have an unresolved external symbol error that I can't seem to find the source off.
  I am trying to give global access to an Engine class by declaring an instance of Engine to have external linkage (<code>g_engine</code>). My main.cpp file is the only file that defines this instance (as far as I have been able to find out). And that works fine. But when I add a call to a method on <code>g_engine</code> in another source file I get a linker error.</p>
  
  <p>engine.hpp:</p>
  
  <pre><code>#include &lt;other_stuff&gt;
  #include "Map.hpp"
  
  class Engine
  {
  public:
    std::shared_ptr&lt;Map&gt; map;
    void run();
    void do_stuff();
    // other methods
  };
  
  extern Engine g_engine;  // Declared in header file
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include "engine.hpp"
  
  Engine g_engine;  // Defined only here
  
  int main()
  {
    g_engine.run();
    return 0;
  }
  </code></pre>
  
  <p>map.hpp:</p>
  
  <pre><code>// Only includes standard headers
  
  class Map
  {
    void some_func();
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: added comment to the constructor</p>
  
  <p>map.cpp:</p>
  
  <pre><code>#include "engine.hpp"
  
  Map::Map()
  {
    // Calls some_func somewhere.
  }
  
  void Map::some_func()
  {
    // g_engine.do_stuff()  &lt;--- Uncommenting this line causes a linker error.
  }
  </code></pre>
  
  <p><strong>EDIT: Added Engine.cpp</strong></p>
  
  <p>Engine.cpp:</p>
  
  <pre><code>Engine::Engine()
  {
    map = std::make_shared&lt;Map&gt;();
  }
  
  void Engine::run()
  {
    // unrelated code
  }
  </code></pre>
  
  <p>With the line commented out, no error is given and the application runs fine. When the line is uncommented, compiling is fine, but a linker error is given: </p>
  
  <pre><code>Error   1   error LNK2001: unresolved external symbol "class Engine g_engine" (?g_engine@@3VEngine@1@A) map.obj.
  </code></pre>