Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: g++ undefined reference - git project  (Read 1424 times)

0 Members and 1 Guest are viewing this topic.

throwaway1

  • Newbie
  • *
  • Posts: 2
    • View Profile
g++ undefined reference - git project
« on: May 10, 2015, 01:37:00 am »
First off, I'm able to successfully launch the simple "SFML Works!" app by means of the steps located here: http://www.sfml-dev.org/tutorials/2.3/start-linux.php.

I'm attempting to get a Git project working on xubuntu.

When I enter:

g++ main.o -o game -lsfml-graphics -lsfml-window -lsfml-system

I get:
main.o: In function `main':
main.cpp:(.text+0x17): undefined reference to `Game::Game()'
main.cpp:(.text+0x26): undefined reference to `Game::run()'
main.cpp:(.text+0x35): undefined reference to `Game::~Game()'
main.cpp:(.text+0x4e): undefined reference to `Game::~Game()'
collect2: error: ld returned 1 exit status

Contents of main.cpp:

#include "Game.hpp"

int main()
{
        Game game;
        game.run();
}

Contents of Game.cpp:
#include "Game.hpp"

const sf::Time Game::TimePerFrame = sf::seconds(1.f / 60.f);

Game::Game()
        : input()
        , window(sf::VideoMode(640, 480), "SFML Application")
        , map("SampleMap.txt")
        , debugger()
{

}

void Game::run()
{
        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        while (window.isOpen())
        {
                timeSinceLastUpdate += clock.restart();
                while (timeSinceLastUpdate > TimePerFrame)
                {
                        timeSinceLastUpdate -= TimePerFrame;
                        processEvents();
                        update(TimePerFrame);
                }
                sf::Time deltaTime = clock.restart();
                update(deltaTime);
                render();
        }
}

void Game::processEvents()
{
        sf::Event event;
        while (window.pollEvent(event))
        {
                switch (event.type)
                {
                case sf::Event::Closed:
                        window.close();
                        break;
                }
        }
}

void Game::update(sf::Time deltaTime)
{
        debugger.update(deltaTime);
        input.update();
        map.player.moveUp = input.cmdUp;
        map.player.moveDown = input.cmdDown;
        map.player.moveLeft = input.cmdLeft;
        map.player.moveRight = input.cmdRight;
        map.update(deltaTime);
}

void Game::render()
{
        window.clear();
        map.render(window);
        debugger.render(window);
        window.display();
}
 

Contents of Game.hpp:
#pragma once

#include <SFML/Graphics.hpp>
#include "Map.hpp"
#include "Debugger.hpp"

class Game
{
public:
        Game();
        ~Game();
        void run();

private:
        void processEvents();
        void update(sf::Time deltaTime);
        void render();

        static const sf::Time TimePerFrame;
        Input input;
        sf::RenderWindow window;
        Map map;
        Debugger debugger;
};

G++ / GCC version: 4.9.2 (also tried 4.8.1)

I've tried removing the *.o file and re-compiling using the -std=c++11parameter, but it doesn't appear to make a difference.

Switching from #pragma_once back to #ifndef didn't seem to fix it either - unless I'm using that incorrectly.

The code was originally created on a Windows machine, will I not be able to work on this code in my Linux environment?

All help is greatly appreciated.
« Last Edit: May 10, 2015, 01:54:28 am by throwaway1 »

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: g++ undefined reference - git project
« Reply #1 on: May 10, 2015, 02:14:23 am »
You need to compile ALL *.cpp files, and link ALL *.o files.

In the command you gave, you're only linking to the main.o file.

throwaway1

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: g++ undefined reference - git project
« Reply #2 on: May 10, 2015, 02:47:40 am »
Thank you :).

This lead to other compile errors which I've resolved and things are working properly now.