简体   繁体   中英

OpenGL headers for OS X & Linux

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>
}

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

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.

#ifdef __APPLE__
#include <OpenGL/gl.h> //OS x libs
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

What we use for OSX, Unix, Linux, Android, and iOS

#if defined(_WIN32) || defined(_WIN64)
#  include <gl/glew.h>
#  include <GL/gl.h>
#  include <GL/glu.h>
#elif __APPLE__
#  include "TargetConditionals.h"
#  if (TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR) || TARGET_OS_IPHONE
#    include <OpenGLES/ES2/gl.h>
#    include <OpenGLES/ES2/glext.h>
#  else
#    include <OpenGL/gl.h>
#    include <OpenGL/glu.h>
#    include <OpenGL/glext.h>
#  endif
#elif defined(__ANDROID__) || defined(ANDROID)
#  include <GLES2/gl2.h>
#  include <GLES2/gl2ext.h>
#elif defined(__linux__) || defined(__unix__) || defined(__posix__)
#  include <GL/gl.h>
#  include <GL/glu.h>
#  include <GL/glext.h>
#else
#  error platform not supported.
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM