2行OpenGL程序中的EXC_BAD_ACCESS

时间:2010-12-17 05:34:36

标签: macos opengl initialization segmentation-fault

以下简单程序在运行时会产生EXEC_BAD_ACCESS(分段错误),我不明白为什么:

#include <OpenGL/gl.h>

int main(void) {
  const GLubyte * strVersion;
  // The next line gives an 'EXEC_BAD_ACCESS'
  strVersion = glGetString (GL_VERSION);
}

我在OS X 10.6.5中运行Xcode,并且我正在与OpenGL框架进行链接。任何想法都将不胜感激。

2 个答案:

答案 0 :(得分:5)

在调用gl *函数之前,必须先创建一个OpenGL上下文。有几种方法可以做到这一点,例如使用GLUT或SDL。

答案 1 :(得分:-1)

对于C规范来创建变量GLubyte,您可以通过

调用它
 const GLubyte* glGetString(GL_VERSION );

然后你应该能够获得该版本。通过以下

 const char *GLVersionString = glGetString(GL_VERSION);
 //Or better yet, use the GL3 way to get the version number
 int OpenGLVersion[2];
 glGetIntegerv(GL_MAJOR_VERSION, &OpenGLVersion[0])
 glGetIntegerv(GL_MINOR_VERSION, &OpenGLVersion[1])

这里有关于glGetString的更多基本信息:

 glGetString returns a pointer to a static string describing some aspect of the current GL connection. name can be one of the following:
 GL_VENDOR
     Returns the company responsible for this GL implementation.
     This name does not change from release to release.             
 GL_RENDERER
     Returns the name of the renderer.
     This name is typically specific to a particular configuration of a hardware platform.
     It does not change from release to release.              
 GL_VERSION
     Returns a version or release number.
 GL_SHADING_LANGUAGE_VERSION
     Returns a version or release number for the shading language.           
 GL_EXTENSIONS
     Returns a space-separated list of supported extensions to GL.