-
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
-
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.
-
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
-
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)
-
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 :)