I can't create a shader when trying to use shaders with sfml. The function glCreateShader(GL_VERTEX_SHADER); causes a segmentation fault. At first I googled it and found that it does that when the program does not have an opengl context. I tried SDL first but the poor documentation and "look at the header to know what to do" made me go for sfml

the code that causes the seg fault is bellow

    sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");

// Set color and depth clear value
glClearColor(0.f, 0.f, 0.f, 0.f);

// Enable Z-buffer read and write

// Setup a perspective projection

GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);


I'm including glew, gl.h, sfml-window, sfml-system, using opengl 2.1 on gcc linux.

What is missing?


1 Answer 1


You're not checking OpenGL extensions, version, or etc.

GLenum err = glewInit();
if (err != GLEW_OK)
  exit(1); // or handle the error in a nicer way
if (!GLEW_VERSION_2_1)  // check that the machine supports the 2.1 API.
  exit(1); // or handle the error in a nicer way

This code needs to happen after creating the OpenGL context, but before using any potentially-not-existing functions. More details on the GLEW web page

  • \$\begingroup\$ glewInit returns 1 and glewGetString(err) returns "1.6.0" \$\endgroup\$ Jan 23, 2012 at 0:51
  • \$\begingroup\$ that '1' return is GLEW_ERROR_NO_GL_VERSION. It means that you don't have an OpenGL context. Don't pass 'err' into glewGetString() -- pass it into glewGetErrorString() to get the printable version of the error message. \$\endgroup\$ Jan 23, 2012 at 1:26
  • \$\begingroup\$ shouldn't the context be created when I call the constructor to sf::Window? \$\endgroup\$ Jan 23, 2012 at 11:08
  • \$\begingroup\$ I don't know SFML, so don't know what you need to do to make the OpenGL context in that library (maybe someone else will be able to post another answer, filling in that knowledge?) But I do use GLEW a lot, and the error value you posted means that at the time when you called glewInit(), no OpenGL context had been created. So presumably there's something else you need to do? \$\endgroup\$ Jan 23, 2012 at 13:08
  • \$\begingroup\$ I followed SFML tutorial on their website. I'll accept your answer but how you create an opengl context? I'm having no luck with SDL as well \$\endgroup\$ Jan 23, 2012 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .