Issue with GLEW initialization causing program to not output

130 views Asked by At

I'm encountering a problem while trying to use the GLEW library for my basic OpenGL program, which is written using GLFW (glfw3.h). Everything compiles without errors, but as soon as I call glewInit(), I don't get any output at all, including any cout statements, neither before nor after the initialization. However, when I remove the glewInit() call, everything works as expected.

I'm wondering why this is happening and how I can fix it. Here's a simplified version of my code:

#define GLEW_STATIC 1 

#include "glew.h"
#include "glfw3.h"
#include <iostream>

int main()
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    
    //INFECTED LINE OF CODE - START
    std::cout << glGetString(GL_VERSION) << std::endl;

    glewExperimental=GL_TRUE;
    GLenum err=glewInit();
    if(err!=GLEW_OK)
    {
        //Problem: glewInit failed, something is seriously wrong.
        std::cout<<"glewInit failed, aborting."<<std::endl;
    }
     //INFECTED LINE OF CODE - END

    /* Loop until the user closes the window */
  while (!glfwWindowShouldClose(window))
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    glVertex2f(-0.5f, -0.5f);
    glVertex2f(0.0f, 0.5f);
    glVertex2f(0.5f, -0.5f);
    glEnd();

    glfwSwapBuffers(window);
    glfwPollEvents();
}

    glfwTerminate();
    return 0;
}

I've included GLEW and GLFW correctly, and there are no compilation errors. What could be causing this issue, and how can I resolve it? Thanks for your help!

//EDIT#1 Tried using:

(...)
 glewExperimental=GL_TRUE;
    GLenum err=glewInit();
    std::cout<<glewGetErrorString(err);
    glewGetErrorString(err);
    if(err!=GLEW_OK)
    {
        //Problem: glewInit failed, something is seriously wrong.
        std::cout<<glewGetErrorString(err);
        glewGetErrorString(err);
        std::cout<<"glewInit failed, aborting."<<std::endl;
    }
(...)

and still No output

When I comment out the infected lines of code and leave only this line:

std::cout << glGetString(GL_VERSION) << std::endl;

I'm getting the output:

4.6.0 - Build 26.20.100.7637

//EDIT#2 debugger output: dbg

1

There are 1 answers

3
Quimby On

You cannot call OpenGL functions like glGetString before glewInit. glewInit() is responsible for loading these functions dynamically from GPU driver's DLL.

If you do, you are likely getting segfault because glGetString is just a macro hiding uninitialized (at that moment) function pointer.