You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建GLEW程序时出现未定义引用错误,求解决方法

OpenGL项目链接错误排查(CLion + MSYS2 g++)

搭建了一个简单OpenGL项目,使用CLion IDE、MSYS2的g++编译器,手动编译GLFW和GLEW源码。单独使用GLFW时无问题,但添加GLEW库后出现链接错误,项目无法构建。尝试过多个GLEW版本均无效,但Visual Studio中使用预编译库可正常运行。

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)

项目源码

#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;
}

项目结构

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

错误日志

====================[ 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

解决方案

问题根源

错误中的__imp_前缀表明编译器在查找动态链接库的导入符号,但当前链接的是静态库libglew32.a,且代码未定义GLEW_STATIC宏,导致GLEW头文件默认按动态库方式导出符号,出现符号不匹配。同时,OpenGL核心函数(如glViewport)需要链接系统OpenGL库。

修复步骤

  1. 添加GLEW_STATIC编译宏:
    在GLEW库的属性中添加编译宏,告诉编译器按静态库方式处理GLEW:

    set_target_properties(glew PROPERTIES
            IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/GLEW/libglew32.a"
            INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include"
            INTERFACE_COMPILE_DEFINITIONS "GLEW_STATIC")
    
  2. 链接系统OpenGL库:
    MinGW环境下需链接opengl32库,更新链接命令:

    target_link_libraries(udemy1 glew glfw opengl32)
    

完整修复后的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"
        INTERFACE_COMPILE_DEFINITIONS "GLEW_STATIC")
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 glfw opengl32)

额外检查

  • 确保编译GLEW时使用的架构(x86_64)与当前MSYS2环境一致,避免32/64位不匹配。
  • 运行时需将glew32.dll和glfw3.dll放置在可执行文件同目录,或添加到系统PATH中。

内容的提问来源于stack exchange,提问作者unstablecookie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:54:55