Porting SDL applications to OS/2

From EDM2
Jump to: navigation, search


To port SDL applications to OS/2, you'll need the latest SDL binaries and header files from netlabs.org. Currently the OpenWatcom compiler is the compiler for the OS/2 port of SDL, but it should also work with GCC.

Using the OpenWatcom compiler

There are some requirements and tricks with using the OpenWatcom compiler to port SDL applications. You should note the followings things.

Use the -ei switch for the OpenWatcom C compiler

You have to use the -ei switch for the OpenWatcom C compiler to have the size of enums equal to the size of ints. This is a requirement of SDL itself.

Use the -5s switch for the OpenWatcom C compiler

Starting with the build of 2005.03.30. of SDL/2, the calling convention has been changed to be stack-based (switch -5s) instead of the previous register-based one (switch -5r or simply -5).

Although the register-based version was faster (in theory), the calling convention has been changed to be compatible with GCC, so people using GCC can use SDL.DLL without problems, even where SDL calls into the GCC code (e.g. audio mixer callback), because now both of them use the very same convention.

So, don't forget to use the -5s switch for the OpenWatcom C compiler if you use that to compile your SDL application!

Wrapping the SDL_Quit() function

The OS/2 version of SDL is compiled into a DLL file. This DLL file has all the SDL functions exported by name. In order to be able to use this DLL from most of the available compilers, these exports are using the _Syscall calling convention. This leads to problems with some SDL apps, where they assume that the calling convention of the API is the same as the calling convention of their runtime library. For example, the following line is very usual in SDL applications:


This should be worked around by creating a wrapper function for SDL_Quit, and using that one, this way:

#ifdef __WATCOMC__
void SDL_Quit_Wrapper()
#ifdef __WATCOMC__

Using the GNU C Compiler

It was reported that some (or maybe all) of the GNU C compilers (GCC) available for OS/2 can not handle the SDL.LIB file included in the SDL DevPack. The problem is that the SDL.LIB file was created directly from the SDL.DLL file, using the IMPLIB.EXE tool from the OS/2 Developer's Toolkit, and the GCC does not recognize it to be a valid library file.

There is a workaround, which is reported to work in this case. One has to create a GCC-compatible library files (.a .lib), using the EMXIMP tool, and link against that file:

emximp -o sdl.imp sdl.lib
mv sdl.lib sdl.lib.owc
emximp -o sdl.a sdl.imp
emximp -o sdl.lib sdl.imp

Don't forget to use the correct EMXIMP tool distributed with the libc that you are using.

If you plan on using SDL's multithreading capabilities (e.g. if you want to include "SDL_thread.h") with the older EMX libc, be sure that enable multithreading with -Zmt on your GCC command line, otherwise you'll get an error because _beginthread() and _endthread() will not be defined. This is not needed with kLIBC as all the libraries are multithreaded but is harmless if used (silently ignored)

General tips and tricks

Configure scripts and SDL

For configure scripts to find and use SDL you need to have a sdl-config script in your path and/or a pkg-config file in your %PKG_CONFIG_PATH%, eg set PKG_CONFIG_PATH=i:/usr/local/lib/pkgconfig;%PKG_CONFIG_PATH%

For example this is the sdl-config I use (note static linking does not work.) Adjust prefix to reflect your setup.

Usage: sdl-config [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--cflags] [--libs] [--static-libs]"
if test $# -eq 0; then
      echo "${usage}" 1>&2
      exit 1
while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  case $1 in
      if test $exec_prefix_set = no ; then
      echo $prefix
      echo $exec_prefix
      echo 1.2.7
#      echo -Zomf
       echo -I/usr/local/include/SDL
      # The portable way of including SDL is #include "SDL.h"
      #if test ${prefix}/include != /usr/include ; then
      #    # Handle oddities in Win32 path handling (assumes  prefix)
      #    prefix=`echo ${prefix} | sed 's,^//\([A-Z]\),\1:,'`
      #    includes=-I${prefix}/include
      #echo $includes -I/usr/local/include/SDL  -Dmain=SDL_main
      echo $libdirs -L/usr/local/lib -lSDL 
#    --libs|--static-libs)
      libdirs="-L${exec_prefix}/lib "
      echo $libdirs -lmingw32 -lSDLmain -lSDL -mwindows   -luser32 -lgdi32 -lwinmm -ldxguid
      echo "${usage}" 1>&2
      exit 1

And this is an example pkg-config file, sdl.pc. Adjust prefix to reflect your install.

# sdl pkg-config source file


Name: sdl
Description: 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.
Version: 1.2.13
Libs: -L${libdir}  -lSDL 
Libs.private: -lSDL   -lm
Cflags: -I${includedir}/SDL -D_GNU_SOURCE=1

Configure can't link SDL_Init

Some configure scripts fail to detect SDL as the configure script creates a program linked to SDL then fail with an error related to not finding _SDL_Init (look in config.log)

One workaround is to create the import libs this way

emximp -o SDL12.def SDL12.dll

Then edit the def adding "_SDL_Init"="SDL_Init" just below the SDL_Init line. The run

emximp -o SDL12.a SDL12.def
emximp -o SDL12.lib SDL12.def

To create your import libs. Adjust names as needed.

Convert your program to a PM program

After compiling running emxbind.exe -e -p execname.exe will convert your program from windowed to PM. A window will open then vanish as your program becomes PM. Stdout can be captured with foo > out.log and Stderr (error messages) can be captured with foo 2> error.log.

Use a DEF file at linking time

If during the linking stage you supply the linker (either LD or link386) with a DEF file the program will become a PM app while still leaving the console open and displaying messages. A simple DEF file can just have the line


Where foo will be the window title. Of course replace foo with the program name. The EMX docs has more info on what can go into a DEF file e.g. description

Morph your program to a PM program at runtime

Most SDL applications assume that they can write to STDOUT, and the user will see it. In OS/2, you have to compile your application to a PM application in order to be able to have a PM Window, but the STDOUT will go to NULL in this case. The solution is to compile your code to a VIO application, and morph the application to a PM one dynamically, at runtime, before calling SDL_Init().

Once you have your text output back, you might use it for showing debug messages. If you do so, it's a good idea to make the STDOUT and STDERR unbuffered, so the debug messages will be shown right at the time when the printf() is called, so every message will be visible even in case of a crash.

Here is an example code on how to morph your VIO application to a PM one dynamically, at runtime, and how to set STDOUT and STDERR unbuffered:

#define INCL_DOS
#include <os2.h>
#include <stdio.h>


void MorphToPM()
  PPIB pib;
  PTIB tib;

  DosGetInfoBlocks(&tib, &pib);

  // Change flag from VIO to PM:
  if (pib->pib_ultype==2) pib->pib_ultype = 3;


int main(int argc, char *argv[])
  MorphToPM(); // Morph the VIO application to a PM one to be able to use Win* functions

  // Make stdout and stderr unbuffered
  setbuf( stdout, NULL );
  setbuf( stderr, NULL );
  /* Initialize SDL */
  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
     fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());