如果安装了多个版本,cmake find_package(X [version]) 总是选择 X 的最高版本?
cmake find_package(X [version]) always selects highest version of X if multiple versions installed?
我是 运行 cmake 3.23.0。我想 find_library(VTK) 基于版本标准。我的ubuntu 20.04系统安装了两个版本的VTK:
/usr/local/lib/cmake/vtk-8.2
/usr/local/lib/cmake/vtk-9.0
我的项目需要 VTK 版本 8.x.x,即需要排除 VTK 9.x.x - 所以 CMakeLists.txt 包括:
find_package(VTK "8...<9" REQUIRED)
但是 VTK_VERSION 发现是 9.0.1 - 为什么,给定约束?
也尝试过
find_package(VTK "8.0...<9.0" REQUIRED)
find_package(VTK "8.0.0...<9.0.0" REQUIRED)
结果相同 - VTK_VERSION 9.0.1 被发现。在没有版本范围的情况下尝试过,但仍然 find_package() returns VTK_VERSION 9.0.1:
find_package(VTK "8" REQUIRED)
find_package(VTK “8.2” REQUIRED)
find_package(VTK “8.2.0” REQUIRED)
唯一实际 returns VTK_VERSION 8.2.0:
find_package(VTK “8.2.0” REQUIRED EXACT)
此行为是因为我的系统上安装了两个 VTK 版本,而 find_packages() 假设只安装了一个吗?
*** 更新 #2:/usr/local/lib/cmake/vtk- 中版本配置文件的内容。:
/usr/local/lib/cmake/vtk-8.2/VTKConfigVersion.cmake:
# This is a basic version file for the Config-mode of find_package().
# It is used by write_basic_package_version_file() as input file for configure_file()
# to create a version-file which can be installed along a config.cmake file.
#
# The created file sets PACKAGE_VERSION_EXACT if the current version string and
# the requested version string are exactly the same and it sets
# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version,
# but only if the requested major version is the same as the current one.
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "8.2.0")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
if("8.2.0" MATCHES "^([0-9]+)\.")
set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
else()
set(CVF_VERSION_MAJOR "8.2.0")
endif()
if(PACKAGE_FIND_VERSION_MAJOR STREQUAL CVF_VERSION_MAJOR)
set(PACKAGE_VERSION_COMPATIBLE TRUE)
else()
set(PACKAGE_VERSION_COMPATIBLE FALSE)
endif()
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it:
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "")
return()
endif()
# check that the installed version has the same 32/64bit-ness as the one which is currently searching:
if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8")
math(EXPR installedBits "8 * 8")
set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)")
set(PACKAGE_VERSION_UNSUITABLE TRUE)
endif()
/usr/local/lib/cmake/vtk-9.0/vtk-config-version.cmake:
# This is a basic version file for the Config-mode of find_package().
# It is used by write_basic_package_version_file() as input file for configure_file()
# to create a version-file which can be installed along a config.cmake file.
#
# The created file sets PACKAGE_VERSION_EXACT if the current version string and
# the requested version string are exactly the same and it sets
# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version.
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "9.0.1")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
# if the installed project requested no architecture check, don't perform the check
if("FALSE")
return()
endif()
# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it:
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "")
return()
endif()
# check that the installed version has the same 32/64bit-ness as the one which is currently searching:
if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8")
math(EXPR installedBits "8 * 8")
set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)")
set(PACKAGE_VERSION_UNSUITABLE TRUE)
endif()
*** 更新 #1:使用 --debug-find-pkg=VTK 的 CMake 输出:
CMake Debug Log at CMakeLists.txt:212 (find_package):
find_package considered the following paths for VTK.cmake
/home/oreilly/projects/mb-system/MB-System/build-utils/FindVTK.cmake
/usr/local/share/cmake-3.23/Modules/FindVTK.cmake
The file was not found.
<PackageName>_ROOT CMake variable [CMAKE_FIND_USE_PACKAGE_ROOT_PATH].
none
CMAKE_PREFIX_PATH variable [CMAKE_FIND_USE_CMAKE_PATH].
none
CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH variables
[CMAKE_FIND_USE_CMAKE_PATH].
none
Env variable VTK_DIR [CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
CMAKE_PREFIX_PATH env variable [CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH env variables
[CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
Paths specified by the find_package HINTS option.
none
Standard system environment variables
[CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH].
/home/oreilly/OpenSceneGraph/build
/home/oreilly/projects/mb-system/MB-System/build
/home/oreilly/projects/hotspot/onboard/smc-script
/home/oreilly/projects/hotspot/onboard/script
/home/oreilly/myUtilities
/home/oreilly/cxxtest-4.3
/home/oreilly/Qt/Tools/QtCreator
/home/oreilly/Qt/5.14.2/gcc_64
/home/oreilly/Android/Sdk/platform-tools
/home/oreilly/android-studio/tools
/home/oreilly/android-studio
/home/oreilly/idea
/home/oreilly
/home/oreilly/.local
/usr/local
/usr
/
/usr/games
/usr/local/games
/snap
/home/oreilly/anaconda2
CMake User Package Registry [CMAKE_FIND_USE_PACKAGE_REGISTRY].
none
CMake variables defined in the Platform file
[CMAKE_FIND_USE_CMAKE_SYSTEM_PATH].
/usr/X11R6
/usr/pkg
/opt
CMake System Package Registry
[CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY].
none
Paths specified by the find_package PATHS option.
none
find_package considered the following locations for the Config module:
/home/oreilly/OpenSceneGraph/build/VTKConfig.cmake
/home/oreilly/OpenSceneGraph/build/vtk-config.cmake
/home/oreilly/projects/mb-system/MB-System/build/VTKConfig.cmake
/home/oreilly/projects/mb-system/MB-System/build/vtk-config.cmake
/home/oreilly/projects/hotspot/onboard/smc-script/VTKConfig.cmake
/home/oreilly/projects/hotspot/onboard/smc-script/vtk-config.cmake
/home/oreilly/projects/hotspot/onboard/script/VTKConfig.cmake
/home/oreilly/projects/hotspot/onboard/script/vtk-config.cmake
/home/oreilly/myUtilities/VTKConfig.cmake
/home/oreilly/myUtilities/vtk-config.cmake
/home/oreilly/cxxtest-4.3/VTKConfig.cmake
/home/oreilly/cxxtest-4.3/vtk-config.cmake
/home/oreilly/Qt/Tools/QtCreator/VTKConfig.cmake
/home/oreilly/Qt/Tools/QtCreator/vtk-config.cmake
/home/oreilly/Qt/5.14.2/gcc_64/VTKConfig.cmake
/home/oreilly/Qt/5.14.2/gcc_64/vtk-config.cmake
/home/oreilly/Android/Sdk/platform-tools/VTKConfig.cmake
/home/oreilly/Android/Sdk/platform-tools/vtk-config.cmake
/home/oreilly/android-studio/tools/VTKConfig.cmake
/home/oreilly/android-studio/tools/vtk-config.cmake
/home/oreilly/android-studio/VTKConfig.cmake
/home/oreilly/android-studio/vtk-config.cmake
/home/oreilly/idea/VTKConfig.cmake
/home/oreilly/idea/vtk-config.cmake
/home/oreilly/VTKConfig.cmake
/home/oreilly/vtk-config.cmake
/home/oreilly/CMake/VTKConfig.cmake
/home/oreilly/CMake/vtk-config.cmake
/home/oreilly/vtk/VTKConfig.cmake
/home/oreilly/vtk/vtk-config.cmake
/home/oreilly/VTK-8.2.0/VTKConfig.cmake
/home/oreilly/VTK-8.2.0/vtk-config.cmake
/home/oreilly/VTKWikiExamples-master/VTKConfig.cmake
/home/oreilly/VTKWikiExamples-master/vtk-config.cmake
/home/oreilly/VTK-9.0.1/VTKConfig.cmake
/home/oreilly/VTK-9.0.1/vtk-config.cmake
/home/oreilly/VTKExamples/VTKConfig.cmake
/home/oreilly/VTKExamples/vtk-config.cmake
/home/oreilly/vtk/CMake/VTKConfig.cmake
/home/oreilly/vtk/CMake/vtk-config.cmake
/home/oreilly/VTK-8.2.0/CMake/VTKConfig.cmake
/home/oreilly/VTK-8.2.0/CMake/vtk-config.cmake
/home/oreilly/VTKWikiExamples-master/CMake/VTKConfig.cmake
/home/oreilly/VTKWikiExamples-master/CMake/vtk-config.cmake
/home/oreilly/VTK-9.0.1/CMake/VTKConfig.cmake
/home/oreilly/VTK-9.0.1/CMake/vtk-config.cmake
/home/oreilly/VTKExamples/CMake/VTKConfig.cmake
/home/oreilly/VTKExamples/CMake/vtk-config.cmake
/home/oreilly/.local/VTKConfig.cmake
/home/oreilly/.local/vtk-config.cmake
/usr/local/VTKConfig.cmake
/usr/local/vtk-config.cmake
/usr/local/lib/cmake/vtk-9.0/VTKConfig.cmake
/usr/local/lib/cmake/vtk-9.0/vtk-config.cmake
The file was found at
/usr/local/lib/cmake/vtk-9.0/vtk-config.cmake
CMake Debug Log at CMakeLists.txt:212 (find_package):
Found VTK package version 9.0.1, VTK_LIBRARIES: VTK::WrappingTools;VTK::ViewsQt;[et cetera...]
find_package
根据 search procedure 选择它找到的第一个包。在该搜索过程中打破关系时,它按文件系统顺序进行。这取决于您的文件系统,因此未指定。
对于 version selection,它 询问包的 *-config-version.cmake
或 *ConfigVersion.cmake
脚本包是否与请求的版本兼容。没有此类脚本的包被假定为 不兼容 。旧包通常不理解版本范围,并且倾向于将 8.0.0...<9.0.0
之类的表达式简单地视为 8.0.0
.
您从 --debug-find-pkg=VTK
输出中看到的是您的文件系统在 VTK 8 之前订购 VTK 9。也许那是因为它较新或碰巧分配了较小的 inode,或者谁知道呢?不保证按字母顺序排列。
然后它首先考虑 VTK 9 并加载 vtk-9.0/vtk-config-version.cmake
文件。正如你在这里看到的:
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
它只检查当前版本 (9.0.1) 是否低于请求的版本(任何以 8
开头的版本字符串)。由于 9 不 小于 8,因此 PACKAGE_VERSION_COMPATIBLE
变量设置为 true 并且包被接受。
当您指定 EXACT
时,STREQUAL
比较将 return 为假,因此 PACKAGE_VERSION_EXACT
不会设置为 TRUE
,因此包是拒绝并考虑 VTK 8 包。
这是 VTK 9 方面的不幸行为。它不够复杂,无法解释版本范围参数,并且它声称与所有以前的版本向后兼容。
要在您的案例中强制使用 VTK 8,您应该编写以下内容:
find_package(VTK 8...<9 REQUIRED)
if (VTK_VERSION VERSION_GREATER_EQUAL 9)
message(FATAL_ERROR "This application requires VTK 8.x, but VTK ${VTK_VERSION} was found. Please set VTK_DIR to a folder containing the VTKConfig.cmake file from a VTK 8 installation.")
endif ()
然后就可以在命令行设置-DVTK_DIR=/usr/local/lib/cmake/vtk-8.2
我是 运行 cmake 3.23.0。我想 find_library(VTK) 基于版本标准。我的ubuntu 20.04系统安装了两个版本的VTK:
/usr/local/lib/cmake/vtk-8.2
/usr/local/lib/cmake/vtk-9.0
我的项目需要 VTK 版本 8.x.x,即需要排除 VTK 9.x.x - 所以 CMakeLists.txt 包括:
find_package(VTK "8...<9" REQUIRED)
但是 VTK_VERSION 发现是 9.0.1 - 为什么,给定约束? 也尝试过
find_package(VTK "8.0...<9.0" REQUIRED)
find_package(VTK "8.0.0...<9.0.0" REQUIRED)
结果相同 - VTK_VERSION 9.0.1 被发现。在没有版本范围的情况下尝试过,但仍然 find_package() returns VTK_VERSION 9.0.1:
find_package(VTK "8" REQUIRED)
find_package(VTK “8.2” REQUIRED)
find_package(VTK “8.2.0” REQUIRED)
唯一实际 returns VTK_VERSION 8.2.0:
find_package(VTK “8.2.0” REQUIRED EXACT)
此行为是因为我的系统上安装了两个 VTK 版本,而 find_packages() 假设只安装了一个吗?
*** 更新 #2:/usr/local/lib/cmake/vtk- 中版本配置文件的内容。:
/usr/local/lib/cmake/vtk-8.2/VTKConfigVersion.cmake:
# This is a basic version file for the Config-mode of find_package().
# It is used by write_basic_package_version_file() as input file for configure_file()
# to create a version-file which can be installed along a config.cmake file.
#
# The created file sets PACKAGE_VERSION_EXACT if the current version string and
# the requested version string are exactly the same and it sets
# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version,
# but only if the requested major version is the same as the current one.
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "8.2.0")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
if("8.2.0" MATCHES "^([0-9]+)\.")
set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
else()
set(CVF_VERSION_MAJOR "8.2.0")
endif()
if(PACKAGE_FIND_VERSION_MAJOR STREQUAL CVF_VERSION_MAJOR)
set(PACKAGE_VERSION_COMPATIBLE TRUE)
else()
set(PACKAGE_VERSION_COMPATIBLE FALSE)
endif()
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it:
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "")
return()
endif()
# check that the installed version has the same 32/64bit-ness as the one which is currently searching:
if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8")
math(EXPR installedBits "8 * 8")
set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)")
set(PACKAGE_VERSION_UNSUITABLE TRUE)
endif()
/usr/local/lib/cmake/vtk-9.0/vtk-config-version.cmake:
# This is a basic version file for the Config-mode of find_package().
# It is used by write_basic_package_version_file() as input file for configure_file()
# to create a version-file which can be installed along a config.cmake file.
#
# The created file sets PACKAGE_VERSION_EXACT if the current version string and
# the requested version string are exactly the same and it sets
# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version.
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "9.0.1")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
# if the installed project requested no architecture check, don't perform the check
if("FALSE")
return()
endif()
# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it:
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "")
return()
endif()
# check that the installed version has the same 32/64bit-ness as the one which is currently searching:
if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8")
math(EXPR installedBits "8 * 8")
set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)")
set(PACKAGE_VERSION_UNSUITABLE TRUE)
endif()
*** 更新 #1:使用 --debug-find-pkg=VTK 的 CMake 输出:
CMake Debug Log at CMakeLists.txt:212 (find_package):
find_package considered the following paths for VTK.cmake
/home/oreilly/projects/mb-system/MB-System/build-utils/FindVTK.cmake
/usr/local/share/cmake-3.23/Modules/FindVTK.cmake
The file was not found.
<PackageName>_ROOT CMake variable [CMAKE_FIND_USE_PACKAGE_ROOT_PATH].
none
CMAKE_PREFIX_PATH variable [CMAKE_FIND_USE_CMAKE_PATH].
none
CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH variables
[CMAKE_FIND_USE_CMAKE_PATH].
none
Env variable VTK_DIR [CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
CMAKE_PREFIX_PATH env variable [CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH env variables
[CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].
none
Paths specified by the find_package HINTS option.
none
Standard system environment variables
[CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH].
/home/oreilly/OpenSceneGraph/build
/home/oreilly/projects/mb-system/MB-System/build
/home/oreilly/projects/hotspot/onboard/smc-script
/home/oreilly/projects/hotspot/onboard/script
/home/oreilly/myUtilities
/home/oreilly/cxxtest-4.3
/home/oreilly/Qt/Tools/QtCreator
/home/oreilly/Qt/5.14.2/gcc_64
/home/oreilly/Android/Sdk/platform-tools
/home/oreilly/android-studio/tools
/home/oreilly/android-studio
/home/oreilly/idea
/home/oreilly
/home/oreilly/.local
/usr/local
/usr
/
/usr/games
/usr/local/games
/snap
/home/oreilly/anaconda2
CMake User Package Registry [CMAKE_FIND_USE_PACKAGE_REGISTRY].
none
CMake variables defined in the Platform file
[CMAKE_FIND_USE_CMAKE_SYSTEM_PATH].
/usr/X11R6
/usr/pkg
/opt
CMake System Package Registry
[CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY].
none
Paths specified by the find_package PATHS option.
none
find_package considered the following locations for the Config module:
/home/oreilly/OpenSceneGraph/build/VTKConfig.cmake
/home/oreilly/OpenSceneGraph/build/vtk-config.cmake
/home/oreilly/projects/mb-system/MB-System/build/VTKConfig.cmake
/home/oreilly/projects/mb-system/MB-System/build/vtk-config.cmake
/home/oreilly/projects/hotspot/onboard/smc-script/VTKConfig.cmake
/home/oreilly/projects/hotspot/onboard/smc-script/vtk-config.cmake
/home/oreilly/projects/hotspot/onboard/script/VTKConfig.cmake
/home/oreilly/projects/hotspot/onboard/script/vtk-config.cmake
/home/oreilly/myUtilities/VTKConfig.cmake
/home/oreilly/myUtilities/vtk-config.cmake
/home/oreilly/cxxtest-4.3/VTKConfig.cmake
/home/oreilly/cxxtest-4.3/vtk-config.cmake
/home/oreilly/Qt/Tools/QtCreator/VTKConfig.cmake
/home/oreilly/Qt/Tools/QtCreator/vtk-config.cmake
/home/oreilly/Qt/5.14.2/gcc_64/VTKConfig.cmake
/home/oreilly/Qt/5.14.2/gcc_64/vtk-config.cmake
/home/oreilly/Android/Sdk/platform-tools/VTKConfig.cmake
/home/oreilly/Android/Sdk/platform-tools/vtk-config.cmake
/home/oreilly/android-studio/tools/VTKConfig.cmake
/home/oreilly/android-studio/tools/vtk-config.cmake
/home/oreilly/android-studio/VTKConfig.cmake
/home/oreilly/android-studio/vtk-config.cmake
/home/oreilly/idea/VTKConfig.cmake
/home/oreilly/idea/vtk-config.cmake
/home/oreilly/VTKConfig.cmake
/home/oreilly/vtk-config.cmake
/home/oreilly/CMake/VTKConfig.cmake
/home/oreilly/CMake/vtk-config.cmake
/home/oreilly/vtk/VTKConfig.cmake
/home/oreilly/vtk/vtk-config.cmake
/home/oreilly/VTK-8.2.0/VTKConfig.cmake
/home/oreilly/VTK-8.2.0/vtk-config.cmake
/home/oreilly/VTKWikiExamples-master/VTKConfig.cmake
/home/oreilly/VTKWikiExamples-master/vtk-config.cmake
/home/oreilly/VTK-9.0.1/VTKConfig.cmake
/home/oreilly/VTK-9.0.1/vtk-config.cmake
/home/oreilly/VTKExamples/VTKConfig.cmake
/home/oreilly/VTKExamples/vtk-config.cmake
/home/oreilly/vtk/CMake/VTKConfig.cmake
/home/oreilly/vtk/CMake/vtk-config.cmake
/home/oreilly/VTK-8.2.0/CMake/VTKConfig.cmake
/home/oreilly/VTK-8.2.0/CMake/vtk-config.cmake
/home/oreilly/VTKWikiExamples-master/CMake/VTKConfig.cmake
/home/oreilly/VTKWikiExamples-master/CMake/vtk-config.cmake
/home/oreilly/VTK-9.0.1/CMake/VTKConfig.cmake
/home/oreilly/VTK-9.0.1/CMake/vtk-config.cmake
/home/oreilly/VTKExamples/CMake/VTKConfig.cmake
/home/oreilly/VTKExamples/CMake/vtk-config.cmake
/home/oreilly/.local/VTKConfig.cmake
/home/oreilly/.local/vtk-config.cmake
/usr/local/VTKConfig.cmake
/usr/local/vtk-config.cmake
/usr/local/lib/cmake/vtk-9.0/VTKConfig.cmake
/usr/local/lib/cmake/vtk-9.0/vtk-config.cmake
The file was found at
/usr/local/lib/cmake/vtk-9.0/vtk-config.cmake
CMake Debug Log at CMakeLists.txt:212 (find_package):
Found VTK package version 9.0.1, VTK_LIBRARIES: VTK::WrappingTools;VTK::ViewsQt;[et cetera...]
find_package
根据 search procedure 选择它找到的第一个包。在该搜索过程中打破关系时,它按文件系统顺序进行。这取决于您的文件系统,因此未指定。
对于 version selection,它 询问包的 *-config-version.cmake
或 *ConfigVersion.cmake
脚本包是否与请求的版本兼容。没有此类脚本的包被假定为 不兼容 。旧包通常不理解版本范围,并且倾向于将 8.0.0...<9.0.0
之类的表达式简单地视为 8.0.0
.
您从 --debug-find-pkg=VTK
输出中看到的是您的文件系统在 VTK 8 之前订购 VTK 9。也许那是因为它较新或碰巧分配了较小的 inode,或者谁知道呢?不保证按字母顺序排列。
然后它首先考虑 VTK 9 并加载 vtk-9.0/vtk-config-version.cmake
文件。正如你在这里看到的:
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
它只检查当前版本 (9.0.1) 是否低于请求的版本(任何以 8
开头的版本字符串)。由于 9 不 小于 8,因此 PACKAGE_VERSION_COMPATIBLE
变量设置为 true 并且包被接受。
当您指定 EXACT
时,STREQUAL
比较将 return 为假,因此 PACKAGE_VERSION_EXACT
不会设置为 TRUE
,因此包是拒绝并考虑 VTK 8 包。
这是 VTK 9 方面的不幸行为。它不够复杂,无法解释版本范围参数,并且它声称与所有以前的版本向后兼容。
要在您的案例中强制使用 VTK 8,您应该编写以下内容:
find_package(VTK 8...<9 REQUIRED)
if (VTK_VERSION VERSION_GREATER_EQUAL 9)
message(FATAL_ERROR "This application requires VTK 8.x, but VTK ${VTK_VERSION} was found. Please set VTK_DIR to a folder containing the VTKConfig.cmake file from a VTK 8 installation.")
endif ()
然后就可以在命令行设置-DVTK_DIR=/usr/local/lib/cmake/vtk-8.2