cmake find_package, find_library 可以定位到glfw,但是TARGET glfw为false
cmake find_package, find_library can locate glfw, but TARGET glfw is false
我的 glfw3 是从源代码构建的,FindGLFW.cmake 用于查找库:
find_path(
GLFW_INCLUDE_DIR
NAMES
GLFW
PATHS
include)
find_library(
GLFW_LIBRARY
NAMES
glfw glfw3 glfw3dll
PATHS
lib)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW REQUIRED_VARS GLFW_LIBRARY GLFW_INCLUDE_DIR)
我可以在其他项目中使用 find_package(GLFW REQUIRED)
和 target_link_libraries(xxx glfw3)
,没有构建错误。但是,if(TARGET glfw)
或 if(TARGET glfw3)
中的语句总是给我错误的结果。可能是什么原因?
示例:
find_package(GLFW REQUIRED)
MESSAGE(STATUS "GLFW: ${GLFW_FOUND}")
if(TARGET glfw)
MESSAGE(STATUS "Found it")
else()
MESSAGE(STATUS "Not Found")
endif()
target_link_libraries(xxx glfw3)
这给了我 GLFW: TRUE
和 Not Found
输出,但没有构建错误。
What could be the reason?
您的 cmake 配置中没有名为 glfw
或 glfw3
.
的此类目标
请注意,target_link_libraries(xxx glfw3)
很可能正在与库 glfw3
链接,而忽略您的 find_package
调用。我认为你应该做 target_include_directories(xxx ${GLFW_INCLUDE_DIR})
和 target_link_libraries(xxx ${GLFW_LIBRARY})
.
您的 FindGLFW.cmake
文件不足。事实上,根本 不需要它或不存在它。 GLFW 已经提供了配置包。以下是构建和使用 any 行为良好的 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
我的 glfw3 是从源代码构建的,FindGLFW.cmake 用于查找库:
find_path(
GLFW_INCLUDE_DIR
NAMES
GLFW
PATHS
include)
find_library(
GLFW_LIBRARY
NAMES
glfw glfw3 glfw3dll
PATHS
lib)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW REQUIRED_VARS GLFW_LIBRARY GLFW_INCLUDE_DIR)
我可以在其他项目中使用 find_package(GLFW REQUIRED)
和 target_link_libraries(xxx glfw3)
,没有构建错误。但是,if(TARGET glfw)
或 if(TARGET glfw3)
中的语句总是给我错误的结果。可能是什么原因?
示例:
find_package(GLFW REQUIRED)
MESSAGE(STATUS "GLFW: ${GLFW_FOUND}")
if(TARGET glfw)
MESSAGE(STATUS "Found it")
else()
MESSAGE(STATUS "Not Found")
endif()
target_link_libraries(xxx glfw3)
这给了我 GLFW: TRUE
和 Not Found
输出,但没有构建错误。
What could be the reason?
您的 cmake 配置中没有名为 glfw
或 glfw3
.
请注意,target_link_libraries(xxx glfw3)
很可能正在与库 glfw3
链接,而忽略您的 find_package
调用。我认为你应该做 target_include_directories(xxx ${GLFW_INCLUDE_DIR})
和 target_link_libraries(xxx ${GLFW_LIBRARY})
.
您的 FindGLFW.cmake
文件不足。事实上,根本 不需要它或不存在它。 GLFW 已经提供了配置包。以下是构建和使用 any 行为良好的 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