SFML community forums

Help => Window => Topic started by: Erkoren on May 08, 2016, 02:55:20 pm

Title: [Solved] XCode wont let me compile if I put this Game class in its own file
Post by: Erkoren on May 08, 2016, 02:55:20 pm
Hi!

If I try to run this code from the SFML Game Development Book. I can run it if the class and the main() are in the same file but once I put the class in its own header file, which I include in the main file XCode won't let me compile. I'm on Mac OSX 10.10.

#include <SFML/Graphics.hpp>
class Game
{
public:
  Game();
  void run();
private:
  void processEvents();
  void update();
  void render();
private:
  sf::RenderWindow mWindow;
  sf::CircleShape  mPlayer;
};

Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application") , mPlayer()
{
  mPlayer.setRadius(40.f);
  mPlayer.setPosition(100.f, 100.f);
  mPlayer.setFillColor(sf::Color::Cyan);
}
void Game::run()
{
  while (mWindow.isOpen())
  {
    processEvents();
    update();
    render();
  }
}

void Game::processEvents()
{
  sf::Event event;
  while (mWindow.pollEvent(event))
  {
    if (event.type == sf::Event::Closed)
      mWindow.close();
  }
}

void Game::update()
{
 
}

void Game::render()
{
  mWindow.clear();
  mWindow.draw(mPlayer);
  mWindow.display();
}

int main()
{
  Game game;
  game.run();
}
Title: Re: XCode wont let me compile if I put this Game class in its own file
Post by: Mr_Blame on May 08, 2016, 03:02:33 pm
Which compilation error you get?
Title: Re: XCode wont let me compile if I put this Game class in its own file
Post by: Erkoren on May 08, 2016, 03:23:45 pm
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Title: Re: XCode wont let me compile if I put this Game class in its own file
Post by: Mr_Blame on May 08, 2016, 03:27:03 pm
As I can see the compiler says that you didn't link to some libs. Post please list of libs to which you linked, maybe you are missing some.
Title: Re: XCode wont let me compile if I put this Game class in its own file
Post by: Laurent on May 08, 2016, 03:29:10 pm
Quote
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This doesn't look like an actual error message, but rather the last line that tells you that there were errors. Look at the full output.

Quote
As I can see the compiler says that you didn't link to some libs.
Really? All I can see is that the linker failed for some unknown reason. A linker error doesn't always mean that you forgot to link something, it can be many other things.
Title: Re: XCode wont let me compile if I put this Game class in its own file
Post by: Erkoren on May 08, 2016, 04:08:02 pm
Hi!

Problem was solved once I put the implementation of the methods into the .cpp file and the class in the .h file.

When it didn't work, the Game.h file had the exact same code from above (minus the main), the Game.cpp was simply empty and the main file had the include and the main function.

I'm sorry to have bothered you something this simple. Anyways thanks for your help.