From HaskellWiki
Revision as of 23:58, 17 June 2016 by Henk-Jan van Tuyl (talk | contribs) (Install and execute SDL2 on a Windows computer: Added a link to "Windows SDL2 is now almost painless via stack")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Install and execute SDL2 on a Windows computer

If you want to install SDL2 with stack, see "Windows SDL2 is now almost painless via stack", otherwise, see below.

Note: when you are using GHC 7.10.2 or earlier, using SDL2 will not be possible; you will get the following message when trying to install a package:

<command line>: can't load .so/.DLL for: mingw32.dll (addDLL: could not load DLL)

See GHC ticket 3242

You have to install the C package SDL2 first, then either the Haskell package SDL2 or hsSDL2. See below on how to do this.

Downloading files

Download the precompiled SDL2 package and unzip it to e.g. C:\Libs

What architecture to use

Open a command prompt and type 'C:\Windows\System32\where.exe ghc.exe'. 'where' will return all ghc.exe files in the %PATH% environment variable.

If your version of GHC is installed in 'C:\Program Files', e.g.

$ where ghc
C:\Program Files\Haskell Platform\7.10.3\bin\ghc.exe

that means you are using a 64 bit version of GHC.

If you also have GCC installed and plan on compiling or linking SDL2 C files in the future, you should probably uninstall 64-bit GHC and install the 32-bit version (Unless you also install(ed) 64-bit GCC).

This because GCC and GHC both hapily accept library files for the wrong architecture, to then error because of invalid linkage. This means you can only have one architecture at a time in the %PATH%. You might be able to get around this restriction with some symlinks, but it's really not worth the effort.

Note that SDL, as many others (including GHC and GCC), use a very confusing naming scheme:

  • 32-bit becomes i686-w64-mingw32
  • 64-bit becomes x86_64-w64-mingw32

The rest of this page asumes you want to install SDL2-2.0.4 for a 32-bit environment.

Compiling from source

If you want to compile from source, you will need

If you are using SDL 2.0.3, it is recommended to install the debugged version of SDL_platform.h in the include directory.

Installing SDL2

Put the following in a install-sdl2.bat file, and execute it as a admin.

SET "SDL_ARCH=i686-w64-mingw32"
REM "SDL_ARCH=x86_64-w64-mingw32"

REM SETX /M PKG_CONFIG_PATH "C:\Programs\GTK\lib\pkgconfig\;%PKG_CONFIG_PATH%"
CABAL.EXE install sdl2

Note: You might want make sure all environment variables are correct after installing:

 Control Panel -> System and Security -> System -> Advanced system settings -> Environment variables... -> System variables

If the environment variable did not yet exist (as is usually the case with %LIBRARY_PATH%, %C_INCLUDE_PATH% and %PKG_CONFIG_PATH%), the ';%VARIABLE_NAME%' will be included at the end, which might not be desirable (Although it probably won't do any harm).

If no errors are reported, SDL2 is now installed and you should be able to compile, link and execute your programs.

Execute your program on other computers

In order to ship your SDL2 product, all you will have to do is to include a SDL2.dll from the correct architecture (The one the program is compiled with). You can do this freely by the license of SDL. By placing the DLL in the same directory as the executable, everything should now run as expected.

The Haskell package hsSDL2

Download and unpack the SDL2 development libraries (the .tar.gz files with mingw in the name) from:

Copy the bin, lib, include and share directories from the packages to a common directory, like C:\Libraries. If you downloaded SDL 2.0.3, replace the header file include/SDL_platform.h with the debugged version

Set environment variables like this:

  • To help consistency in the environment variables, we first do:
  • then:

If you don't have git yet, install it. Go to a directory where you want the Haskell library source code installed and fetch the Haskell libraries:

 git clone
 git clone
 git clone
 git clone

If you want to use a cabal sandbox:

 cabal sandbox init

Install the Haskell packages:

 cabal install .\hsSDL2
 cabal install .\hs-sdl2-image
 cabal install .\hsSDL2-ttf
 cabal install .\hs-sdl2-mixer

If you get any of the messages:

 cabal: Missing dependency on a foreign library:
 * Missing (or bad) header file: SDL_mixer.h
 configure: error: *** SDL not found! Get SDL from

you should check if all installation steps were done properly.