SFML and gcc (Linux)
This tutorial is the first one you should read if you’re using SFML under Linux with the GCC compiler. It will explain how to install SFML, setup your compiler, and compile a SFML program.
Compiling SFML libraries is also explained, for more advanced users (although it’s quite simple).
Installing SFML
First, you must download the SFML development files. It is recommended that you download the full SDK, which contains source code and binaries, as well as samples and documentation.
The package can be found on the download page.
Once you have downloaded and extracted the files to your hard drive, you must install the SFML headers and library files to the appropriate location. To do so, you just have to go to the SFML-x.y directory and type «sudo make install».
Compiling your first SFML program
Create a new text file, and write a SFML program. For example, you can try the sf::Clock class of the System package :
#include #include int main() < sf::Clock Clock; while (Clock.GetElapsedTime() < 5.f) < std::cout return 0; >
Don’t forget that all SFML classes and functions are in the sf namespace.
Then compile it like any C++ program, and link to the SFML libraries you are using with the «-l» directive :
g++ -c clock.cpp g++ -o clock clock.o -lsfml-system
When linking to multiple SFML libraries, make sure you link them in the right order, as it’s important for gcc. The rule is the following : if library XXX depends on (uses) library YYY, put XXX first and then YYY. An exemple with SFML : sfml-graphics depends on sfml-window, and sfml-window depends on sfml-system. The link command line would be as follows :
g++ -o . -lsfml-graphics -lsfml-window -lsfml-system
Basically, every SFML library depends on sfml-system, and sfml-graphics also depends on sfml-window. That’s it for dependencies.
If you are using the Graphics or Audio packages, you must first install the external libraries needed by each package.
Graphics requires freetype.
Audio requires libsndfile and openal.
These libraries can be installed using the standard package manager of your system, or downloaded from their official website.
If you have troubles using the default OpenAL library (which is often the case as the Linux implementation is not stable), you can replace it with the OpenAL-Soft implementation. The binaries are fully compatible, so you won’t need to recompile SFML nor your programs using it.
Compiling SFML (for advanced users)
If the precompiled SFML libraries don’t exist for your system, you can compile them quite easily. In such case, no test have been made so you are encouraged to report any failure or success encountered during your compile process. If you succeed compiling SFML for a new platform, please contact the development team so that we can share the files with the community.
First, you need to install the development packages of the external libraries used by SFML. Here is the complete list:
- build-essential
- mesa-common-dev
- libx11-dev
- libxrandr-dev
- libgl1-mesa-dev
- libglu1-mesa-dev
- libfreetype6-dev
- libopenal-dev
- libsndfile1-dev
You can also get them automatically if SFML is in your distribution’s packages repository, with the following command:
Then, to actually compile the SFML libraries and samples, you must download and install the full SDK. Go to the SFML-x.y directory, and type the following commands:
make # builds the SFML libraries sudo make install # installs the compiled libraries make sfml-samples # compiles the SFML examples
Note: if Qt and wxWidgets are not installed on your system, you may get compile errors with the corresponding samples; just ignore them.
The following options are available to customize the build :
- DESTDIR=xxx : installs SFML to xxx path instead of the default one (which is /usr/lib)
- DEBUGBUILD=yes/no : builds debug or optimized SFML libraries (default is no — optimized)
- STATIC=yes/no : builds static or dynamic SFML libraries (default is no — dynamic)
SFML and Linux
This tutorial is the first one you should read if you’re using SFML on Linux. It will explain how to install SFML, and compile projects that use it.
Installing SFML
There are different approaches to the installation of SFML on Linux:
- Install it directly from your distribution’s package repository
- Get the source code, build it and install it
- Download the precompiled SDK and manually copy the files
Option 1 is the preferred one; if the version of SFML that you want to install is available in the official repository, then install it using your package manager. For example, on Debian you would do:
sudo apt-get install libsfml-dev
Option 2 requires more work: you need to ensure all of SFML’s dependencies including their development headers are available, make sure CMake is installed, and manually execute some commands. This will result in a package which is tailored to your system.
If you want to go this way, there’s a dedicated tutorial on building SFML yourself.
Finally, option 3 is a good choice for quick installation if SFML is not available as an official package. Download the SDK from the download page, unpack it and copy the files to your preferred location: either a separate path in your personal folder (like /home/me/sfml), or a standard path (like /usr/local).
If you already had an older version of SFML installed, make sure that it won’t conflict with the new version!
Compiling a SFML program
In this tutorial we’re not going to talk about IDEs such as Code::Blocks or Eclipse. We’ll focus on the commands required to compile and link an SFML executable. Writing a complete makefile or configuring a project in an IDE is beyond the scope of this tutorial — there are better dedicated tutorials for this.
If you’re using Code::Blocks, you may refer to the Code::Blocks tutorial for Windows; many things should be similar. You won’t have to set the compiler and linker search paths if you installed SFML to one of your system’s standard paths.
First, create a source file. For this tutorial we’ll name it «main.cpp». Put the following code inside the main.cpp file:
#include int main() < 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(); > return 0; >
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):
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 /lib -lsfml-graphics -lsfml-window -lsfml-system
We are now ready to execute the compiled program:
If SFML is not installed in a standard path, you need to tell the dynamic linker where to find the SFML libraries first by specifying LD_LIBRARY_PATH:
export LD_LIBRARY_PATH= /lib && ./sfml-app
If everything works, you should see this in a new window:
SFML and Linux
This tutorial is the first one you should read if you’re using SFML on Linux. It will explain how to install SFML, and compile projects that use it.
Installing SFML
There are different approaches to the installation of SFML on Linux:
- Install it directly from your distribution’s package repository
- Get the source code, build it and install it
- Download the precompiled SDK and manually copy the files
Option 1 is the preferred one; if the version of SFML that you want to install is available in the official repository, then install it using your package manager. For example, on Debian you would do:
sudo apt-get install libsfml-dev
Option 2 requires more work: you need to ensure all of SFML’s dependencies including their development headers are available, make sure CMake is installed, and manually execute some commands. This will result in a package which is tailored to your system.
If you want to go this way, there’s a dedicated tutorial on building SFML yourself.
Finally, option 3 is a good choice for quick installation if SFML is not available as an official package. Download the SDK from the download page, unpack it and copy the files to your preferred location: either a separate path in your personal folder (like /home/me/sfml), or a standard path (like /usr/local).
If you already had an older version of SFML installed, make sure that it won’t conflict with the new version!
Compiling a SFML program
In this tutorial we’re not going to talk about IDEs such as Code::Blocks or Eclipse. We’ll focus on the commands required to compile and link an SFML executable. Writing a complete makefile or configuring a project in an IDE is beyond the scope of this tutorial — there are better dedicated tutorials for this.
If you’re using Code::Blocks, you may refer to the Code::Blocks tutorial for Windows; many things should be similar. You won’t have to set the compiler and linker search paths if you installed SFML to one of your system’s standard paths.
First, create a source file. For this tutorial we’ll name it «main.cpp». Put the following code inside the main.cpp file:
#include int main() < 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(); > return 0; >
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):
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 /lib -lsfml-graphics -lsfml-window -lsfml-system
We are now ready to execute the compiled program:
If SFML is not installed in a standard path, you need to tell the dynamic linker where to find the SFML libraries first by specifying LD_LIBRARY_PATH:
export LD_LIBRARY_PATH= /lib && ./sfml-app
If everything works, you should see this in a new window: