I want to get into game development with C++, so I decided to try SFML. However, even after putting all the files in the right places, it won't find the files need.
I followed all the steps on the page https://www.sfml-dev.org/tutorials/2.5/start-osx.php, making sure to put all the frameworks and dylibs in the right folders, but it still won't allow me to use the SFML files
#include <iostream>
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
window.display();
}
return 0;
}
What the program should do is display a black window that can close, but it gives me the error message
g++ -Wall -o "helloWorld" "helloWorld.cpp" (in directory /Users/Splavacado100/Desktop/Coding/C++)
helloWorld.cpp:2:10 fatal error: 'SFML/Graphics.hpp' file not found
#include <SFML/Graphics.hpp>
SFML (and to my knowledge any additional library outside the Standard Template Library) requires the compiler to know where the lib and include folders are stored. In this error instance, it looks like the IDE you're using isn't finding the right path to the folders. It's not listed in the MacOS version, because the tutorial you liked to assumes you're using XCode, which is like Visual Studio for Mac.
From what I can gather, if you're writing your program in a plain text editor, and compiling using makefiles or command line prompts, look at the SFML and Linux article for more complete idea of how to use it. The relevant to this scenario:
In case you installed SFML to a non-standard path, you'll need to tell the compiler where to find the SFML headers (.hpp files):
g++ -c main.cpp -I<sfml-install-path>/include Here
, is the directory where you copied SFML, for example /home/me/sfml.You must then link the compiled file to the SFML libraries in order to get the final executable. SFML is made of 5 modules (system, window, graphics, network and audio), and there's one library for each of them. To link an SFML library, you must add "-lsfml-xxx" to your command line, for example "-lsfml-graphics" for the graphics module (the "lib" prefix and the ".so" extension of the library file name must be omitted).
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
If you installed SFML to a non-standard path, you'll need to tell the linker where to find the SFML libraries (.so files):
g++ main.o -o sfml-app -L<sfml-install-path>/lib -lsfml-graphics
-\lsfml-window -lsfml-system
We are now ready to execute the compiled program: