MacLibrary         How to create a library ( that is, a .dylib or an .bundle) using Ogre3D on Mac OSX

If you want to create a library ( that is, a .dylib or an .bundle) using Ogre3D on Mac OSX, you have to add a few lines of code to your project.

The problem is that when building an .app, Cocoa initialisation works fine. However, when building a dylib or a bundle, Cocoa is not getting initialised before SDL_Init is called, which leads to a lot of NSAutreleasepool warnings and finally a

*** Uncaught exception: <NSInternalInconsistencyException> Error (1002) creating CGSWindow

To prevent this, we have to initialise Cocoa "by hand". Since I don't know anything of Objective C, I just did it in C:
1. add an empty main method (don't ask, just do it...)
2. add the following code in your initialise function (or wherever you want, it just needs to be executed before you create your ogre application object)

void* cocoa_lib; 
 cocoa_lib = dlopen( "/System/Library/Frameworks/Cocoa.framework/Cocoa", RTLD_LAZY ); 
 void (*nsappload)(void); 
 nsappload = (void(*)()) dlsym( cocoa_lib, "NSApplicationLoad"); 

This forces Cocoa initialisation. It may not be the nicest way to do it, but it works...

3. Of course you still need to include the SDLMain.m and SDLMain.h as with the .app version

I had to do this since I use python and python ctypes for my app, and I needed a lib. While the .app worked nicely, getting the lib to work was a pain, so maybe this helps someone with the same problem...