首页 > 解决方案 > 如何将 GLFW 库链接到 CLION (Windows)?

问题描述

首先,我从他们的网站下载了适用于 Windows 的 GLFW 32 位二进制文​​件。以下为本次下载内容:

target_link_libraries(myapp glfw)

然后,我将“include”和“lib-vc2019”文件复制到我的 Clion 项目文件夹“OpenGL”下名为“Dependencies”的文件夹中:

在此处输入图像描述

按照https://www.glfw.org/docs/3.3/build_guide.html#build_link_cmake_package中的“使用 CMake 并安装 GLFW 二进制文件”中的说明进行操作

在我的 CMakeLists.txt 文件中,我有以下内容:

cmake_minimum_required(VERSION 3.19)
project(OpenGL)

set(CMAKE_CXX_STANDARD 20)

add_executable(OpenGL Main.cpp)

include_directories(Dependencies)

find_package(glfw3 3.3 REQUIRED)
target_link_libraries(OpenGL glfw)

当我尝试构建时,我收到以下错误:

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

  Could not find a package configuration file provided by "glfw3" (requested
  version 3.3) with any of the following names:

    glfw3Config.cmake
    glfw3-config.cmake

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


-- Configuring incomplete, errors occurred!
See also "C:/Users/moehe/Desktop/CS/CPP/OpenGL/cmake-build-debug/CMakeFiles/CMakeOutput.log".
mingw32-make.exe: *** [Makefile:194: cmake_check_build_system] Error 1

在这上面花了很多时间,很困惑。如果有人可以提供一步一步的指导来完成这项工作,将不胜感激。

标签: c++openglcmakeclionglfw

解决方案


您误解了“已安装”部分:它们会生成一个 glfw3Config.cmake 文件,告诉 CMake 库和头文件所在的位置。find_package将找到并加载该文件。

将 CMake 文件的最后两行替换为以下内容。这将使用预定义的库和头文件设置 CMake 目标:

add_library(glfw STATIC IMPORTED)
set_target_properties(glfw PROPERTIES
  IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/lib-vc2019/glfw3.lib"
  INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(OpenGL glfw)

请参阅《是时候正确地做 CMake》了,以很好地介绍现代 CMake。


推荐阅读