Monday, November 17, 2014

How to setup SFML on Ubuntu 14.04 LTS - Update

Hi Folks,

it's been over six months since I wrote the article about compiling and installing SFML on Ubuntu 14.04. On 11.11 (no carnival joke ;-) the SFML Team announced that version 2.2 of the SFML library isn't far off and the team needs us developers to test if the new git commit builds and runs. 
Since I also re-installed my totally messed up development machine with a fresh Ubuntu 14.04, so it is perfect time to write an update for my old article while testing the new SFML build.
Since this is no official release from SFML, but just a git commit, I will incorporate git in the whole process, fetching the master branch from GitHub, compiling and installing it. Here we go...

Since I run this on a totally fresh Ubuntu installation, I start by creating some folders (to open a terminal hit ctrl+alt+t): 
~$ mkdir development
~$ cd development
~/development$ mkdir projects
~/development$ cd projects
~/development/projects$

I create the directory development in my home directory, and within that a directory for my projects. In the project-folder, I want to clone the SFML master branch from GitHub. In order to do this I need git, and because we need a compiler and cmake to actually build SFML, I install the following development tools:

sudo apt-get update
sudo apt-get install g++ cmake git

Now I clone the SFML repo:
~/development/projects$ git clone https://github.com/LaurentGomila/SFML.git

Now we could start to build SFML, but since this is a fresh installation we need all dependencies first. The official tutorial on compiling SFML lists the needed packages, and here are the actual package names for Ubuntu 14.04:

libx11-dev
freeglut3-dev
libjpeg-dev
libfreetype6-dev
libxrandr-dev
libglew-dev
libsndfile1-dev
libopenal-dev
libudev-dev

To install them all, run the following command: 
sudo apt-get install libx11-dev freeglut3-dev libjpeg-dev libfreetype6-dev libxrandr-dev libglew-dev libsndfile1-dev libopenal-dev libudev-dev

After this step I am able to configure and run the build with the following steps:

Switch to the SFML directory, in my case ~/development/projects/SFML/
and run the following commands:
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug -D BUILD_SHARED_LIBS=TRUE .
make
sudo make install

Note the dot at the end of the cmake-command, which tells cmake to use the actual directory as a working directory. There are only two relevant cmake switches, CMAKE_BUILD_TYPE (either Debug or Release) and BUILD_SHARED_LIBS (TRUE for dynamic linking, FALSE for static linking). In order to build all four configurations, you have to run the above three commands four times while alternating the two switches.

In order to link the SFML libraries in your projects, add them to the linker options in your project, e.g. in Code::Blocks:



After that you are finally able to develop, build and run SFML apps on Ubuntu 14.04 with the latest master build of SFML from GitHub.





Wednesday, May 28, 2014

How to setup SFML on Ubuntu 14.04 LTS


Edit: There is an updated version of this article available here
================================================


Hi Folks,

in one of my first posts about game development I said I would show you how to setup a SFML development environment on Linux. Now it's time to fulfill this promise ;)

At first I want to limit the Linux distro to Ubuntu, 14.04 LTS in particular. I've only tested it on Ubuntu because I don't use any other distro anymore. Ubuntu rocks them all ;) (personal taste). Because Ubuntu is based on Debian, this article should also apply to Debian, although I haven't tested it.
The problematic part about Ubuntu 14.04, the official SFML version in the repos is 1.6, and we don't want this old one. We want the actual 2.1 stable release. There are also some more version problems with SFML dependencies, especially with GLEW. But this means we need to build it from the official source code ourselves with all dependencies solved, so everything works fine. Ok, let's do it!

After a clean Ubuntu 14.04 installation, first thing we need is a compiler. I think GCC will be sufficient enough ;) We also need the meta build tool cmake in order to build SFML. Open a terminal (ctrl+alt+t) and enter the following commands

sudo apt-get install g++
sudo apt-get install cmake



After this we need all dependencies of SFML solved. Enter the following commands (order doesn't matter)

sudo apt-get install freeglut3-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libfreetype6-dev
sudo apt-get install libxrandr-dev
sudo apt-get install libglew-dev
sudo apt-get install libsndfile1-dev
sudo apt-get install libopenal-dev


The important part is, that we always need the developer version of these libraries in order to build SFML. Now we only need the SFML source code which can be downloaded here: http://www.sfml-dev.org/download/sfml/2.1/SFML-2.1-sources.zip

Extract the zip-file to some directory, e.g. ~/dev/sfml-master. Now we need to configure the makefiles with cmake. Switch to the extraction directory of SFML and enter the following:

cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug -D BUILD_SHARED_LIBS=TRUE .
make
sudo make install

Don't forget the dot at the end of the cmake command. This tells cmake to use the actual working directory as source directory. Until now we only have the shared debug lib, but we also need the release libs. Therefore simply replace the CMAKE_BUILD_TYPE=Debug with Release:
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=TRUE .
make
sudo make install

Finally if you want to link your SFML apps statically you need the static libs. Therefore repeat to two above steps for Debug and Release and change BUILD_SHARED_LIBS to FALSE:
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug -D BUILD_SHARED_LIBS=FALSE .
make
sudo make install

cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=FALSE .
make
sudo make install

Now you have covered all four possibilities of the libraries and you can now develop SFML projects the way you like.
You can now start a new project in e.g. Code::Blocks (do not use the SFML-Project Template shipped with Code::Blocks, it contains legacy SFML-Code). Create a new console project and goto Build Options -> Linker Settings and add the SFML libraries to the Linker Settings:



If you need networking or audio don't forget to add these here as well (sfml-audio-d, sfml-audio, sfml-network-d, sfml-network). That's all, you are now able to write, compile & run SFML 2.1 driven applications on Ubuntu 14.04.

Cheers until next time.