Undefined references when building GLEW program?

59 views Asked by At

i set up simple OpenGl project (Clion IDE, g++ compiler from msys) i download glfw and glew source files, compiled them manually with mingw32-make. Still cannot build project due to linking problem (i guess). Meanwhile i've used this way glfw with no problems before, struggle started only with addition of glew library. i'm new to c++ , so this error confuses me a lot. i ran out of options trying to fix it.

cmake:

cmake_minimum_required(VERSION 3.14)
project(udemy1)

set(CMAKE_CXX_STANDARD 17)
add_executable(udemy1
        src/main.cpp
        )

add_library(glew STATIC IMPORTED)
add_library(glfw STATIC IMPORTED)

set_target_properties(glew PROPERTIES
        IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/GLEW/libglew32.a"
        INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")
set_target_properties(glfw PROPERTIES
        IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/GLFW/libglfw3.a"
        INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")

target_link_libraries(udemy1 glew)
target_link_libraries(udemy1 glfw)

souce

#include <stdio.h>

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

const GLint WIDTH = 800, HEIGHT = 600;//window dimensions

int main()
{
    if (!glfwInit())
    {
        printf("GLFW init failed!");
        glfwTerminate();
        return 1;
    }

    //setup GLFW window props
    //OpenGL version
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
    //CORE FILE -> no backwards compatibility
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    //allow forward compat
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    GLFWwindow *mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "test window", NULL, NULL);
    if(!mainWindow)
    {
        printf("GLFW window creation failed");
        glfwTerminate();
        return 1;
    }

    //get buffer size information
    int bufferWidth, bufferHeight;
    glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);

    //set context for glew to use
    glfwMakeContextCurrent(mainWindow);

    //allow modern extentions
    glewExperimental = GL_TRUE;

    if(glewInit() != GLEW_OK)
    {
        printf("glew init failed!");
        glfwDestroyWindow(mainWindow);
        glfwTerminate();
        return 1;
    }

    //set up viewport size
    glViewport(0, 0, bufferWidth, bufferHeight);

    //loop untill window closed
    while(!glfwWindowShouldClose(mainWindow))
    {
        //get and handle user input events
        glfwPollEvents();//check for events
        //clear window
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(mainWindow);//two scenes
    }
    return 0;
}
project structure:
udemy1
    -build
    -cmake-build-debug
    -Dependencies
        -GLEW
            -glew32.dll
            -glewinfo.exe
            -libglew32.a
            -libglew32.dll.a
            -visualinfo.exe
        -GLFW
            -glfw3.dll
            -libglfw3.a
            -libglfw3dll.a
    -include
        -GL
            -glew.h
            -glxew.h
            -wglew.h
        -GLFW
            -glfw3.h
            -glfw3native.h
    -src
        -main.cpp
    CmakeList.txt

EDIT, attached error. my bad

====================[ Build | all | Debug ]=====================================
"D:\DEV\cpp\cl\CLion 2019.2\bin\cmake\win\bin\cmake.exe" --build D:\DEV\cpp\projects\udemy1\build --target all -- -j 8
mingw32-make[1]: Entering directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make[2]: Entering directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make[2]: Leaving directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make[2]: Entering directory 'D:/DEV/cpp/projects/udemy1/build'
[ 50%] Linking CXX executable udemy1.exe
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\udemy1.dir/objects.a(main.cpp.obj): in function `main':
D:/DEV/cpp/projects/udemy1/src/main.cpp:42:(.text+0xe9): undefined reference to `__imp_glewExperimental'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/DEV/cpp/projects/udemy1/src/main.cpp:44:(.text+0xf3): undefined reference to `__imp_glewInit'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/DEV/cpp/projects/udemy1/src/main.cpp:53:(.text+0x140): undefined reference to `glViewport'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/DEV/cpp/projects/udemy1/src/main.cpp:61:(.text+0x166): undefined reference to `glClearColor'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/DEV/cpp/projects/udemy1/src/main.cpp:62:(.text+0x170): undefined reference to `glClear'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [CMakeFiles\udemy1.dir\build.make:88: udemy1.exe] Error 1
mingw32-make[2]: Leaving directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make[1]: *** [CMakeFiles\Makefile2:72: CMakeFiles/udemy1.dir/all] Error 2
mingw32-make[1]: Leaving directory 'D:/DEV/cpp/projects/udemy1/build'
mingw32-make: *** [Makefile:83: all] Error 2

i did try compile other versions of glew.- same error. i reproduced project with Visual Studio IDE and precompiled libraries (and it works).

1

There are 1 answers

1
Botje On

You need to define the GLEW_STATIC preprocessor symbol when linking statically to glew:

target_compile_definitions(glew PUBLIC GLEW_STATIC)

If you do not define this symbol, glew defines all of its functions as dllimport, which is why you see a missing symbol for __imp_glewExperimental.