Building SFML 2.0 with Make (GCC/Ubuntu)
I’ve now demonstrated how to build the SFML 2.0 binaries in an IDE and also with a build utility (better). However, all my tutorials have undoubtedly been Windows centric – I discussed Microsoft Visual C++ and MinGW, after all. Well now it’s time to make that right – in this article I’m going to explain how to build the latest versions of the SFML 2.0 binaries for Ubuntu, using GCC.
Other Unix Operating Systems
With the addition of the post, I’ve covered Windows – both MinGW and MSVC – and also GCC on Ubuntu, Linux Mint, Debian or an Ubuntu variant such as Xubuntu. But what about the rest of you? What if you’re on another Unix operating system? Well, the fact is that you should still have access to GCC. This means you should be able to invoke the actual build operation with the same commands. The other software we need – CMake – should also be available for most platforms. Thus the core elements of what I demonstrate here for GCC with Ubuntu should be applicable to GCC with other Unix platforms.
The thing that you will have to bear in mind, however, is that the other terminal commands I use (such as apt-get to install software) may not be available on your system. You may have a different terminal, and different package manager which is invoked via a different command and syntax. If you want to follow this tutorial, you’ll have to translate those commands into ones appropriate for your OS. Just remember that the invocation of cmake and make should be the same in most circumstances.
The Video
Read on the for textual tutorial. I suggest that even those of you who prefer videos read this one, as we’ll be working primarily with the terminal: something which is undoubtedly easier to talk about in writing.
What you’ll need
Software
Let’s start be looking at the software we’ll need. Under Ubuntu we can grab all of it via aptitude at the terminal. I’m going to be guiding you through the process of building the SFML 2.0 binaries using GCC, the GNU Compiler Collection. I shall assume you already have this installed (if not, just run sudo apt-get install gcc). We’ll also need something called CMake, which will take some data files that come with the SFML 2.0 source download and use them to generate a makefile which will tell Make (GCC’s make utility) what to build and how to build it. So if you don’t have CMake already, go ahead and install it by typing
sudo apt-get install cmake
at the terminal.
Dependencies
Unlike on Windows or MacOS, we have to hunt down all of SFML’s dependencies ourselves on Ubuntu (this is true for other Linux distributions as well). A complete list of those necessary is available on Laurent Gomila’s tutorial for building SFML 2.0. They are:
- pthread
- opengl
- xlib
- xrandr
- freetype
- glew
- jpeg
- sndfile
- openal
sudo apt-get install libpthread-stubs0-dev sudo apt-get install libgl1-mesa-dev sudo apt-get install libx11-dev sudo apt-get install libxrandr-dev sudo apt-get install libfreetype6-dev sudo apt-get install libglew1.5-dev sudo apt-get install libjpeg8-dev sudo apt-get install libsndfile1-dev sudo apt-get install libopenal-dev
The Source
Finally, we have to get hold of a copy of the SFML source. For this, head over to the SFML download page and click “SFML 2.0 snapshot”. Or just go ahead and download it here. Now, when this file finishes downloading, you can open it up with your archive manger, and extract the contents. The resultant folder should have a name such as LaurentGomila-SFML-xxxxxxx. Laurent Gomila is the name of the developer, SFML, of course, is the name of the library and the xs represent a sequence of numbers and letters which marks the particular version of the source.
This folder name may be useful, but it is also long and unwieldy. Change it to sfml2 or something similar. You can also move it to a new directory if you’d like: for example, I’ll be using /home/<user name here>/Development/ (where Development/ is a folder I created for development files). Take a brief look inside the sfml2 directory. You’ll see various subdirectories such as src (which contains the source code), include (which contains the header files) and extlibs (which contains binaries for external libraries such as OpenAL, though we shall not use these files for the Ubuntu build). There is a license file, a readme and another text file called CMakeLists.txt, which is the root file used by CMake.
Building the Binaries
It’s time to build the binaries! Open a terminal window and switch to our SFML directory, /home/<user name here>/Development/sfml2, or whichever other directory you chose:
cd /home/<user name here>/Development/
Now let’s invoke CMake for the first time.
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=TRUE .
cmake is the name of the CMake executable. The -G switch allows us to specify the generator, that is, the make utility or IDE for which CMake should generate files. In our case, we provide Unix Makefiles as we will be working with GCC Make under a Unix operating system. We then use the -D switch to change some project settings.
CMAKE_BUILD_TYPE can be set to Release or Debug. We choose Release, so the resulting binaries will be compact, with compiler optimisations applied. After, we shall come back and choose Debug so that we can get binaries with debugging information as well. BUILD_SHARED_LIBS is a boolean. If we set it to TRUE, make will yield shared libraries, and if we set it to false we’ll get static ones. We’ve chosen TRUE to get shared libraries, but as with CMAKE_BUILD_TYPE, we’ll come back and get static binaries. Finally, the ‘.’ merely tells CMake to find the files it expects (e.g. CMakeLists.txt) starting in the current directory.
Next we simply need to invoke make to build the binaries from the generated makefiles. And remember, this is the dynamic release build, so we can expect non debug shared libraries.
make
So, as mentioned above, this was the build for the dynamic release libraries.
Now, those binaries have been built, which is all well and good. However, if we want to runtime to be able to find the shared libraries and the linker to be able to find the static (and shared) libraries, then they need to be in an appropriate location, such as /usr/local/lib/. Similarly, it’s nice to put the header files in /usr/local/include/. Now, we could open a file explorer window with sudo and copy and paste everything over manually, but luckily we’re saved this effort. The makefile for make has a target, install, which will automatically move all the files when we build it. Let’s do that now.
sudo make install
Next we’ll change CMAKE_BUILD_TYPE to Debug to get the debug release libraries. We have to re-invoke CMake, and then Make.
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug -D BUILD_SHARED_LIBS=TRUE . make sudo make install
Next, the static release libraries. We’ll change CMAKE_BUILD_TYPE back to Release, but also change BUILD_SHARED_LIBS to FALSE.
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=FALSE . make sudo make install
Finally, we want the static debug binaries, so let’s change CMAKE_BUILD_TYPE to Debug again.
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug -D BUILD_SHARED_LIBS=FALSE . make sudo make install
And we’re done. We now have static debug and release builds and dynamic/shared debug and release builds. Close the prompt, and head over to /home/<user name here>/Development/sfml2/lib/ to have a look at what we’ve got.
SFML has five modules: graphics, audio, window, system and network. Each module has an identical set of binaries, so let’s just take SFML Audio as an example.
There are several links to shared libraries, which we’ll ignore. Then we have two shared libraries:
- libsfml-audio.so.2.0 – the shared release binary
- libsfml-audio-d.so.2.0 – the shared debug binary
- libsfml-audio-s.a – the static release library
- libsfml-audio-s-d.a – the static debug binary
Well, that’s all for today. You’ve now got a full set of SFML 2.0 binaries with which you can start developing with SFML using GCC under Ubuntu. In a future post, I’ll be showing you how to use Code::Blocks to make your first SFML 2.0 project under Ubuntu.
Related articles
- SFML 2.0 Prebuilt Binaries for Download (sfmlcoder.wordpress.com)
- SFML Bindings (sfmlcoder.wordpress.com)
- Tip of the Day – SFML and C++0x (sfmlcoder.wordpress.com)
- Videos on the Way (sfmlcoder.wordpress.com)





I can’t wait for your article about making a SFML 2.0 project with Code::block!
Thanks for the feedback! The post should be out relatively soon as a lot of it is already written.
Great tutorial! I had the libraries all built in 10 mins using this. Very helpful that you included a nice list of commands on grabbing the dependencies too!
Thanks very much – I really appreciate your efforts here, dude
Thank you very much for that instructive tutorial. I got the SFML 2 lib installed really quickly, the problem is that it replaced the SFML 1.6 library I had installed thought the package manager of my distribution (Ubuntu 11.04).
How do I uninstall the lib correctly, and how can I do a proper install alongside the sfml 1.6 lib ? (I have several project using the 1.6 version and I don’t want to break those).
Firstly, the SFML 1.6 files are probably still there. If you installed them with the package manager, you’ll find them in /usr. That is. header files in /usr/include/SFML, libraries in /usr/lib and documentation/examples/etc in /usr/share/SFML. Now, when you installed SFML 2.0 using the makefile, it would have been put in the corresponding locations in /usr/local.
Now, it seems that /usr/local has higher priority than /usr and there’s no easy way to change this. What I’d recommend therefore is to remove your current installed SFML 2.0 files (I’ll tell you how in a minute), and then to install SFML 2.0 again in a different location (I’ll explain this too).
To uninstall SFML 2.0
It’s just a matter of deleting the installed files really. So, you have to delete:
where in the last line, libsfml* represents anything starting with libsfml. Because these files were installed as a superuser (i.e. you used sudo), you’ll need to be a superuser to delete them. So either delete them on the command line using sudo, or if you’re more comfortable with a GUI, then open a terminal and type sudo nautilus /usr/local. That will open up a file browser window in /usr/local with the necessary rights to delete the files. But take care, with these rights, it’ll let you delete anything in there, which could break things. So make sure you only delete the SFML related items I told to to.
Installing SFML Elsewhere
For this, just repeat the my steps from the video/text which you’ve already used to install SFML. But when you use CMake, add one extra option:
cmake -G “Unix Makefiles” etc etc -D CMAKE_INSTALL_PREFIX=/home/user-name/SFML
Then, when you run make install, SFML will be installed to /home/user-name/SFML, not /usr/local (where user-name is replaced by your username). Pick a different directory if you’d like, but something somewhere inside your home directory is recommended.
Note: Now the SFML files will not be in a system location, so you’ll have to provide /home/user-name/SFML/include as a compiler search directory and /home/user-name/SFML/lib as a library search directory. If you are unsure how to do this, let me know what software you’re using for development.
I can get this all to work but I can not get anythign to compile.. how do I set up code blocks now? I thought it would work but I keep getting an error saying the “function” is not part of SF:: ???
This is something I’ve been meaning to post about. Recently the SFML code has undergone some extensive renaming. Now the first letter of each function is no longer capitalised. So for example,
sf::Window::IsOpen()has become
sf::Window::isOpen()There are only a couple changes to get this working with Code::Blocks. Change the cmake -G “Unix Makefiles” to cmake -G “CodeBlocks – Unix Makefiles” then after all of the steps are complete run “sudo ldconfig -v”.
Yes this can equally be done in Code::Blocks – in fact there should be a tutorial somewhere on the site for that. However, if you don’t want to modify the source code it may well be quicker just to use make on the command line so you don’t have to open the IDE or anything.
I think you misunderstood what I said. You don’t have to open the IDE or make any changes to the source to get this to work for the IDE. You can use cmake with Code::Blocks from the terminal.
Should be
cmake -G “Unix Makefiles” -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=TRUE
because I get error like this
http://stackoverflow.com/questions/12887333/cmake-contains-unsupported-character-when-i-try-to-install-sfml-via-terminal
I wasn’t aware that inserting that space could cause problems. I’ll look into the issue further before releasing the revised tutorial.