Home > Introductory, SFML, Tutorial > Building SFML 2.0 with MinGW Make

Building SFML 2.0 with MinGW Make

In this tutorial I’m going to show you how to build the SFML 2.0 binaries from the source using MinGW Make, that is the MinGW make utility.

The Video

Read on for the textual tutorial. Even if you watch the video, it’s worth a look at the text as we will be using console commands.

What you’ll need

There are just a few files you’ll need to download to complete this tutorial – but don’t worry. It’s all free!

Software

In this tutorial, I’ll be showing you how to build the binaries for use with MinGW (e.g. for use with Code::Blocks and its default compiler). We’ll be using MinGW Make, the MinGW make utility, but don’t worry as it comes with MinGW or Code::Blocks with MinGW (for Windows).

The only thing you’ll need apart from MinGW is CMake: the utility which will create our makefiles. But it’s absolutely free, so just get the appropriate version from the download page. If necessary install it.

The SFML Source

It’s time to download the SFML source itself. You can find it on the SFML downloads page, but for the textual tutorial, I may as well give you the link directly. So click here to get the latest version of the SFML 2.0 source.

Getting ready to build SFML

There are just a few things to do before we get started with our build operations, in order to make things easier for us later.

Moving the Source Folder

The SFML download is a .tar.gz file. So you’ll need an archive manager such as 7-Zip to open in. Extract it once to yield a .tar file, and then again to get an actual folder with a name such as LaurentGomilla-SFML-xxxxxxx where the xs will be some sequence of numbers and letters representing the particular SFML version.

Move this folder to a more convenient location such as c:/sfml, where c: is your root drive. Then rename the folder to sfml2. Now all your SFML files should be in the folder c:/sfml/sfml2, where they can be easily found.

The SFML 2 Root Directory

The SFML 2 Root Directory

Configuring Environment Variables

We’re going to need to invoke CMake and MinGW Make from the command line. We don’t want to have to qualify their executables with the full directory paths, so we should like to add their binary directories the the system’s environment variable PATH.

Locate your CMake binaries – they will probably be in c:/program files (x86)/cmake-x.x/bin if you used the windows tutorial. As this tutorial focuses on the build operation, I shan’t go through the process of changing environment variables. Here is an excellent tutorial on the subject (for Windows). I suggest that you do not put quotes around the path.

Then locate your MinGW binaries – they will probably be in a directory such as c:/mingw/bin or c:/program files (x86)/mingw/bin if you obtained MinGW separately) or c:/program files (x86)/codeblocks/mingw/bin if you got it with Code::Blocks.

The Build Operation

It’s time to run CMake. So start your command line interface, such as cmd.exe.

Then change the active directory to your SFML directory, which will be c:/sfml/sfml2 if you followed my instructions.

cd c:/sfml/sfml2

Next, we need to run CMake. We have to pass some arguments to it, to specify the generator (the build utility for which we want makefiles – here this is MinGW Make), to configure some project settings and to provide the source directory in which there is a file CMakeLists.txt to instruct CMake. Now, the first CMake command. Take care to enter this correctly, with the same capitalisation and whitespace as I use.

cmake -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=Release
-D BUILD_SHARED_LIBS=TRUE c:/sfml/sfml2/ .
Running CMake on the Command Line

Running CMake on the Command Line

cmake is the name of the CMake command line executable. We use -G to specify the generator: MinGW Makefiles (i.e. files to be used by MinGW’s make utility). We then use -D twice to set two project settings.

CMAKE_BUILD_TYPE marks whether we release libraries, or debug ones (with debug information!). BUILD_SHARED_LIBS signifies whether we get shared and import libraries, or static libraries. Setting it to TRUE yields shared libraries. Shared libraries are loaded at runtime – they are DLL files – whereas static ones are combined with our code during linking. Import libraries are files which we can link to our project to provide easy access to the functions and so forth in DLL files.

Finally, we give the directory path where CMake will find the data files it needs. CMake will now run for a while, creating the makefile. When this is done, assuming everything goes to plan, it is time to actually build the SFML binaries. CMake has done most of the work. To finish, just invoke MinGW make, the MinGW build utility:

mingw32-make

Running MinGW Make on the Command Line

Running MinGW Make on the Command Line

Once MinGW make is done building, we already have the shared release versions of the SFML binaries. Now we have to repeat the last two commands for the shared debug binaries as well as the static release and debug static libraries. Type this line next:

cmake -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=Debug
-D BUILD_SHARED_LIBS=TRUE c:/sfml/sfml2/

We have simply changed the build type to Debug. Then invoke MinGW make again to build the binaries.

mingw32-make

Next, the release static libraries: we change the build type back to Release, but change BUILD_SHARED_LIBS to FALSE.

cmake -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=Release
-D BUILD_SHARED_LIBS=FALSE c:/sfml/sfml2/

Run MinGW make:

mingw32-make

Finally, we change the build type to Debug again for the static debug libraries.

cmake -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=Debug
-D BUILD_SHARED_LIBS=FALSE c:/sfml/sfml2/

And run MinGW make for the final time.

mingw32-make

When the build is complete, you can now close the command prompt. In fact, we are now done building. However, let’s just go to the folder c:/sfml/sfml2/lib (it will be different if you put SFML elsewhere) and have a look at what you find.

There are a load of files in there, but we only care about the *.dll dynamic libraries and the *.a static and import libraries.

SFML has five modules: graphics, audio, window, system and network, and there are libraries for each one. Since each module has an identical set of files, let’s just look at audio binaries, for example. There are four *.a files:

  • libsfml-audio.a – the import library from the dynamic release build
  • libsfml-audio-s.a – the static library from the static release build
  • libsfml-audio-d.a – the import library from the dynamic debug build
  • libsfml-audio-s-d.a – the static library from the static debug build
There are also two DLL files:
  • sfml-audio-2.dll – the dynamic library from the dynamic release build
  • sfml-audio-d-2.dll – the dynamic library from the dynamic debug build
The SFML 2 Binaries Directory

The SFML 2 Binaries Directory

Naturally, there aren’t any DLLs for the static builds. Anyway, that’s all for today. You now have all the binaries you need to develop with SFML and MinGW. Perhaps now would be a good time to read my First SFML Project tutorial, if you haven’t already.
About these ads
  1. Michael
    27.03.2012 at 16:52

    Hello, nice tutorial but I have got troubles. When I write in command line:
    cmake -G “MinGW Makefiles” -D CMAKE_BUILD_TYPE=Release
    -D BUILD_SHARED_LIBS=TRUE c:/sfml/sfml2/
    there is an error like this:
    http://imgur.com/pD395
    Environment variables are OK.
    I’m compilling via cmd simple programs in c and c++ and it works fine.
    Could you help me ?

    • 02.04.2012 at 08:35

      Well CMake says that your compiler isn’t working. How exactly did you install the compiler?

      • Michael
        03.04.2012 at 11:43

        I downloaded and installed MinGW by the latest installer. At the instalation I check “Download latest repository catalogues”. Folder is C:/MinGW.

        • 06.04.2012 at 09:25

          You say your environment variables are “OK”. So I assume “C:\MinGW\bin” is in %PATH%?

          • Michael
            06.04.2012 at 12:53

            Yes, of course, I have no idea why it doesn’t work.

        • 06.04.2012 at 13:48

          One idea – write a simple C code file, just a hello world program or something:

          #include <stdio.h>
          main()
          {
          printf("Hello World");
          }

          Then try compiling it:

          gcc test.c -o test.exe

          See what output you get, and if the test.exe program exists and/or works. You may as well also write a C++ hello world program and try

          g++ test.cpp -o test.exe

  1. 06.07.2011 at 13:19
  2. 02.08.2011 at 10:28
  3. 23.12.2011 at 14:42

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 77 other followers

%d bloggers like this: