Windows10下CMake构建mangetsu时无法找到glfw3包求助
解决Windows下CMake无法识别手动安装的GLFW3问题
问题背景
在Windows 10系统下使用CMake构建mangetsu项目,启用BUILD_GUI=On参数时,出现找不到glfw3包的错误,具体CMake执行日志如下:
C:\mangetsu\build>cmake -DBUILD_GUI=On .. -- Building for: Visual Studio 16 2019 -- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.19045. -- The C compiler identification is MSVC 19.29.30146.0 -- The CXX compiler identification is MSVC 19.29.30146.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found OpenGL: opengl32 -- Found PkgConfig: C:/Program Files/CMake/bin/pkg-config.exe (found version "0.26") -- Checking for module 'glfw3' -- No package 'glfw3' found CMake Error at C:/Program Files/CMake/share/cmake-3.24/Modules/FindPkgConfig.cmake:607 (message): A required package was not found Call Stack (most recent call first): C:/Program Files/CMake/share/cmake-3.24/Modules/FindPkgConfig.cmake:829 (_pkg_check_modules_internal) CMakeLists.txt:37 (pkg_check_modules)
项目仅提供Linux环境的安装指导:
sudo apt install -y build-essential cmake zlib1g-dev libssl-dev
sudo apt install -y libopengl-dev libglfw3-dev
已手动安装glfw3,但CMake仍无法识别。
问题原因分析
项目的CMakeLists.txt通过pkg_check_modules依赖pkg-config查找GLFW3,但Windows环境下:
- pkg-config没有系统级的包数据库,手动安装的GLFW不会自动注册到pkg-config的搜索路径
- CMakeLists.txt中硬编码了Linux风格路径(如
/usr/local/),未适配Windows的库查找逻辑
解决方案
方案1:修改CMakeLists.txt适配Windows
替换原有的GLFW查找逻辑,改用CMake原生的FindGLFW3模块:
- 找到CMakeLists.txt中
if (${BUILD_GUI})区块,修改GLFW相关代码:
if (${BUILD_GUI}) # OpenGL find_package(OpenGL REQUIRED) include_directories(${OPENGL_INCLUDE_DIRS}) # Glfw for windowing find_package(GLFW3 REQUIRED) include_directories(${GLFW3_INCLUDE_DIR}) endif()
- 修改imgui的链接配置:
target_link_libraries(imgui ${OPENGL_LIBRARIES} ${GLFW3_LIBRARY} )
- 执行CMake时指定GLFW路径(如果GLFW未安装到默认路径):
cmake -DBUILD_GUI=On -DGLFW3_INCLUDE_DIR="C:/path/to/glfw/include" -DGLFW3_LIBRARY="C:/path/to/glfw/lib-vc2019/glfw3.lib" ..
方案2:给pkg-config指定GLFW的.pc文件路径
无需修改CMakeLists.txt,手动配置pkg-config的搜索路径:
- 找到GLFW安装目录下的
glfw3.pc文件(通常在glfw/lib/pkgconfig) - 执行CMake前设置环境变量:
set PKG_CONFIG_PATH=C:/path/to/glfw/lib/pkgconfig
- 正常执行CMake命令:
cmake -DBUILD_GUI=On ..
方案3:使用vcpkg安装GLFW(推荐)
vcpkg会自动处理CMake依赖的查找与配置:
- 安装vcpkg并集成到CMake:
git clone https://github.com/microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.bat ./vcpkg integrate install
- 安装GLFW3:
./vcpkg install glfw3:x64-windows
- 使用vcpkg的工具链构建项目:
cmake -DBUILD_GUI=On -DCMAKE_TOOLCHAIN_FILE=C:/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake ..
内容的提问来源于stack exchange,提问作者Arthur Maciel Batista
相关产品推荐
相关产品推荐

