Stucked with a problem

Hi, I'm trespassing a working project from VisualStudio (Windows) to g++ (Xubuntu).

In visual studio it compile and worked well but in linux it throws an error when I try to compile:

main.o: In function `main':
/home/albert/Projects/SaGa2/main.cpp:5: undefined reference to `SaGa::Game::Game(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'output' failed
make: *** [output] Error 1

My main function is:

1
2
3
4
5
6
7
#include "Game.hpp"
#include "DEFINITIONS.hpp"
int main() {
        SaGa::Game(SCREEN_WIDTH, SCREEN_HEIGHT, "SaGa II");

        return EXIT_SUCCESS;
}


And the Game.hpp code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once

#include <memory>
#include <string>
#include <SFML/Graphics.hpp>
#include "StateMachine.hpp"
#include "AssetManager.hpp"
#include "InputManager.hpp"

namespace SaGa
{
        struct GameData
        {
                StateMachine machine;
                sf::RenderWindow window;
                AssetManager assets;
                InputManager input;
        };

        typedef std::shared_ptr<GameData> GameDataRef;

        class Game
        {
        public:
                Game(int width, int height, std::string title);

        private:
                const float dt = 1.0f / 60.0f; // frame rate
                sf::Clock _clock;

                GameDataRef _data = std::make_shared<GameData>();

                void Run();
        };
}


I am newbie with g++ (v. 7.4.9) and I don't know what is happening
What's happening is that somewhere there is a file containing the actual implementation code for the constructor function Game(int width, int height, std::string title);, and you aren't building (or not linking) that code (which I'd guess lives in a file named Game.cpp)

How are you building this? What command line or IDE?
Last edited on
I have implemented the function inside Game.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Game.hpp"
#include "SplashState.hpp"
#include <stdlib.h>
#include <time.h>

namespace SaGa
{
        Game::Game(int width, int height, std::string title)
        {
                srand(time(NULL));

                _data->window.create(sf::VideoMode(width, height), title, sf::Style::Close | sf::Style::Titlebar);
                _data->machine.AddState(StateRef(new SplashState(this->_data)));

                this->Run();
        }
...


I compile the project using a MakeFile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
LIBS= -lsfml-graphics -lsfml-window -lsfml-system

output: main.o AssetManager.o Game.o InputManager.o SplashState.o StateMachine.o TileMap.o WorldMapState.o
        g++ -g -std=c++11 main.o -o output

main.o: main.cpp
        g++ -g -std=c++11 -Wall -c main.cpp

AssetManager.o: AssetManager.cpp AssetManager.hpp
        g++ -g -std=c++11 -Wall -c AssetManager.cpp

Game.o: Game.cpp Game.hpp
        g++ -g -std=c++11 -Wall -c Game.cpp

InputManager.o: InputManager.cpp InputManager.hpp
        g++ -g -std=c++11 -Wall -c InputManager.cpp

StateMachine.o: StateMachine.cpp StateMachine.hpp
        g++ -g -std=c++11 -Wall -c StateMachine.cpp

TileMap.o: TileMap.cpp TileMap.hpp
        g++ -g -std=c++11 -Wall -c TileMap.cpp

WorldMapState.o: WorldMapState.cpp WorldMapState.hpp
        g++ -g -std=c++11 -Wall -c WorldMapState.cpp

clean:
        rm *.o

I think it could be the order of the output instruction. I will try to reorder it.

Sorry for not repling
Last edited on
I have moved the main.o of output to the end of the line. It throws the same error. Also in the error, the function takes 5 params, but I defined it to have 3 (I suppose that it is normal using a <string> param).
Ok. I'm stupid.

1
2
output: main.o AssetManager.o Game.o InputManager.o SplashState.o StateMachine.o TileMap.o WorldMapState.o
        g++ -g  main.o AssetManager.o Game.o InputManager.o SplashState.o StateMachine.o TileMap.o WorldMapState.o -o output


This is the correct way to do it. But now it doesn't find the SFML libraries :(
You need to add the sfml libraries to the link line. Put all of the following that you actually need (e.g., if you aren't using the network subsystem don't include that library) right before -o ouput

 
-lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lsfml-network

You'll need to add options to the g++ command, to link against those libraries. You'll need to supply the names of those libraries, and the paths to the directories that contain those libraries.
Thanks dutch and MykeyBoy. It works!
You're welcome!
Topic archived. No new replies allowed.