CMake 错误:找不到 GLFW3(缺少:GLFW3_DIR)
CMake Error: Could NOT find GLFW3 (missing: GLFW3_DIR)
ID: cmake/glfw3-not-found-missing-config
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| GLFW 3.3 | active | — | — | — |
| GLFW 3.4 | active | — | — | — |
| CMake 3.16 | active | — | — | — |
| CMake 3.20 | active | — | — | — |
根因分析
CMake 的 find_package(GLFW3) 无法找到 GLFW3Config.cmake 或 glfw3-config.cmake 文件,通常是因为 GLFW3 未安装或安装前缀不在 CMake 搜索路径中。
English
CMake's find_package(GLFW3) cannot locate the GLFW3Config.cmake or glfw3-config.cmake file, typically because GLFW3 is not installed or the install prefix is not in CMake's search path.
官方文档
https://www.glfw.org/docs/latest/build.html解决方案
-
安装开发包:在 Ubuntu/Debian 上:sudo apt-get install libglfw3-dev。在 Fedora 上:sudo dnf install glfw-devel。在 macOS 上:brew install glfw。
-
如果从源码构建 GLFW3,运行 cmake 时加上 -DGLFW_BUILD_DOCS=OFF -DGLFW_INSTALL=ON,然后 sudo make install。然后将 GLFW3_DIR 设置为安装前缀(例如 /usr/local/lib/cmake/glfw3)。
-
在命令行手动提供 GLFW3_DIR:cmake .. -DGLFW3_DIR=/path/to/glfw3/lib/cmake/glfw3
无效尝试
常见但无效的做法:
-
70% 失败
Some system packages (e.g., libglfw3 on Ubuntu) install only the shared library and headers, not the CMake config file. The config file is in a separate -dev package.
-
85% 失败
GLFW3_DIR must point to the directory containing GLFW3Config.cmake, typically the build or install directory, not the source root.
-
60% 失败
Without REQUIRED, CMake continues but variables like GLFW3_FOUND are FALSE, leading to undefined symbols at link time.