title: G++: "Undefined Reference to" error
body: <p>Note: I am aware that there plenty of questions similar to this one here, but I've looked at them all, to no avail.</p>
  
  <p>So, I have a simple <strong>singleton GameManager class</strong> split into and <code>.h</code> and an <code>.cpp</code> file.</p>
  
  <p>GameManager.h</p>
  
  <pre><code>#ifndef _GM_H_
  #define _GM_H_
  
  #include &lt;iostream&gt;
  /**
   * Singleton GameManager class.
   *
   **/
  class GameManager {
  
      //Private Variables
      private:
          static GameManager* _instance;
          int _gameObjects;
  
      //Private Methods
      private:
          GameManager();
          ~GameManager();
  
      //Public Methods and Functions
      public:
          static GameManager* instance()
          {
              if (!_instance) 
                  _instance = new GameManager();
  
              return _instance;
          }
  
          //Placeholders
          void addObject();
          int getObjects();
  };
  
  #endif
  </code></pre>
  
  <p>GameManager.cpp</p>
  
  <pre><code>#include "GameManager.h"
  #include &lt;iostream&gt;
  
  
  GameManager::GameManager()
  {
      _gameObjects = 0 ;
  }
  
  void GameManager::addObject()
  {
      _gameObjects += 1;
  }
  
  int GameManager::getObjects()
  {
      return _gameObjects;
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include "GameManager.h"
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  
  
  int main(int argc, char** argv)
  {
  
      GameManager::instance();
      GameManager::instance()-&gt;addObject();
      std::cout &lt;&lt; GameManager::instance()-&gt;getObjects() &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>When I try to compile this using </p>
  
  <pre><code>g++ main.cpp GameManager.cpp
  </code></pre>
  
  <p>I get the error</p>
  
  <pre><code>/tmp/cctNvscU.o: In function `GameManager::instance()':
  main.cpp:(.text._ZN11GameManager8instanceEv[_ZN11GameManager8instanceEv]+0x9): undefined reference to `GameManager::_instance'
  main.cpp:(.text._ZN11GameManager8instanceEv[_ZN11GameManager8instanceEv]+0x29): undefined reference to `GameManager::_instance'
  main.cpp:(.text._ZN11GameManager8instanceEv[_ZN11GameManager8instanceEv]+0x2e): undefined reference to `GameManager::_instance'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>Any help is appreciated. Thanks in advance.</p>