Search code examples
c++openglglfwglew

GLEW and GLFW undefined references


I am trying to set up openGl with glfw and glew. This is the source code:

#define GLFW_INCLUDE_GLU
#ifdef _WIN32
    #define GLFW_DLL // -> windows
    #include <GL/glew.h>
#elif __linux__
    #include <GL/glew.h>
#endif


#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>

GLFWwindow* window = NULL;
GLFWmonitor* monitor = NULL;

int window_width = 800;
int window_height = 600;

static void window_hints() {
    const GLFWvidmode* mode = glfwGetVideoMode(monitor);

    glfwWindowHint(GLFW_RED_BITS, mode->redBits);
    glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
    glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
    glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);

    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
}


static void set_window_callbacks() {
    glfwSetWindowCloseCallback(window, [] (GLFWwindow *window) {
        std::cout << "closing window!";
    });
    glfwSetKeyCallback(window, [] (GLFWwindow *window, int key,int scancode, int action, int mods) {
        if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
            glfwSetWindowShouldClose(window, GLFW_TRUE);
        }
    });
    glfwSetWindowSizeCallback(window, [] (GLFWwindow *window, int width, int height) {
        glViewport(0, 0, width, height);
        window_width = width;
        window_height = height;
        glLoadIdentity();
        GLdouble aspect = (GLdouble)window_width / window_height;
        glOrtho(-1, 1, -1 / aspect, 1 / aspect, 0.01, 10000);
        glTranslatef(0, 0, -10);
    });
}

void GLAPIENTRY
MessageCallback( GLenum source,
                 GLenum type,
                 GLuint id,
                 GLenum severity,
                 GLsizei length,
                 const GLchar* message,
                 const void* userParam )
{
  fprintf( stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
           ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
            type, severity, message );
}



int main(int argc, char *argv[])
{

    glfwSetErrorCallback([] (int code, const char * err_msg) {
        std::cerr << "GLFW Error " << code << ": \n\t" << err_msg << std::endl;
    });

    if(!glfwInit())
        throw std::runtime_error("glfwInit failed");

    monitor = glfwGetPrimaryMonitor();
    window_hints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    window = glfwCreateWindow(window_width, window_height, "test window", NULL, NULL);

    if(!window)
        throw std::runtime_error("glfwOpenWindow failed.");


    set_window_callbacks();

    // GLFW settings
    glfwMakeContextCurrent(window);
    glewInit(); 

    glfwSwapInterval(1);

    std::cout << glGetString(GL_VERSION) << std::endl;
    
    glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
    glDebugMessageCallback(MessageCallback, 0);

    glMatrixMode(GL_PROJECTION);

    glViewport(0, 0, window_width, window_height);
    glLoadIdentity();
    GLdouble aspect = (GLdouble)window_width / window_height;
    glOrtho(-1, 1, -1 / aspect, 1 / aspect, 0.01, 10000);
    glTranslatef(0, 0, -10);    


    while(!glfwWindowShouldClose(window))
    {
        // process pending events
        glfwPollEvents();
        glClearColor(0, 0, 0, 1); // black
        glClear(GL_COLOR_BUFFER_BIT);

        //glRotatef(1, 0, 0, 0.1);

        glBegin(GL_TRIANGLES);

        glColor3f(1.0, 0.0, 0.0);
        glVertex2f(-0.5, -0.5);

        glColor3f(0.0, 1.0, 0.0);
        glVertex2f( 0.5, -0.5);

        glColor3f(0.0, 0.0, 1.0);
        glVertex2f( 0.0,  0.5);

        glEnd();

        glfwSwapBuffers(window);
    }

    // clean up and exit
    glfwTerminate();
    std::cerr << std::endl;
    return 0;
}

And this works perfectly under Linux with the following makefile:

main: main.cpp makefile
    g++ main.cpp -g -o main -lglfw -lGL -lX11 -lGLEW -std=c++2a

Then, I wanted to make it to compile under windows too, so I tried with this makefile:

main.exe: main.cpp makefile
    g++ main.cpp -g -o main.exe -I"D:\cpp_libraries\glfw-3.3.5.bin.WIN64\include" -I"D:\cpp_libraries\glew-2.1.0\include" -L"D:\cpp_libraries\glfw-3.3.5.bin.WIN64\lib-mingw-w64" -L"D:\cpp_libraries\glew-2.1.0\lib\Release\x64" -lglfw3dll -lglew32 -lopengl32

Here, "glew-2.1.0" and "glfw-3.3.5.bin.WIN64" are downloaded from the respective download pages for precompiled windows binaries. I have added "D:\cpp_libraries\glew-2.1.0\bin\Release\x64" and "D:\cpp_libraries\glfw-3.3.5.bin.WIN64\lib-mingw-w64" to the Path environment variable as they contain glew32.dll and glfw3.dll.

When I try to compile, g++ gives "undefined reference to " every single glew/gl/glfw function call. I also tried running the same g++ command through VS Code's tasks.json with the same result.

Then I noticed that everyone on the internet said I must link against "glfw3.lib" and I noticed that the first -L argument to g++ is a folder that does not actually contain any .lib files as it should. But I cannot find the "glfw3.lib" anywhere in the zip I downloaded earlier. I even tried building glfw from source with CMake, Make and Code::Blocks, but none of them generated a .lib file I can link against.

Edit: I forgot to mention that I have not added the dlls to my project directory, but it really should not be necessary. I tried it anyway and it didn't fix the problem.

Edit 2: it would be better to link with -lglfw, not -lglfwdll, so I changed that.

Edit 3: actually, gl functions are getting linked fine. The problem is only with glfw and glew.


Solution

  • I finally got it working. It turns out the precompiled binaries for glew and glfw do not work on my machine. I had to download both sources and compile the libraries myself. This is the makefile that finally works:

    main.exe: main.cpp makefile
        g++ main.cpp -g -o main.exe -I"D:/cpp_libraries/glfw-3.3.5-source/include" -I"D:/cpp_libraries/glew-2.1.0-source/include" -L"D:/cpp_libraries/glfw-3.3.5-compiled/src" -L"D:/cpp_libraries/glew-2.1.0-compiled/lib" -lglfw3 -lglew32 -lopengl32