EXEC_BAD_ACCESS in a two-line OpenGL program

The following simple program creates EXEC_BAD_ACCESS (segmentation fault) at startup, and I do not understand why:

#include <OpenGL/gl.h>

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

I run in Xcode on OS X 10.6.5, and I get attached to the OpenGL framework. Any ideas would be appreciated.

+3
source share
2 answers

You need to create an OpenGL context before you can call gl * functions. There are several ways to do this, for example using GLUT or SDL.

+5
source

For the C specification to create a GLubyte variable, you call it

 const GLubyte* glGetString(GL_VERSION );

then you can get the version. as follows

 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])

here is more general information about 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.
-1

Source: https://habr.com/ru/post/1780818/


All Articles