Search code examples
c++makefilesfmlgame-development

Error when compiling c++ program with SFML


I am learning SFML with c++, when I am compiling with mingw32-make it is giving error because I am using class file

this is the error:

main.o:main.cpp:(.text+0x16): undefined reference to `Game::Game()'
main.o:main.cpp:(.text+0x21): undefined reference to `Game::running() const'
main.o:main.cpp:(.text+0x30): undefined reference to `Game::pollEvent()'
main.o:main.cpp:(.text+0x3b): undefined reference to `Game::update()'
main.o:main.cpp:(.text+0x46): undefined reference to `Game::render()'
main.o:main.cpp:(.text+0x58): undefined reference to `Game::~Game()'
main.o:main.cpp:(.text+0x69): undefined reference to `Game::~Game()'
collect2.exe: error: ld returned 1 exit status
Makefile:6: recipe for target 'link' failed
mingw32-make: *** [link] Error 1

this is the code

main.cpp:

#include "Game.h"
#include <iostream>
using namespace std;
using namespace sf;
int main()
{
Game mygame;
while (mygame.running()){
    //poll event
    mygame.pollEvent();
    //update
    mygame.update();
    //render
    mygame.render();
}
//End of the game
return 0;

}

Game.h:

#ifndef GAME_H
#define GAME_H
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
using namespace sf;
class Game{
private:
//variable 
RenderWindow *window;
Event ev;
VideoMode videoMode;
//private function
void initVar();
void initWindow();

public:
    //constructor / Destructor
    Game();
    virtual ~Game();
    //Accessors
    const bool running() const;
    //functions
    void pollEvent();
    void update();
    void render();
};
#endif

Game.cpp:

#include "Game.h"
#include <iostream>
void Game::initVar(){
     this->window = nullptr;
}
void Game::initWindow(){
    this->videoMode.height = 600;
    this->videoMode.width = 800; 
    this->window = new RenderWindow(this->videoMode, "Game 1", Style::Titlebar | Style::Close | Style::Resize);
}
//constructure
Game::Game(){
    this->initVar();
    this->initWindow();
}
//Destructure
Game::~Game(){
    delete this->window;
}
//Accsessors
const bool Game::running() const{
    return this->window->isOpen();
}
//Functions
void Game::pollEvent(){
    while(this->window->pollEvent(this->ev)){
            switch(this->ev.type){
                case Event::Closed:
                    this->window->close();
                    break;
                case Event::KeyPressed:
                    if(this->ev.key.code == Keyboard::Escape){
                        this->window->close();
                        break;
                     }
            }
        }
}
void Game::update(){
    this->pollEvent();
}
void Game::render(){
    this->window->clear(Color(255, 0, 0, 255));
    //Draw game objects
    this->window->display();
}

Makefile:

all: compile link

compile:
    g++ -I src/include -c main.cpp
link:
    g++ main.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system

Solution

  • You're not compiling game.cpp, so the linker is looking for the implementation of these functions and is unable to find them. You can update your makefile as follows

    compile:
        g++ -I src/include -c main.cpp -c Game.cpp
    link:
        g++ main.o Game.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
    

    and it should work