SDL in CodeWarrior for Mac OS X

Synopsis

This section covers an approach to developing SDL Applications in Mac OS X using Metrowerks CodeWarrior v8. The foundation of the approach is a stationery distribution which is available for download.

Quickstart

Follow these quick steps to get SDL stationery in CodeWarrior:

Background

I didn't think this was going to be so hard to do, so I was very surprised when at the end of the day, I *still* didn't have the most basic SDL code working in CodeWarrior v8 for Mac OS X (10.2.5). Here is what I learned from the experience:

Always update your development environment to the latest version

I can't stress this enough. I *did* update my Windows install of CodeWarrior to the latest version (v8.2 as of this writing), and had no troubles making everything work. But I forgot to do it for the Mac, and the result was that the compiler and linker were not happy with quite a bit. Bringing CodeWarrior up to v8.3 fixed a number of linker problems, and made it possible to use the pre-compiled SDL Frameworks.

SDL needs to be a Cocoa App

I could be wrong about this one, but I sure couldn't find a way to make it work *without* wrapping the SDL window in Cocoa. Writing a simple SDL program as a console application produces hundreds of error messages at runtime, and one of the earliest error messages tells you it can't generate the window. If someone knows how to make it work without wrapping it in a Cocoa framework, please let me know!

GCC and CodeWarrior are not friends

Before the update to v8.3, I couldn't get the linker to work with any libraries that I compiled using GCC. It gave me a 'Mach-O Importer' error in the linking stage and gave up. It doesn't do that anymore, but there are still some things that you can get away with in GCC that CodeWarrior will not accept. For my own projects, this isn't a big deal, but compiling some of the libraries using CodeWarrior is a real nuisance and can require source code changes.

console.stubs.c or console_os_x.c is required

You need to import one of those files, or you'll get link errors like:

Link Error: undefined: 'WriteCharsToConsole' (code) Referenced from '__write_console' in MSL_All_Mach-O.lib Link Error: undefined: 'WriteCharsToErrorConsole' (code) Referenced from '__write_console' in MSL_All_Mach-O.lib Link Error: undefined: 'ReadCharsFromConsole' (code) Referenced from '__read_console' in MSL_All_Mach-O.lib Link Error: undefined: 'RemoveConsole' (non lazy ptr IL) Referenced from '__check_console' in MSL_All_Mach-O.lib

If you want the stdout and stderr to go to the console, you should use console_os_x.c. If you would rather shunt all of that to the void (or use an SIOUX console window), use console.stubs.c.

Instructions

To get SDL working with CodeWarrior (v8.3), follow these steps:

  • Update your installation of CodeWarrior to the latest version.

      Seriously, if you don't do this, you are in for pain. This is especially true if you've recently upgraded the OS to a new major point release (ex: upgrading from 10.1 to 10.2).

  • Install the SDL Developers framework for Mac OS X.

      You can get it from the SDL Website. Note: The developers version is installed in your personal Library/Frameworks directory, not the /System/Library/Frameworks directory where CodeWarrior (and others) is expecting it. Be sure to copy the framework from your personal ~/Library/Frameworks directory to the /System/Library/Frameworks directory.

  • Download the SDL Stationery for CodeWarrior.

The 'SDL Stationery' directory contains all the files to start an SDL project in CodeWarrior v8.3 for Mac OS X. In principle, the directory can be installed in CodeWarrior's project stationery directory and it will be available for use in new projects. In practice, I can't get it to work. If someone figures out what is preventing CodeWarrior from accepting it as official stationery, please let me know!

The files in the project are heavily based on the Project Builder stationery provided with the developers version of the SDL framework. I've done my best to limit changes to the files to necessities to make it work in CodeWarrior, so if further changes are made to the Project Builder stationery are made, I'll do my best to merge them into the files.

As far as I know, the NIB code in SDLmain.m does not work in CodeWarrior, due to some language differences. I haven't tried it in Project Builder, but I assume it works. If someone figures out how to get it working in CodeWarrior, let me know and I'll merge in the changes.

Debugging

Debugging the SDL application requires that you manually copy the application's linked frameworks into a subfolder in the directory created when the debug target is built. For example, if the debug build creates SimpleHello.app, then copies of the SDL frameworks and any others you might be using need to be placed in SimpleHello.app/Contents/Frameworks/. If the Frameworks subdirectory does not exist, you will need to create it.

If the frameworks are not copied into the required directory, when you go to debug the application, CodeWarrior will simply hang and you'll have to Force Quit the development environment. Metrowerks is aware of this bug and they promise it will be fixed in the next release.

A similar problem happens in Project Builder, but in that environment, the debugger fails silently without crashing the rest of the IDE. Unlike CodeWarrior, Project Builder has the ability to copy the required frameworks automatically, so this is really no big deal.

Useful Links