您的FindGLFW.cmake
文件不足。事实上,它根本不应该被需要或存在。GLFW 已经提供了一个配置包。以下是您应该如何构建和使用任何表现良好的 CMake 包的方法:
首先,获取源代码,构建包,并将其安装到本地前缀:
$ git clone https://github.com/glfw/glfw
Cloning into 'glfw'...
remote: Enumerating objects: 27050, done.
remote: Counting objects: 100% (96/96), done.
remote: Compressing objects: 100% (66/66), done.
remote: Total 27050 (delta 40), reused 54 (delta 24), pack-reused 26954
Receiving objects: 100% (27050/27050), 13.27 MiB | 2.87 MiB/s, done.
Resolving deltas: 100% (19047/19047), done.
$ cd glfw/
glfw$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B build
...
-- Generating done
-- Build files have been written to: /path/to/glfw/build
glfw$ cmake --build build
...
glfw$ cmake --install build --prefix install
-- Install configuration: "Release"
-- Installing: /path/to/glfw/install/lib/libglfw3.a
-- Installing: /path/to/glfw/install/include/GLFW
-- Installing: /path/to/glfw/install/include/GLFW/glfw3.h
-- Installing: /path/to/glfw/install/include/GLFW/glfw3native.h
-- Installing: /path/to/glfw/install/lib/cmake/glfw3/glfw3Config.cmake
-- Installing: /path/to/glfw/install/lib/cmake/glfw3/glfw3ConfigVersion.cmake
-- Installing: /path/to/glfw/install/lib/cmake/glfw3/glfw3Targets.cmake
-- Installing: /path/to/glfw/install/lib/cmake/glfw3/glfw3Targets-release.cmake
...
然后为您的示例项目创建一个文件夹并将您的 CMakeLists.txt 放在那里:
glfw$ mkdir ../glfw-test ; cd ../glfw-test
glfw-test$ vim CMakeLists.txt
glfw-test$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(example)
find_package(glfw3 REQUIRED)
message(STATUS "GLFW: ${glfw3_FOUND}")
if (TARGET glfw)
message(STATUS "Target exists")
else ()
message(STATUS "Target does not exist")
endif()
然后你可以构建它并使用<pkg>_ROOT
变量告诉 CMake 在哪里可以找到glfw3
. 如果您在系统范围内安装了软件包,则无需设置此变量。
glfw-test$ cmake -S . -B build -Dglfw3_ROOT=/path/to/glfw/install
...
-- GLFW: 1
-- Target exists
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/glfw-test/build