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.
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
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.
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:
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
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.
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.