Setting up MSYS/MinGW build system for compiling SDL/OpenGL applications

1. Introduction

MSYS is a Minimal SYStem to provide POSIX/Bourne configure scripts the ability to execute and create a Makefile used by make on MS Windows operating system.
MinGW is a collection of freely available and freely distributable Windows specific header files and import libraries combined with GNU tool sets that allow one to produce native Windows programs that do not rely on any 3rd-party C runtime DLLs.
In this tutorial we will setup MSYS/MinGW configuration, and compile libraries needed for development of SDL and OpenGL applications.

2. Installation

2.1. MSYS Installation

First, you need to download MSYS setup exe file, and execute it. Once you have started setup, accept the license and select the folder where MSYS will be installed (or leave it on C:\msys\1.0, which is the default installation location). When installation is finished, MSYS will start post-install script in console window, which will try to detect if there is a previous installation of MSYS on your system (just enter y here), and then it will ask if you have MinGW installed (enter n here, because MinGW will be installed later). Follow the instructions presented, and installation will finish.

Now you have basic Bourne shell on your Windows system, with some standard UNIX-like utilities (make, diff, awk, etc). You can start MSYS shell from Start->Programs->MinGW->MSYS menu.

Next you need to download and install msysDTK. The msysDTK package is MSYS Developer Tool Kit, which contains the following list of tools: autoconf, automake, libtool, guile, cvs, openssl, openssh, inetutils, perl and vim. Similar to MSYS, the download is an installation binary. It will default the directory location to your MSYS installation.

2.2. MinGW Installation

Download the most recent MinGW setup, in the time of writing this, it is version 5.0.2. This setup will download and install components of MinGW package. It will present you a list of SourceForge mirror sites, and an option to download only or download and install MinGW components. Next, choose Current as a type of MinGW package, and select components to be installed. At least, choose MinGW base tools, which will install a GNU C compiler, binutils, and Win32 API header files for you. Other components you can choose are g++ compiler (GNU C++ compiler), g77 compiler (fortran), Ada, Java and Objective C Compiler. Next, you need to select a destination folder for component installation. Since we already installed MSYS, you can choose mingw folder in MSYS installation folder for destination folder.

If you choose some other folder, you will need to modify /etc/fstab file under MSYS, and map a MinGW installation folder to /mingw MSYS folder. For example, if you installed MinGW in C:\mingw, you will need to add the following line to /etc/fstab file:
C:/mingw /mingw

3. SDL compilation

3.1. Introduction

Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer.
We will download source version of SDL library, and compile it under MSYS/MinGW. There are also precompiled versions of library for MinGW, but if you compile it yourself, you have a control over library configuration and compilation.

3.2. Preparation

Download SDL library source code (the .tar.gz archive) and put it in your MSYS user home directory. The current library version in the time of writing is SDL 1.2.11.

Note: After installing MSYS, setup creates user home folder with the name of system user name, under the MSYS /home folder. You can access it from Windows Explorer from {msys_instal_dir}\home\{windows_user}. When you start MSYS shell, working directory is set to user home directory.

Extract the content of the archive and enter SDL directory:

$ tar -xzvf SDL-1.2.11.tar.gz
$ cd SDL-1.2.11

3.3. Configuring SDL

SDL library compilation can be configured with many options. Configure script examines the capabilities of system, and tries to adapt the library configuration. Some options can be controlled by user. For a list of available configuration options and their default values, run:

$ ./configure --help

One option that you will need to set is –prefix=PREFIX option. This options set installation directory for the library. Under MSYS, we need to set this option to /mingw directory, so that include files and compiled library is installed in compiler directory.

$ ./configure --prefix=/mingw

Some options control what parts of SDL subsystem will be compiled into the library:

--enable-audio Enable the audio subsystem [default=yes]
--enable-video Enable the video subsystem [default=yes]
--enable-events Enable the events subsystem [default=yes]
--enable-joystick Enable the joystick subsystem [default=yes]
--enable-cdrom Enable the cdrom subsystem [default=yes]
--enable-threads Enable the threading subsystem [default=yes]
--enable-timers Enable the timer subsystem [default=yes]
--enable-file Enable the file subsystem [default=yes]
--enable-loadso Enable the shared object loading subsystem [default=yes]
--enable-cpuinfo Enable the cpuinfo subsystem [default=yes]

If you do not need some SDL subsystems, you can configure compilation to exclude it, by passing parameters to configure script. For example, if you do not need the support for joystick and cdrom subsystem, you can configure SDL with:

$ ./configure --prefix=/mingw --enable-joystick=no --enable-cdrom=no

Another option that might be interesting is --enable-stdio-redirect, which is enabled by default on Win32 systems. This option enables redirection of standard console output to files stdio.txt and stderr.txt. If this option is enabled, every program output (using functions like printf in C, for example) is redirected to these files. If you don’t want these files to be created when your SDL program is started, you can disable this option:

$ ./configure --prefix=/mingw --enable-stdio-redirect=no

You can control C compiler options by setting CFLAGS environmental variable before configuring library compilation. Also, you can set linker flags by setting LDFLAGS variable. Be carefull, setting some compiler options can cause serious compilation or execution problems, so, before setting options, be sure that you know what you are doing. You set environmental variables by using export shell command. For example, to set compiler optimization for size, type:

$ export CFLAGS=-Os

If you don’t want to make any customizations to SDL configuration and leave all options to their default values, than just configure it with the following command:

$ ./configure --prefix=/mingw

3.4 Compiling SDL

Now that SDL library is configured for compilation, you can proceed with compilation and installation.

$ make
$ make install

After that, you will have installed a working version of SDL library, and header files.

3.5 Testing compiled SDL library

SDL library provides testing examples in test subdirectory, which you can use to test various features and capabilities of an SDL library. To compile test examples:
$ cd test
$ ./configure
$ make

When make process is finished, you can start examples from MSYS shell, by typing the name of test exe file. If you want to start example from Windows, you will need to copy exe file with the SDL.dll library, which can be found under /mingw/bin directory, to some separate directory and start it from there.

Note: You can also copy SDL.dll library in windows\system32 directory, and in that case, all the applications that use SDL will use that copy of the library, which might lead to some problems, depending of the options you used during compilation…

Note: Almost every SDL example gives some output, so depending of options you specified during the SDL library configuration, you can see that output on console, or saved in stdio.txt file.

4. Conclusion

This procedure will get you complete system for configuring and compiling SDL applications, and you should be able to compile existing SDL application, or write your own…

7 Responses to “Setting up MSYS/MinGW build system for compiling SDL/OpenGL applications”

  1. sENSEr says:

    It’s a very good, and comprehensive artice ( tutorial ). Keep up the good work!

  2. Tom says:

    Hi.
    I get this error with SDL version 1.2.13 :

    ./src/video/windib/SDL_dibvideo.c: In function `DIB_SetColors’:
    ./src/video/windib/SDL_dibvideo.c:951: error: `SYSPAL_NOSTATIC256′ undeclared (first use in this function)

    And it seems I’m not the only one. Googling for “SYSPAL_NOSTATIC256 undeclared”, check the ‘Cached’ one, or:
    http://66.102.9.104/search?q=cache:JOWsRh7RiGAJ:forums.codeblocks.org/index.php%3Ftopic%3D7619.0%3Bprev_next%3Dprev+SDL_dibvideo.c+SYSPAL_NOSTATIC256+undeclared&hl=en&ct=clnk&cd=1&client=firefox-a

    I think I’m gonna ask somewhere else.
    Tom

  3. montyx says:

    Hi,
    Need update win32api for mingw32.
    SYSPAL_NOSTATIC256 declared in wingdi.h

    Sorry, my english is too bad.

  4. alban says:

    To Toms:

    Just add the following sentence into”/msys/1.0/msys.bat” :

    call “D:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32.bat”

  5. Andy says:

    Thanks for “–enable-stdio-redirect=no” .. you made my day.

  6. John Montgomery says:

    Thanks for that – it was really helpful. Got SDL compiled and understood Msys/MinGW setup a bit better.

    I tried to give you rating 10/10, but mysql broke, and the average decreased. Tried again and it decreased more!!

    So, the ratings is very wrong.
    :-)
    John.

Leave a Reply