# Creating SDL applications



## fafner (Mar 4, 2007)

Hello,

I have unsuccessfully tried to create SDL applications on Mac OS X since quite some time. No problem with Windows and GNU/Linux (well, almost). Being a complete novice on Mac OS X development probably doesn't help 
I have tried to compile the following code:

```
#include <SDL/SDL.h>

int main(int,const char*)
{
  if(SDL_Init(SDL_INIT_VIDEO)==0)
         {
         fprintf(stderr,"SDL initialized.\n");
         SDL_Quit();
         }
    else fprintf(stderr,"Error while initializing SDL.\n");
  return 0;
}
```
I use the following command to compile it: g++ -g -framework SDL -framework Cocoa test.cpp -o test
I have also used various combinations of the following options: -lSDL -L/Library/Frameworks/SDL.framework/Versions/Current -D_REENTRANT -I/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/default
I have also tried to use with and without the file SDLMain_tmpl.m (my example is with, errors are different without it) .
It compiles without problem, but fails at linking with the following errors:


> ld: Undefined symbols:
> _SDL_free
> _SDL_main
> _SDL_malloc
> ...


Do someone has an idea on what's going on? And yes, I checked that SDL is correctly installed, but since I am novice in Mac OS X, I may be wrong.

Thanks in advance


----------



## szymczyk (Mar 4, 2007)

You must compile the file SDLMain.m along with test.cpp. SDLMain.m contains glue code that you need to run SDL apps on Mac OS X.

The SDL development libraries for Mac OS X contain SDL Xcode project templates. Building a project from the templates is going to be easier for a novice than building from the command line.


----------



## fafner (Mar 5, 2007)

I had tried to compile with the file SDLMain_tmpl.m too ( did you mean that instead of SDLMain.m ? ) . Without that file, I had an undefined symbol _main , even though I had defined the main function. I had read that I needed that SDLmain_tmpl.m file, and once added I got the errors above.

I have also tried to create an application from templates using xcode, but no SDL application was available anywhere (it is fairly possible I missed something though, however I am sure I installed the SDL package properly). Since I am stuck to xcode 1.5 (because I have only Mac OS X 10.3.9), I may lack the templates I need...


----------



## szymczyk (Mar 5, 2007)

The file should be called SDLMain.m. What version of SDL are you using? If you download the most recent runtime library for Mac OS X, the file SDLMain.m is in the devel-lite folder on the disk image.

If you're using Xcode 1.5, you have to use the Project Builder templates, not the Xcode ones. The Xcode templates are for Xcode 2.1 and later, which is confusing for people using earlier versions of Xcode.

I have written instructions on setting up SDL with Xcode that you might find helpful.


----------



## andreyu (Apr 7, 2007)

Hello *szymczyk*,
We try to port our latest game Arkanoid: Space Ball on to Mac OS X.
Can you explain or give me url to document described SDL application structure in Mac OS X?


----------



## szymczyk (Apr 9, 2007)

Apple has a Bundle Programming Guide that provides an introduction to Mac OS X application bundles.

For an SDL game, loading game files from the application bundle is the big issue. When you use SDL's Xcode project templates, your game's graphics, sound, and data files are copied to a Resources folder inside the application bundle. But the Mac OS X version of SDL sets the working directory to the directory above the application bundle. This behavior makes getting to the Resources folder difficult.

The easiest thing to do is to set the working directory to the Resources folder. The file SDLMain.m has a method to set the current working directory. You modify that method. The Bundle Programming Guide shows you the code you have to write to set the working directory.


----------

