find_package() 忽略 <PackageName>_ROOT
find_package() ignores <PackageName>_ROOT
在我的 CMake 脚本中,我将包含 <PackageName>Config.cmake
的文件夹的路径附加到 <PackageName>_ROOT
,然后调用 find_package(<PackageName> REQUIRED)
但它找不到我的包。
当我使用 CMAKE_FIND_DEBUG_MODE
时,我看到我的文件夹列在 <PackageName>_ROOT CMake variable [CMAKE_FIND_USE_PACKAGE_ROOT_PATH]
中(实际上它是那里唯一的文件夹),但它没有列在 find_package considered the following locations for the Config module:
之后。
为什么我会出现这种行为?我使用来自 Android Studio 的 CMake 3.18.1。我要查找的软件包是 OpenCV。
相对于我的CMakeLists.txt
文件OpenCV安装在../../build/opencv-build<custom suffix>
,所以我的查找代码是:
get_filename_component(OPENCV_BUILD_DIRS_ROOT ../../build REALPATH)
file(GLOB OPENCV_BUILD_DIRS ${OPENCV_BUILD_DIRS_ROOT}/opencv-build*)
list(APPEND OpenCV_ROOT ${OPENCV_BUILD_DIRS})
find_package(OpenCV ${OPENCV_VERSION} REQUIRED ${OPENCV_PUBLIC_LIBRARIES} ${OPENCV_PRIVATE_LIBRARIES})
当我在 Linux(实际上是 WSL)上 运行 时,一切正常,CMake 在 ../../build/opencv-build
(或 <project path>/build/opencv-build
中以绝对形式成功找到 OpenCV路径)。
但是当我尝试从 Android Studio 构建项目时它没有,我从 CMAKE_FIND_DEBUG_MODE
:
得到这个输出
CMake Debug Log at external/opencv/CMakeLists.txt:12 (find_package):
find_package considered the following paths for OpenCV.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/cmake/3.18.1/share/cmake-3.18/Modules/FindOpenCV.cmake
The file was not found.
<PackageName>_ROOT CMake variable [CMAKE_FIND_USE_PACKAGE_ROOT_PATH].
<project path>/build/opencv-build
CMAKE_PREFIX_PATH variable [CMAKE_FIND_USE_CMAKE_PATH].
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64
CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH variables
[CMAKE_FIND_USE_CMAKE_PATH].
Env variable OpenCV_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].
<a lot of unrelated directories>
CMake User Package Registry [CMAKE_FIND_USE_PACKAGE_REGISTRY].
none
CMake variables defined in the Platform file
[CMAKE_FIND_USE_CMAKE_SYSTEM_PATH].
C:/Users/<username>/AppData/Local/Android/Sdk/cmake/3.18.1
<other unrelated directories>
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:
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/i686-linux-android/21/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/i686-linux-android/21/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/opencv-config.cmake
The file was not found.
因为你提供了一个配置文件,你应该设置 <PackageName>_DIR
到你有配置的地方,而不是 <PackageName>_ROOT
(这是查找模块的提示,当你有配置路径)。
如果您不确切知道路径,或者想使用多个路径,您应该参考 docs 关于搜索过程的信息,因为有多种方法可以影响 CMake 搜索配置文件的位置(其中包括 <PackageName>_ROOT
、HINTS
、CMAKE_PREFIX_PATH
等)。
但是,如果您的配置无法通过默认设置找到并且您仍然需要进行提示,我看不出您不能只设置 <PackageName>_DIR
变量的任何原因。你知道你想要什么包,你知道它在哪里并且它是单一路径,没有理由使用任何其他变量。
唯一的原因可能是如果您有一个包含许多库的预期文件夹结构,但此树不位于默认位置,在这种情况下我会修改 CMAKE_PREFIX_PATH
因为只需修改一次即可找到你所有的库。
在我的 CMake 脚本中,我将包含 <PackageName>Config.cmake
的文件夹的路径附加到 <PackageName>_ROOT
,然后调用 find_package(<PackageName> REQUIRED)
但它找不到我的包。
当我使用 CMAKE_FIND_DEBUG_MODE
时,我看到我的文件夹列在 <PackageName>_ROOT CMake variable [CMAKE_FIND_USE_PACKAGE_ROOT_PATH]
中(实际上它是那里唯一的文件夹),但它没有列在 find_package considered the following locations for the Config module:
之后。
为什么我会出现这种行为?我使用来自 Android Studio 的 CMake 3.18.1。我要查找的软件包是 OpenCV。
相对于我的CMakeLists.txt
文件OpenCV安装在../../build/opencv-build<custom suffix>
,所以我的查找代码是:
get_filename_component(OPENCV_BUILD_DIRS_ROOT ../../build REALPATH)
file(GLOB OPENCV_BUILD_DIRS ${OPENCV_BUILD_DIRS_ROOT}/opencv-build*)
list(APPEND OpenCV_ROOT ${OPENCV_BUILD_DIRS})
find_package(OpenCV ${OPENCV_VERSION} REQUIRED ${OPENCV_PUBLIC_LIBRARIES} ${OPENCV_PRIVATE_LIBRARIES})
当我在 Linux(实际上是 WSL)上 运行 时,一切正常,CMake 在 ../../build/opencv-build
(或 <project path>/build/opencv-build
中以绝对形式成功找到 OpenCV路径)。
但是当我尝试从 Android Studio 构建项目时它没有,我从 CMAKE_FIND_DEBUG_MODE
:
CMake Debug Log at external/opencv/CMakeLists.txt:12 (find_package):
find_package considered the following paths for OpenCV.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/cmake/3.18.1/share/cmake-3.18/Modules/FindOpenCV.cmake
The file was not found.
<PackageName>_ROOT CMake variable [CMAKE_FIND_USE_PACKAGE_ROOT_PATH].
<project path>/build/opencv-build
CMAKE_PREFIX_PATH variable [CMAKE_FIND_USE_CMAKE_PATH].
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64
CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH variables
[CMAKE_FIND_USE_CMAKE_PATH].
Env variable OpenCV_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].
<a lot of unrelated directories>
CMake User Package Registry [CMAKE_FIND_USE_PACKAGE_REGISTRY].
none
CMake variables defined in the Platform file
[CMAKE_FIND_USE_CMAKE_SYSTEM_PATH].
C:/Users/<username>/AppData/Local/Android/Sdk/cmake/3.18.1
<other unrelated directories>
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:
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/i686-linux-android/21/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/i686-linux-android/21/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/opencv-config.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/OpenCVConfig.cmake
C:/Users/<username>/AppData/Local/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot/opencv-config.cmake
The file was not found.
因为你提供了一个配置文件,你应该设置 <PackageName>_DIR
到你有配置的地方,而不是 <PackageName>_ROOT
(这是查找模块的提示,当你有配置路径)。
如果您不确切知道路径,或者想使用多个路径,您应该参考 docs 关于搜索过程的信息,因为有多种方法可以影响 CMake 搜索配置文件的位置(其中包括 <PackageName>_ROOT
、HINTS
、CMAKE_PREFIX_PATH
等)。
但是,如果您的配置无法通过默认设置找到并且您仍然需要进行提示,我看不出您不能只设置 <PackageName>_DIR
变量的任何原因。你知道你想要什么包,你知道它在哪里并且它是单一路径,没有理由使用任何其他变量。
唯一的原因可能是如果您有一个包含许多库的预期文件夹结构,但此树不位于默认位置,在这种情况下我会修改 CMAKE_PREFIX_PATH
因为只需修改一次即可找到你所有的库。