tags:

views:

60

answers:

3

I'd like to have both the includes for OS X as well as linux in my opengl program (C++) how can I set my program to use one if the other is not available? Here's what i'm currently doing:

 if(!FileExists(OpenGL/gl.h))
    #include <GL/glut.h> //linux lib
else {
    #include <OpenGL/gl.h> //OS x libs
    #include <OpenGL/glu.h>
    #include <GLUT/glut.h>
}
+3  A: 
#ifdef __APPLE__
#include <OpenGL/gl.h> //OS x libs
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
greyfade
+6  A: 

Here is what I use:

#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#ifdef _WIN32
  #include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif

All compilers for the mac (well,I guess that's gcc, and maybe clang) should define __APPLE__. I throw the _WIN32 in there since windows.h must be included before gl.h on windows platforms, it seems.

You can put this in its own include file (say gl_includes.h) if you have many files that need OpenGL

-matt

Matthew Hall
I prefer the `#if defined` form instead of `#ifdef` beacuse you can then use `#elif defined` and have a single `#endif` rather than `#else`,`#ifdef` with multiple `#endif`
Skizz
+1 for *not* including `glut.h` anywhere in there either!
Jerry Coffin
In general, that does sound like a better idea. Of course here, the #ifdef _WIN32 is actually folded into the non-apple branch of the first #ifdef, so I'm not sure it would reduce the #endifs. But it might be clearer if I made 3 separate branches for mac/win/*nix, and used #if defined, as you suggest.
Matthew Hall
Jerry- oops - I copied this from my own, non-glut project. Edit time.
Matthew Hall
+2  A: 

Alternatively, put the platform specific headers into their own files:

linux\platform.h

#include <GL/gl.h>
#include <GL/glu.h>

osx\platform.h

#include <OpenGL/gl.h> //OS x libs
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

win32\platform.h

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

and include in code:

#include "platform.h"

and then let your build system specify the correct search path based on the target platform.

Skizz
+1, ifdefs are terrible.
Steve M