Search code examples
c++glewopengl-3

I cant get GLEW to work on netbeans in ubuntu 20.04 (C++)


I managed to get GLFW3 to work and properly display a window, however when i add glewInit() to my project it tells me undefined reference to glewInit'. I am using g++ as my compiler with -lglfw3 and lGl as flags. Below is a simple project to demonstrate my issue. I am on linux. Any help is much appreicated.

#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>

#include <iostream>

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // glfw window creation
  
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    
    // here is the error, my glew is not intializing :(
    if(glewInit() != GLEW_OK) {
        return 0;
    }
    
    // render loop
   
    while (!glfwWindowShouldClose(window))
    {

        // render

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
 
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // glfw: terminate, clearing all previously allocated GLFW resources.
   
    glfwTerminate();
    return 0;
}


Solution

  • There is a standard way on Linux to get a correct set of libraries to link with your program - please look at man pkg-config. In your case you need to link everything, which is needed by both GLFW3 and GLEW - so you should call the pkg-config two times:

    hekto@ubuntu:~$ pkg-config --libs glfw3
    -lglfw
    hekto@ubuntu:~$ pkg-config --libs glew
    -lGLEW -lGLU -lGL
    

    Therefore, you need to use -lglfw instead of -lglfw3 and add two more libraries - -lGLEW and -lGLU. These calls to the pkg-config tool can be directly added to your Makefile as well.