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

Author Topic: undefined reference to Error  (Read 5628 times)

0 Members and 1 Guest are viewing this topic.

Zonsa

  • Newbie
  • *
  • Posts: 10
    • View Profile
undefined reference to Error
« on: December 02, 2021, 06:03:54 pm »
Hello,
started out with SFML and need help with connecting the .cpps and .h

main.cpp
#include "game.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>


int main()
{
    do_something();

    return 0;
}

game.h
    #ifndef __GAME_H
    #define __GAME_H

    void do_something();

    #endif

game.cpp
#include "game.h"
#include "main.cpp"


void do_something()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }
}

hope you can help me
thanks
« Last Edit: December 02, 2021, 06:13:54 pm by Zonsa »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: undefined reference to Error
« Reply #1 on: December 02, 2021, 06:53:40 pm »
It looks like the g++ command line is only linking the main.o. It would also need game.o, which is where the do_something() function is.

Zonsa

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: undefined reference to Error
« Reply #2 on: December 02, 2021, 07:07:51 pm »
It looks like the g++ command line is only linking the main.o. It would also need game.o, which is where the do_something() function is.

Makefile looked like this
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

i added to link:
g++ game.o -o game -L src/lib -l sfml-graphics -l sfml-window -l sfml-system

still doesnt work

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: undefined reference to Error
« Reply #3 on: December 03, 2021, 03:17:38 pm »
I usually avoid makefiles at all costs, so this is my memory from about 20 years ago, but iirc...
In your original makefile, the line:
g++ -I src/include -c main.cpp
compiles the file main.cpp, which generates the main.o file.
Then
g++ main.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
says to take main.o and the sfml libraries and link them all together to output the final executable called main.
But game.cpp is never compiled into game.o, and game.o isn't linked with main.o.

CPP files are compiled independently. Then they need to be linked together into a single program.

So you'd need something like:
all: compile link

compile:
        g++ -I src/include -c main.cpp
        g++ -I src/include -c game.cpp


link:
        g++ main.o game.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
(Or maybe game.o main.o. I can't remember if the order there matters in that situation)

Zonsa

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: undefined reference to Error
« Reply #4 on: December 04, 2021, 02:30:18 pm »
I usually avoid makefiles at all costs, so this is my memory from about 20 years ago, but iirc...
In your original makefile, the line:
g++ -I src/include -c main.cpp
compiles the file main.cpp, which generates the main.o file.
Then
g++ main.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
says to take main.o and the sfml libraries and link them all together to output the final executable called main.
But game.cpp is never compiled into game.o, and game.o isn't linked with main.o.

CPP files are compiled independently. Then they need to be linked together into a single program.

So you'd need something like:
all: compile link

compile:
        g++ -I src/include -c main.cpp
        g++ -I src/include -c game.cpp


link:
        g++ main.o game.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
(Or maybe game.o main.o. I can't remember if the order there matters in that situation)

Worked thanks :)