CMake 的 GLFW 构建错误:"The following configuration files were considered but not accepted"

GLFW build error with CMake: "The following configuration files were considered but not accepted"

我一直在尝试设置 GLFW 来启动一个 OpenGL 项目,但是无论我尝试什么,我都无法解决一个错误。错误消息的重要部分如下:

CMake Error at CMakeLists.txt:12 (find_package):
  Could not find a configuration file for package "glfw3" that is compatible
  with requested version "3.3.2".

The following configuration files were considered but not accepted:

    C:/Users/../GLFW/lib/cmake/glfw3/glfw3Config.cmake, version: 3.3.2 (32bit)

我的 CMakeLists.txt 看起来像这样:

set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib/cmake/glfw3")

add_executable(1_Intro main.cpp)

include_directories(Dependencies/GLFW/include/GLFW/)

find_package(glfw3 3.3.2 CONFIG REQUIRED)
target_link_libraries(1_Intro ${GLFW})

所有必需的 GLFW 文件都可以在我的项目文件夹中找到,如下所示:

GLFW
|- include
|   |- GLFW
|       | - glfw3.h
|       | - glfw3native.h
|- lib
    |- cmake
        |- glfw3
            |- glfw3Config.cmake
            |- glfw3ConfigVersion.cmake
            |- glfw3Targets.cmake
            |- glfw3Targets-noconfig.cmake
    |- pkgconfig
        |- glfw3.pc
    |- libglfw3.a

我为构建库所做的工作是下载源代码,然后使用 cmake -G "MinGW Makefiles",因为我使用 MinGW-w64 GCC 编译器,然后是 mingw32-makemingw32-make install。然后我将构建文件复制到如上所示的文件夹中。

在搜索解决方案后,我发现问题可能来自库和编译器使用的构建架构的差异 (here and here)。我使用 MinGW-w64 编译器并且更愿意坚持使用它,所以我尝试更改 CMake 构建。但是,我遇到了另一个问题,我无法使用 -DCMAKE_GENERATOR_PLATFORM 等选项更改“MinGW Makefiles”的平台规范。我尝试更改工具链,但这也没有用;唯一的解决方案似乎是使用 Visual Studio 64 位工具链,但这不是我想要的。

有没有办法将我的 CMake 构建更改为 x64 架构?或者将我的编译器设置为 x86(如果这甚至有意义的话)以匹配“glfw3Config.cmake”版本?我对其他解决方案持开放态度,只要我能保持我的项目结构和编译器不变。

所以我想出了问题所在。我的计算机上有 MinGW-w64 and MinGW,它们的 bin 目录路径都在 PATH 中。例如,当 运行ning where gcc 时,显示的是后者的 bin 路径,而不是前者。所以我从 PATH 中删除了 MinGW 的路径,重新启动计算机,按照之前的解释重新构建和重新编译,现在可以了。

不过,我将此作为一个答案来解释我安装 GLFW 所采取的步骤,这有点像噩梦,在上述问题之前我遇到了很多问题。我使用 Windows 10、CMake 3.17.2、MinGW-w64 和 GCC 8.1.0,但以下与其他编译器和体系结构相同,并进行必要的更改:

  1. 正在从 the GLFW website 下载源代码。在撰写本文时,版本为 3.3.2。

  2. 构建库 :

    1. 首先,解压缩源代码并在其中创建一个build文件夹(尽管您可以在源文件夹之外创建它,但没有区别)和运行 cmake -G "MinGW Makefiles" -S . -B build(我之前推荐 运行ning where gcc,检查 GCC 路径是否指向 MinGW-w64 的 bin;参见 this post

    2. 运行 在 build 文件夹中命令 mingw32-make。这将花费一点时间,因为它会生成所有目标,不仅是库本身,还有文档和示例。如果你不想要这个,你可以 return 到步骤 2.1 和 运行 带有选项 as described here (a simpler option is to just use the CMake GUI, where you can turn on and off these options fairly easily). I would like to thank the author of this post 的 CMake 命令,其对构建过程的精确描述(以及他正在安装的事实GLFW with CMake,在 Windows 上,像我一样)对我帮助很大。

    3. 您应该有一个 cmake_install.cmake 文件。打开它:您应该在前几行中看到类似这样的内容:

      # Set the install prefix
      if(NOT DEFINED CMAKE_INSTALL_PREFIX)
        set(CMAKE_INSTALL_PREFIX "C:/Program Files (x86)/GLFW")
      endif()
      

      这是默认的 CMAKE_INSTALL_PREFIX 路径。它告诉 make 程序在哪里安装依赖项:将其更改为您想要的文件夹,但请记住它不会被创建,它必须已经存在(并且不要忘记更改反斜杠 \斜线 /)。现在 运行 mingw32-make install。您应该在您提供的文件夹中看到 includelib 文件夹。

  3. 正在编辑 CMakeLists.txt :

    1. 现在您已经构建了您的库,您需要编辑您的 CMakeLists.txt 文件以便 link GLFW 到您的CMake 项目。典型的未修改 CMakeLists.txt 文件如下所示:

      cmake_minimum_required(VERSION 3.17)
      project(PROJECT_NAME)
      
      set(CMAKE_CXX_STANDARD 17)
      
      add_executable(PROJECT_NAME main.cpp)
      

      您需要 link 的是首先设置 CMAKE_PREFIX_PATH 变量。就我而言,我在 add_executable(..) 之前添加了这一行:

      set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib/cmake/glfw3")
      

      这是因为我想在我的项目文件夹中包含 GLFW 依赖项。但是,如果您也输入绝对路径,它应该可以正常工作:重要的是该文件夹包含 glfw3Config.cmakeglfw3Targets.cmake 文件(如果你正确构建了库,你应该在 build_folder/lib/cmake/glfw3/ 下找到它们)。

      现在您已经告诉 CMake 在何处查找 glfw3Config.cmake 文件,您需要 link 项目的库。您可以通过在 add_executable(..) 命令后添加来执行此操作:

      find_package(glfw3 REQUIRED)
      target_link_libraries(PROJECT_NAME glfw)
      

      如果这给你一个像

      这样的错误
      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.2) 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.
      

      glfw3REQUIRED之间添加CONFIG(详见here)。

    2. 您需要做的最后一件事是在 GLFW 构建文件夹中添加带有 include 路径的行 include_directories(Dependencies/GLFW/include/GLFW/)CMakeLists.txt 现在应该是这样的:

      cmake_minimum_required(VERSION 3.17)
      project(PROJECT_NAME)
      
      set(CMAKE_CXX_STANDARD 17)
      set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib/cmake/glfw3")
      
      add_executable(PROJECT_NAME main.cpp)
      
      include_directories(Dependencies/GLFW/include/GLFW/)
      
      find_package(glfw3 REQUIRED)
      target_link_libraries(PROJECT_NAME glfw)
      

      如果你顺利到达这里,恭喜你。您已成功 linked GLFW 到您的 CMake 项目! 现在最后一件事是启动您的项目并 运行ning 并能够编写一些基本的 OpenGL 代码:更改 find_package 的一部分:

      find_package(OpenGL REQUIRED)
      find_package(glfw3 REQUIRED)
      target_link_libraries(PROJECT_NAME opengl32 glfw)
      

      如您所料,这会将 link OpenGL 添加到项目中。不需要做更多的事情,因为 OpenGL DLL(opengl32.dllglu32.dll)应该已经在 C:\Windows\System32.

我希望这个答案能帮助人们安装 GLFW,而无需进行一天的论坛 QA 滚动、文档阅读和 Youtube 教程观看。