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?

#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;
}
5
  • If you're going to do it from the command line I suggest you get familiar with some type of build tool like make or cmake. Commented Apr 30, 2014 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? Commented Apr 30, 2014 at 20:49
  • have you seen this answer? stackoverflow.com/questions/13263359/… Commented Apr 30, 2014 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. Commented Apr 30, 2014 at 21:02
  • SFML includes the necessary files to build with cmake. Commented Apr 30, 2014 at 21:33

4 Answers 4

6

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

Comments

0

I'm using NPPExec on Notepad++ and this works:

NPP_SAVE
CD $(CURRENT_DIRECTORY)
cmd /c C:\mingw64\bin\g++ -c "$(FILE_NAME)"  -I"C:\SFML-2.6.2\include" -DSFML_STATIC
cmd /c C:\mingw64\bin\g++ "$(NAME_PART)".o  -mwindows -L"C:\SFML-2.6.2\lib" -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32 -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic -o "$(NAME_PART)".exe
cmd /k "$(NAME_PART)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.