'How to compile SFML with MinGW?
I have a simple file "main.cpp" seen below. I also have all the sfml 2.1 libraries under "C:\SFML-2.1\". My question is: What are the commands to compile, link, and run this project? I'm very comfortable using g++ to compile projects from the command line, but have never done so with any external libraries (such as sfml) before. Any help would be greatly appreciated. Thanks.
#include <SFML/Window.hpp>
int main()
{
sf::Window 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();
}
}
return 0;
}
Solution 1:[1]
Quick Answer
g++ -c main.cpp -IC:\SFML-2.1\include -DSFML_STATIC
g++ main.o -o main -LC:\SFML-2.1\lib -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32
main
Long Answer
I'll show you how to either link the project statically or dynamically. It doesn't matter which one you chose to do if you are running the project on your computer, but if you want to send the executable file to another device, chose static linking.
Compile Project
First, compile, but not link, your project using the -c flag. Make sure to include the SFML header files using the -I prefix.
If you are going to statically link SFML, include SFML_STATIC using the -D flag.
// dynamic linking
g++ -c main.cpp -IC:\SFML-2.1\include
// static linking
g++ -c main.cpp -IC:\SFML-2.1\include -DSFML_STATIC
Link Project
Now you have to link the SFML libraries. To link a library, use the -l prefix. For convenience, link the ones you're most likely to use: -lsfml-graphics, -lsfml-window, and -ssfml-system.
If you are statically linking, use a -s prefix on the libraries: -lsfml-graphics-s, -lsfml-window-s, and -lsfml-system-s.
Also, you also have to link certain dependencies for the libraries. That's the opengl32, winmm, and gdi32 libraries. Again, use the prefix -l to link the libraries (you don't need the -s prefix on these libraries even if you are statically linking it).
// dynamic linking
g++ main.o -o main -LC:\SFML-2.1\lib -lsfml-graphics -lsfml-window -lsfml-system -lopengl32 -lwinmm -lgdi32
// static linking
g++ main.o -o main -LC:\SFML-2.1\lib -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32
Run Project
And lastly, just type the name of the exe file in the command line:
main
And you're done!
Solution 2:[2]
Copy the SFML folder present at say Downloads\SFML-2.5.1\include\ to mingw64\lib\gcc\x86_64-w64-mingw32\10.3.0\include\ this should compile your main.cpp using command
g++ main.cpp -c -o main.o
For linking, copy all the .a files present at SFML-2.5.1\lib to msys64\mingw64\lib where all the different linkers are present, this should link your object file, use command
g++ main.o -o main.exe -lsfml-graphics -lsfml-window -lsfml-system -lopengl32 -lwinmm -lgdi32
Then to run the exe file, you need to copy the .dll files, for that, copy all the files inside SFML-2.5.1\bin to mingw64\bin, then just open the .exe file, it should run.
Solution 3:[3]
You can add SFML to path of your compiler or use g++ -I path/SFML
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Lorhan Sohaky |
