How to Install SDL Under MinGW on Netadelica
Introduction
SDL is great and it lives here. But if you're not already a guru, it can be difficult to install, and there aren't many if any step-by-step instructions for getting it working. So, if you're struggling to install it under MinGW, this page might be helpful.
Installing SDL
These are the steps I took to get SDL 1.2.9 running, starting with MinGW and MSYS already installed, and using Windows 2000.
Download and unpack SDL
- On the SDL Web site, go to the download section for the current version (1.2.9 at the moment).
- You're given a choice of Source Code, Runtime Libraries, or Development Libraries. Assuming you want to write programs that use SDL, and you just want to get up and running without being bothered compiling the source code yourself, you need the Development Libraries bit of the page.
- You're installing it under MinGW, so download SDL-devel-1.2.9-mingw32.tar.gz (assuming 1.2.9 is the current version).
- Unzip and unpack that file into a temporary directory on your PC. You can delete the directory when you've finished installing. Let's assume you've unpacked the file into /temp/. You'll need something that can handle .gz and .tar, but that's probably in MinGW/MSYS already. Personally I use WinNavigator which deals with them nicely.
Install SDL files for the compiler
- Find your MinGW directory, the one with the bin, doc, include and lib directories in it. Let's assume it's /mingw/
- Create a new directory to put the SDL header files in: /mingw/include/SDL/
- Copy all files (30 of them in SDL 1.2.9) in /temp/SDL-1.2.9/include/ to this new directory.
- Copy all files (4 of them in SDL 1.2.9) in /temp/SDL-1.2.9/lib/ to /mingw/lib/
- Find your MSYS directory, the one with the bin, doc, etc, and home directories in it. Let's assume it's /msys/
- Copy the file /temp/SDL-1.2.9/bin/i386-mingw32msvc-sdl-config to /msys/bin/sdl-config (note that you're renaming the file as well as copying it). I chose /msys/bin/ because you can be sure that anything in there will always get executed regardless of which directory you're currently in. When you compile an SDL program you need to execute the file sdl-config on the compilation line (more below).
Copy the SDL DLL and tidy up
- Copy the file /temp/SDL-1.2.9/bin/SDL.dll to c:/WINNT/system32/. As that directory will be in your PATH, it ensures that any SDL program you write will find the DLL and you can keep just one copy on your disk. If your Windows drive is something other than c: then change accordingly.
- Delete /temp/SDL-1.2.9/
You should now have a working SDL installation and you can write some code.
Your First SDL program
Here's a small program to get started and to test everything's working.
#include <stdio.h>
#include "SDL/SDL.h"
// include the SDL headers you put in /mingw/include/SDL/
int main(int argc, char *argv[]) {
// with SDL, you need the argc and argv parameters
printf("Hello world\n");
// with SDL, anything you printf will get printed to the
// file stdout.txt in the current directory, not to the screen
}
How to Compile an SDL program
To compile an SDL program, use this:
gcc filename.c `sdl-config --cflags --libs`
Note that you use backticks, not normal apostrophes. The file sdl-config will get executed (which is why you copied it to /msys/bin/ so that you can be sure that it will get executed) and will pass its output to gcc, in a sort of magical ritual that I don't understand.
If you compile the above program, then you should see no error messages or any other output from the compiler. You should be left with an exe file in the current directory (a.exe by default) which when run will create stdout.txt and write "Hello world" to it. To learn how to do graphics and that, see the SDL Web site.
Back to the Netadelica home page
1 Sep 2005