Using Gambit Scheme On Windows With SDL And OpenGL
Sep 18, 2010
5 minute read

As you can see from past posts, I usually develop software within a GNU/Linux environment. However, I do recognize the importance of being able to target Windows for binaries. Here’s a quick rundown of what you need to do to compile a quick test using Gambit Scheme, OpenGL, SDL and SDL_image on Windows 7.

Step 1: MinGW

Download mingw-get-inst-20100909.exe from here (look in the Automated MinGW Installer\mingw-get-inst\mingw-get-insta-20100909 sub-folder). When running the installer, I used the “Use pre-packaged repository catalogues” option, put the files in C:\MinGW and insalled these components: C Compiler, C++ Compiler, MSYS Basic System and MinGW Developer ToolKit. Maybe other ways to setup MinGW work – I’m not a MinGW expert.

This gets you a basic gcc setup from which you can compile software in Windows. It also comes with a shell to make you feel a little more at home – if you’re used to Unix, that is. Gambit needs gcc to create binaries too.

Step 2: Gambit Scheme

Get the installer from Gambit’s main page. Install the software with default settings. Quick and painless.

The next few steps install dependencies for SDL and SDL_image.

Step 3: ZLib

Download zlib125.zip source code from the website. Unzip the source code, open up a MinGW shell (accessible from the Windows start menu), “cd” into the source directory and execute:

$ make -f win32/Makefile.gcc

After the compile is done, copy the static libraries to user local from within the MinGW shell:

$ cp -iv zconf.h zlib.h /usr/local/include/

`zconf.h' -> `/usr/local/include/zconf.h'
`zlib.h' -> `/usr/local/include/zlib.h'

$ cp -iv libz.a /usr/local/lib
`libz.a' -> `/usr/local/lib/libz.a'

Step 4: Libpng

Download libpng source code from the website. Make sure to get the .tar.gz version so that the configure scripts are included (libpng-1.4.3.tar.gz). In a MinGW shell, extract the source code, “cd” into the directory and execute:

$ configure --disable-shared --enable-static CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"

This builds only static libraries, which is what we’ll use for now. This way you do not have to mess with making sure DLLs can be found. The CFLAGS and LDFLAGS setting help the configure script find zlib and other libraries we will be building.

Complete the build by executing:

$ make

and then

$ make install

Step 5: JPEG library

Download the jpegsrc.v8b.tar.gz source code package from the website. In a MinGW shell, extract the source code, “cd” into the directory and execute:

$ configure --disable-shared --enable-static CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"

Again, we’re only building the static libraries. Follow this up with:

$ make

and

$ make install

Step 6: SDL

Download the SDL-1.2.14.zip source code from the website. In a MinGW shell, extract the source code, “cd” into the directory and execute:

$ configure --disable-shared --enable-static CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"

Again, follow this up with:

$ make

and

$ make install

With SDL installed, you should be able to execute the “sdl-config” program in the shell to access what flags to use while compiling software. With what I’ve built so far, here’s the output of sdl-config for me:

$ sdl-config --cflags
-I/usr/local/include/SDL -D_GNU_SOURCE=1 -Dmain=SDL_main

$ sdl-config --libs
-L/usr/local/lib -lmingw32 -lSDLmain -lSDL -mwindows -liconv -lm -luser32 -lgdi32 -lwinmm

$ sdl-config --static-libs
-L/usr/local/lib -lmingw32 -lSDLmain -lSDL -mwindows -liconv -lm -luser32 -lgdi32 -lwinmm

Step 7: SDL_image

Download SDL_image-1.2.10.zip source code from the website. For my quick test, I’m only going to enable the png and jpeg support libraries (built in steps 4 and 5 above). This means the tiff format will not be supported, but if this is important to you there is a library for that as well. In a MinGW shell, extract the source code, “cd” into the directory and execute:

$ configure --disable-shared --enable-static --enable-jpg --enable-png CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"

followed by

$ make

and

$ make install

Step 8: Test Time!

Use the code from this post of mine (download from here). Save the test-03.scm and water-128x128.png files to a directory, run a MinGW shell, “cd” to this directory and compile the scheme source code with the Gambit compiler like this:

$ gsc -cc-options "-I/usr/local/include/SDL/" -ld-options "-L/usr/local/lib/ -lmingw32 -lSDLmain -lSDL -mwindows -liconv -lm -luser32 -lgdi32 -lwinmm -lopengl32 -lSDL_image -lpng -ljpeg -lz" -exe test-03.scm

This should produce a “test-03.exe” file that has all of the files statically linked.

Now, there are legal ramifications of static linking SDL, SDL_image and other LGPL projects. Essentially, you have to provide a way for the user to relink your project which essentially means you cannot use it in closed source applications. The fix for that is to dynamically link the software.

Zlib, libpng and libjpeg can all be linked statically without any repercussions per their respective licenses. This means we just have SDL and SDL_image to worry about.

Within the MinGW shell, change directories to the SDL library source code and run:

$ make uninstall

$ make clean

$ configure --enable-shared --disable-static CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"

Copy SDL.dll from C:\MinGW\msys\1.0\local\bin\SDL.dll to the test directory where test-03.scm is compiled.

For the SDL_image library, within the shell, change directories to the SDL_image library source code and run:

make uninstall

make clean

$ configure --enable-shared --disable-static --enable-png --enable-jpg CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"

Copy SDL_image.dll from C:\MinGW\msys\1.0\local\bin\SDL.dll to test directory. This DLL depends on SDL.dll which was already copied.

Now recompile the code with gambit using a simplified command:

$ gsc -cc-options "-I/usr/local/include/SDL/" -ld-options  "-L/usr/local/lib/ -lmingw32 -lSDLmain -lSDL -lopengl32 -lSDL_image" -exe  test-03.scm

This should produce a slightly lighter executable (2.65 MB stripped, 3.29 MB unstripped) that only needs the SDL and SDL_image DLL files next to it.