7

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;
}
Quinn McHugh
  • 1,577
  • 2
  • 17
  • 23
  • If you're going to do it from the command line I suggest you get familiar with some type of build tool like [make](https://www.gnu.org/software/make/) or [cmake](http://www.cmake.org/). – Captain Obvlious Apr 30 '14 at 20:44
  • I considered doing it with make, do you by chance know where a viable makefile for sfml 2.1 might reside so I can utilize it? – Quinn McHugh Apr 30 '14 at 20:49
  • have you seen this answer? http://stackoverflow.com/questions/13263359/using-mingw-to-compile-a-sfml-project – Jimmy Apr 30 '14 at 20:58
  • Yes, I saw that question before posting this one and I don't have any of the .a library files, only the files that come when downloaded from sfml.org. I also am not having the same issue as him because I don't even know how to compile my simple program with sfml libraries recognized. – Quinn McHugh Apr 30 '14 at 21:02
  • SFML includes the necessary files to build with cmake. – Captain Obvlious Apr 30 '14 at 21:33

3 Answers3

5

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, choose 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.

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 suffix 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 executable file in the command line:

main

And you're done!

Kesto2
  • 70
  • 1
  • 13
Alex
  • 462
  • 1
  • 7
  • 19
3

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.

Yasir
  • 73
  • 1
  • 6
0

You can add SFML to path of your compiler or use g++ -I path/SFML

Lorhan Sohaky
  • 19
  • 1
  • 4