C++ OS X 和 Linux 的 OpenGL 头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3907818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:02:44  来源:igfitidea点击:

OpenGL headers for OS X & Linux

c++openglheader

提问by Drew

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:

我想在我的 opengl 程序 (C++) 中同时包含 OS X 和 linux 的包含,如果另一个不可用,我该如何设置我的程序使用一个?这是我目前正在做的事情:

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

回答by Gretchen

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 _WIN32in there since windows.h must be included before gl.h on windows platforms, it seems.

mac 的所有编译器(嗯,我猜那是 gcc,也许是 clang)应该定义__APPLE__. 我把它扔_WIN32在那里,因为 windows.h 必须包含在 windows 平台上的 gl.h 之前,似乎。

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

如果你有很多需要 OpenGL 的文件,你可以把它放在它自己的包含文件中(比如 gl_includes.h)

-matt

-亚光

回答by Skizz

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

或者,将特定于平台的头文件放入它们自己的文件中:

linux\platform.h

linux\平台.h

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

osx\platform.h

osx\platform.h

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

win32\platform.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.

然后让您的构建系统根据目标平台指定正确的搜索路径。

回答by greyfade

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

回答by Joel Teply

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

我们用于 OSX、Unix、Linux、Android 和 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