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

CMake配置Raylib+MinGW出现未定义引用错误的解决方法

解决Raylib在C++环境中CMake配置的未定义引用错误

环境信息

  • C++编译器:SourceForge提供的默认MinGW
  • 代码编辑器:VSCode
  • 项目文件结构:
build/
├── _deps/
│   ├── raylib-build/
│   ├── raylib-src/
│   │   ├── CHANGELOG
│   │   ├── include/
│   │   ├── lib/
│   │   ├── LICENSE
│   │   └── README.md
│   └── raylib-tmp/
├── .cmake/
│   └── api/
│       └── v1/
├── cmake_install.cmake
├── CMakeCache.txt
├── CMakeFiles/...
├── compile_commands.json
└── Makefile
CMakeLists.txt
main.cpp

问题代码与错误

main.cpp

#include "raylib.h"

int main()
{
    // Initialization
    InitWindow(800, 600, "Hello Raylib");
    SetTargetFPS(60);

    // Main game loop
    while (!WindowShouldClose())
    {
        // Update

        // Draw
        BeginDrawing();
        ClearBackground(RAYWHITE);
        DrawText("Hello, Raylib!", 190, 200, 20, LIGHTGRAY);
        EndDrawing();
    }

    // De-Initialization
    CloseWindow();

    return 0;
}

原CMakeLists.txt

cmake_minimum_required(VERSION 3.30)
project(myproject)
include(FetchContent)

set(RAYLIB_VERSION 5.0)
FetchContent_Declare(
    raylib
    URL https://github.com/raysan5/raylib/releases/download/5.0/raylib-5.0_win64_mingw-w64.zip
)

FetchContent_MakeAvailable(raylib)

// Include the raylib headers
include_directories(${raylib_SOURCE_DIR}/include)

// Link the raylib library
add_executable(main main.cpp)
target_link_libraries(main raylib)

编译错误信息

[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.obj
[100%] Linking CXX executable main.exe
CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:6: undefined reference to `InitWindow'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:7: undefined reference to `SetTargetFPS'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:10: undefined reference to `WindowShouldClose'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:15: undefined reference to `BeginDrawing'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:16: undefined reference to `ClearBackground'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:17: undefined reference to `DrawText'
C:/Users/admin/Desktop/projects/kui c++/main/main.cpp:22: undefined reference to `CloseWindow'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\main.dir\build.make:98: recipe for target 'main.exe' failed        
mingw32-make[2]: *** [main.exe] Error 1
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/main.dir/all' failed   
mingw32-make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:89: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

错误原因分析

  1. CMake注释语法错误:原CMakeLists.txt使用了C++风格的//注释,但CMake仅支持#开头的注释,这会导致后续命令解析异常,库链接逻辑失效。
  2. 预编译库路径识别问题:直接下载预编译zip包时,CMake可能无法自动识别raylib目标,导致target_link_libraries(main raylib)无法找到正确的库文件。
  3. 潜在的位数不匹配:如果你的MinGW是32位版本,却下载了64位的raylib预编译包,会出现链接错误。

解决方法

方法1:修复CMake注释并明确指定库路径

修改后的CMakeLists.txt:

cmake_minimum_required(VERSION 3.30)
project(myproject)
include(FetchContent)

set(RAYLIB_VERSION 5.0)
FetchContent_Declare(
    raylib
    URL https://github.com/raysan5/raylib/releases/download/5.0/raylib-5.0_win64_mingw-w64.zip
)

FetchContent_MakeAvailable(raylib)

# Include the raylib headers
include_directories(${raylib_SOURCE_DIR}/include)

# Link the raylib library - 明确指定静态库文件
add_executable(main main.cpp)
target_link_libraries(main ${raylib_SOURCE_DIR}/lib/libraylib.a)
  • 若使用动态库,替换为${raylib_SOURCE_DIR}/lib/libraylib.dll.a,并将raylib.dll复制到可执行文件同目录。

方法2:从源代码编译raylib(推荐,兼容性更好)

直接拉取raylib源码编译,确保与你的MinGW版本完全匹配:

cmake_minimum_required(VERSION 3.30)
project(myproject)
include(FetchContent)

set(RAYLIB_VERSION 5.0)
FetchContent_Declare(
    raylib
    GIT_REPOSITORY https://github.com/raysan5/raylib.git
    GIT_TAG ${RAYLIB_VERSION}
)

# 强制编译静态库(可选,默认是动态库)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(raylib)

add_executable(main main.cpp)
# 无需手动指定头文件路径,FetchContent会自动处理依赖
target_link_libraries(main raylib)

方法3:检查MinGW与raylib包的位数匹配

  • 若你的MinGW是32位,下载对应32位的raylib预编译包:raylib-5.0_win32_mingw-w64.zip,替换CMakeLists中的URL。

后续操作

  1. 删除原build目录,重新创建并进入。
  2. 执行cmake .. -G "MinGW Makefiles"(明确指定生成器)。
  3. 执行mingw32-make重新编译。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 12:47:32