title: Mac CMake C++ SDL2 duplicate symbols for architecture x86_64
body: <p>I am following a tutorial about C++ and SDL2 on youtube that was done in 2017 and I'm not able to progress due to this error. I'm new to working on Mac and assumed the error was due to CMake or using VS Code so I installed Qt creator and I'm still getting the same error. I've researched this error but I can't understand what the problem is and hope someone can help me to understand. My assumption is it has to do with the lines with new GameObject and new Map in Game.cpp.</p>
  <p>Appreciate any help.</p>
  <p>Error:</p>
  <pre><code>18:19:19: Running steps for project cpp...
  18:19:19: Starting: &quot;/Users/m/Qt/Tools/CMake/CMake.app/Contents/bin/cmake&quot; --build . --target all
  [1/6 2.1/sec] Building CXX object CMakeFiles/cpp.dir/GameObject.cpp.o
  [2/6 4.2/sec] Building CXX object CMakeFiles/cpp.dir/TextureManager.cpp.o
  [3/6 6.2/sec] Building CXX object CMakeFiles/cpp.dir/main.cpp.o
  [4/6 8.3/sec] Building CXX object CMakeFiles/cpp.dir/Map.cpp.o
  [5/6 10.2/sec] Building CXX object CMakeFiles/cpp.dir/Game.cpp.o
  [6/6 8.8/sec] Linking CXX executable cpp
  FAILED: cpp 
  : &amp;&amp; /usr/bin/clang++  -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/cpp.dir/main.cpp.o CMakeFiles/cpp.dir/Game.cpp.o CMakeFiles/cpp.dir/GameObject.cpp.o CMakeFiles/cpp.dir/Map.cpp.o CMakeFiles/cpp.dir/TextureManager.cpp.o  -o cpp -F/Library/Frameworks -Wl,-rpath,/Library/Frameworks  -framework  SDL2  -framework Cocoa  -framework  SDL2_image &amp;&amp; :
  duplicate symbol 'TextureManager::Draw(SDL_Texture*, SDL_Rect, SDL_Rect)' in:
  CMakeFiles/cpp.dir/Game.cpp.o
  CMakeFiles/cpp.dir/GameObject.cpp.o
  duplicate symbol 'TextureManager::Draw(SDL_Texture*, SDL_Rect, SDL_Rect)' in:
  CMakeFiles/cpp.dir/Game.cpp.o
  CMakeFiles/cpp.dir/Map.cpp.o
  duplicate symbol 'TextureManager::Draw(SDL_Texture*, SDL_Rect, SDL_Rect)' in:
  CMakeFiles/cpp.dir/Game.cpp.o
  CMakeFiles/cpp.dir/TextureManager.cpp.o
  ld: 3 duplicate symbols for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.
  18:19:20: The process &quot;/Users/m/Qt/Tools/CMake/CMake.app/Contents/bin/cmake&quot; exited with code 1.
  Error while building/deploying project cpp (kit: Desktop Qt 5.15.1 clang 64bit)
  When executing step &quot;CMake Build&quot;
  18:19:20: Elapsed time: 00:01.
  </code></pre>
  <p>TextureManager.cpp:</p>
  <pre><code>#include &quot;TextureManager.h&quot;
  
  SDL_Texture* TextureManager::LoadTexture(const char* texture)
  {
      SDL_Surface* tempSurface = IMG_Load(texture);
      SDL_Texture* tex = SDL_CreateTextureFromSurface(Game::renderer, tempSurface);
      SDL_FreeSurface(tempSurface);
  
      return tex;
  }
  
  </code></pre>
  <p>TextureManager.h:</p>
  <pre><code>#include &quot;Game.h&quot;
  
  class TextureManager {
  
  public:
      static SDL_Texture* LoadTexture(const char* fileName);
      static void Draw(SDL_Texture* tex, SDL_Rect src, SDL_Rect dest);
  
  };
  
  void TextureManager::Draw(SDL_Texture * tex, SDL_Rect src, SDL_Rect dest)
  {
      SDL_RenderCopy(Game::renderer, tex, &amp;src, &amp;dest);
  }
  
  </code></pre>
  <p>Game.cpp:</p>
  <pre><code>#include &quot;Game.h&quot;
  #include &quot;TextureManager.h&quot;
  #include &quot;GameObject.h&quot;
  #include &quot;Map.h&quot;
  
  GameObject* player;
  GameObject* enemy;
  Map* map;
  
  SDL_Renderer* Game::renderer = nullptr;
  
  Game::Game()
  {}
  Game::~Game()
  {}
  
  void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen)
  {
      int flags = 0;
      if(fullscreen)
      {
          flags = SDL_WINDOW_FULLSCREEN;
      }
  
      if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
      {
          std::cout &lt;&lt; &quot;Subsystems Initialized...&quot; &lt;&lt; std::endl;
  
          window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
          if(window)
          {
              std::cout &lt;&lt; &quot;Window created...&quot; &lt;&lt; std::endl;
          }
  
          renderer = SDL_CreateRenderer(window, -1, 0);
          if(renderer)
          {
              SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
              std::cout &lt;&lt; &quot;Renderer created...&quot; &lt;&lt; std::endl;
          }
  
          isRunning = true;
      } else {
          isRunning = false;
      }
  
      player = new GameObject(&quot;../assets/player.png&quot;, 0, 0);
      enemy = new GameObject(&quot;../assets/enemy.png&quot;, 50, 50);
      map = new Map();
  
  }
  
  void Game::handleEvents()
  {
      SDL_Event event;
      SDL_PollEvent(&amp;event);
      switch (event.type)
      {
      case SDL_QUIT:
          isRunning = false;
          break;
  
      default:
          break;
      }
  
  }
  
  void Game::update()
  {
      player-&gt;Update();
      enemy-&gt;Update();
  }
  
  void Game::render()
  {
      SDL_RenderClear(renderer);
  
      map-&gt;DrawMap();
      player-&gt;Render();
      enemy-&gt;Render();
  
      SDL_RenderPresent(renderer);
  }
  
  void Game::clean()
  {
      SDL_DestroyWindow(window);
      SDL_DestroyRenderer(renderer);
      SDL_Quit();
      std::cout &lt;&lt; &quot;Game cleaned...&quot; &lt;&lt; std::endl;
  }
  
  </code></pre>
  <p>Game.h:</p>
  <pre><code>#ifndef Game_hpp
  #define Game_hpp
  #include &lt;SDL2/SDL.h&gt;
  #include &quot;SDL_image.h&quot;
  #include &lt;iostream&gt;
  
  class Game {
  
  public:
      Game();
      ~Game();
  
      void init(const char* title, int x, int y, int width, int height, bool fullscreen);
  
      void handleEvents();
      void update();
      void render();
      void clean();
  
      bool running() { return isRunning; }
  
      static SDL_Renderer *renderer;
  
  private:
      int cnt = 0;
      bool isRunning;
      SDL_Window *window;
  };
  
  #endif /* Game_hpp */
  
  </code></pre>
  <p>GameObject.cpp:</p>
  <pre><code>#include &quot;GameObject.h&quot;
  #include &quot;TextureManager.h&quot;
  
  GameObject::GameObject(const char* texturesheet, int x, int y)
  {
      objTexture = TextureManager::LoadTexture(texturesheet);
  
      xpos = x;
      ypos = y;
  }
  
  void GameObject::Update()
  {
      xpos++;
      ypos++;
  
      srcRect.h = 32;
      srcRect.w = 32;
      srcRect.x = 0;
      srcRect.y = 0;
  
      destRect.x = xpos;
      destRect.y = ypos;
      destRect.h = srcRect.h * 2;
      destRect.w = srcRect.w * 2;
  
  }
  
  void GameObject::Render()
  {
      SDL_RenderCopy(Game::renderer, objTexture, &amp;srcRect, &amp;destRect);
  }
  
  </code></pre>
  <p>GameObject.h:</p>
  <pre><code>#pragma once
  #include &quot;Game.h&quot;
  
  class GameObject {
  
  public:
      GameObject(const char* texturesheet, int x, int y);
      ~GameObject();
  
      void Update();
      void Render();
  
  private:
      int xpos;
      int ypos;
  
      SDL_Texture* objTexture;
      SDL_Rect srcRect, destRect;
  
  
  };
  
  </code></pre>
  <p>Map.cpp:</p>
  <pre><code>#include &quot;Map.h&quot;
  #include &quot;TextureManager.h&quot;
  
  int lvl1[20][25] = {
      { 0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
  };
  
  Map::Map()
  {
      dirt = TextureManager::LoadTexture(&quot;../assets/dirt.png&quot;);
      grass = TextureManager::LoadTexture(&quot;../assets/grass.png&quot;);
      water = TextureManager::LoadTexture(&quot;../assets/water.png&quot;);
  
      LoadMap(lvl1);
  
      src.x = src.y = 0;
      src.w = dest.w = 32;
      src.h = dest.h = 32;
  
      dest.x = dest.y = 0;
  }
  
  void Map::LoadMap(int arr[20][25])
  {
      for (int row = 0; row &lt; 20; row++)
      {
          for (int column = 0; column &lt; 25; column++)
          {
              map[row][column] = arr[row][column];
          }
      }
  }
  
  void Map::DrawMap()
  {
      int type = 0;
  
      for (int row = 0; row &lt; 20; row++)
      {
          for (int column = 0; column &lt; 25; column++)
          {
              type = map[row][column];
  
              dest.x = column * 32;
              dest.y = row * 32;
  
              switch (type)
              {
              case 0:
                  TextureManager::Draw(water, src, dest);
                  break;
              case 1:
                  TextureManager::Draw(grass, src, dest);
                  break;
              case 2:
                  TextureManager::Draw(dirt, src, dest);
                  break;
              default:
                  break;
              }
  
          }
      }
  }
  
  </code></pre>
  <p>Map.h:</p>
  <pre><code>#pragma once
  #include &quot;Game.h&quot;
  
  class Map
  {
  
  public:
      Map();
      ~Map();
  
      void LoadMap(int arr[20][25]);
      void DrawMap();
  
  private:
      SDL_Rect src, dest;
  
      SDL_Texture* dirt;
      SDL_Texture* grass;
      SDL_Texture* water;
  
      int map[20][25];
  
  };
  
  </code></pre>
  <p>main.cpp:</p>
  <pre><code>#include &quot;Game.h&quot;
  
  Game *game = nullptr;
  
  int main(int argc, const char * argv[]) {
  
      const int FPS = 60;
      const int frameDelay = 1000 / FPS;
  
      Uint32 frameStart;
      int frameTime;
  
      game = new Game();
  
      game-&gt;init(&quot;2D Engine&quot;, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 640, false);
  
      while (game-&gt;running())
      {
          frameStart = SDL_GetTicks();
  
          game-&gt;handleEvents();
          game-&gt;update();
          game-&gt;render();
  
          frameTime = SDL_GetTicks() - frameStart;
  
          if(frameDelay &gt; frameTime)
          {
              SDL_Delay(frameDelay - frameTime);
          }
      }
  
      game-&gt;clean();
  
      return 0;
  }
  
  </code></pre>