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

基于MSYS2的Windows GitHub CI中OPENGL_INCLUDE_DIR未找到问题求助

解决MSYS2环境下CMake编译raylib项目时OPENGL_INCLUDE_DIR NOTFOUND问题

问题背景

在基于MSYS2的Windows GitHub CI环境中,将raylib项目从Makefile迁移到CMake后,出现以下错误:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
/d/a/its_raining/its_raining/includes/raylib/src/OPENGL_INCLUDE_DIR

Ubuntu环境下通过安装libgl1-mesa-dev解决,但Windows环境中安装mingw-w64-ucrt-x86_64-mesa、mingw-w64-x86_64-glew等相关包后仍未解决,且旧版Makefile可正常编译。


解决方案

1. 修正CMake中OpenGL路径的查找逻辑

如果项目直接引入raylib源码(如submodule或本地目录),CMake可能无法自动定位MSYS2下的OpenGL头文件,可通过以下方式修正:

  • 手动指定路径:在CMakeLists.txt中添加对应架构的OpenGL头文件路径:
    # UCRT64架构用此路径,MINGW64替换为/mingw64/include/GL
    set(OPENGL_INCLUDE_DIR "/ucrt64/include/GL" CACHE PATH "OpenGL include directory")
    
  • 使用标准find_package:替换原有依赖查找逻辑,让CMake自动识别系统OpenGL:
    find_package(OpenGL REQUIRED)
    include_directories(${OPENGL_INCLUDE_DIR})
    

2. 清理冗余依赖,安装正确的MSYS2包

raylib自身封装了OpenGL调用,无需额外安装glew或重复的mesa包,执行以下命令安装必要依赖:

# 更新系统包索引
pacman -Syu --noconfirm
# 安装UCRT64架构的编译工具与依赖(MINGW64将前缀替换为mingw-w64-x86_64-*)
pacman -S --noconfirm mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-make mingw-w64-ucrt-x86_64-opengl mingw-w64-ucrt-x86_64-glfw mingw-w64-ucrt-x86_64-raylib

3. 对齐旧Makefile的编译逻辑

旧Makefile能正常运行,说明它指定了正确的依赖路径或链接参数:

  • 查看Makefile中的编译选项,比如是否包含-I/ucrt64/include/GL或链接参数-lopengl32 -lglfw,将这些对应到CMake配置中。
  • 如果旧Makefile使用预编译的raylib库,CMake也应直接链接预编译库,而非从源码编译raylib。

4. 修正GitHub CI的环境配置

确保CI脚本正确激活MSYS2环境并执行编译:

steps:
  - uses: msys2/setup-msys2@v2
    with:
      msystem: UCRT64 # 匹配你的MSYS2架构,可选MINGW64
      update: true
      install: >-
        mingw-w64-ucrt-x86_64-cmake
        mingw-w64-ucrt-x86_64-make
        mingw-w64-ucrt-x86_64-opengl
        mingw-w64-ucrt-x86_64-glfw
        mingw-w64-ucrt-x86_64-raylib
  - name: Build with CMake
    shell: msys2 {0}
    run: |
      mkdir -p build && cd build
      cmake .. -G "MinGW Makefiles"
      make

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 15:22:41