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

如何在新建CLion项目中配置GLFW?遇CMake及CPM依赖问题求助

问题:CLion中导入GLFW/Glad库失败

一、手动导入GLFW的错误情况

已下载GLFW二进制文件,将include文件夹复制到项目根目录,但无法完成链接,CMake报错如下:

====================[ Build | LearnOpenGL | Debug ]=============================
"C:\Program Files\CMake\bin\cmake.exe" --build C:\dev\LearnOpenGL\cmake-build-debug --target LearnOpenGL -j 10
[0/1] Re-running CMake...
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


CMake Error at CMakeLists.txt:6 (find_package):
  By not providing "FindGLFW.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "GLFW", but
  CMake did not find one.

  Could not find a package configuration file provided by "GLFW" with any of
  the following names:

    GLFWConfig.cmake
    glfw-config.cmake

  Add the installation prefix of "GLFW" to CMAKE_PREFIX_PATH or set
  "GLFW_DIR" to a directory containing one of the above files.  If "GLFW"
  provides a separate development package or SDK, be sure it has been
  installed.


ninja: error: rebuilding 'build.ninja': subcommand failed
-- Configuring incomplete, errors occurred!
FAILED: build.ninja 
"C:\Program Files\CMake\bin\cmake.exe" --regenerate-during-build -SC:\dev\LearnOpenGL -BC:\dev\LearnOpenGL\cmake-build-debug

当前CMakeLists.txt内容:

cmake_minimum_required(VERSION 3.0)
project(LearnOpenGL)

set(CMAKE_CXX_STANDARD 23)

find_package(GLFW REQUIRED)

add_executable(LearnOpenGL main.cpp)

include_directories(./include)
target_link_libraries("${CMAKE_PROJECT_NAME}" PRIVATE glfw)

项目结构:

Root
|-- include
|  |-- GLFW
|  |  |-- glfw3.h
|  |  |-- glfwnative.h
|-- CMakeLists.txt
|-- main.cpp

二、尝试CPM管理依赖后的问题

改用CPM管理依赖后,Glad无法正常工作,修改后的CMakeLists.txt内容:

cmake_minimum_required(VERSION 3.0)
project(LearnOpenGL)

set(CMAKE_CXX_STANDARD 23)

add_executable(main main.cpp)

include(cmake/CPM.cmake)
CPMAddPackage("gh:glfw/glfw#3.4")
CPMAddPackage("gh:Dav1dde/glad#v2.0.6")

target_link_libraries(main glad glfw)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 18:13:27