Sharing GLUT Code Between Windows and Mac OS X

If you’ve seen my last post, you’re aware that I’ve been playing with OpenGL and GLUT for a uni subject. As I like to straddle the OS fence and use a mixture of Windows Vista x64, Windows XP and Mac OS X 10.5, I like to have platform independent solutions to problems. The latest little issue I’ve come across regarding GLUT apps is that GLUT on the Mac has a different include. To include GLUT in a project for the mac, you must use the following:

#include <GLUT/glut.h>

and not:

#include <GL/glut.h>

In order to cut code that will compile successfully on either platform we can use optional includes with the #ifdef, #else and #endif preprocessor directives. Props to “kainjow” for providing this information in his/her forum post. Here’s how:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

All this does is say “if we’re running on an Apple machine, include GLUT/glut.h, otherwise include GL/glut.h. Now to actually learn OpenGL/GLUT…