cmake find_package()会忽略< PackageName>_ROOT

e5nqia27  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(197)

在我的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.
cs7cruho

cs7cruho1#

因此,问题实际上是,对于Android(可能还有iOS),CMake将CMAKE_FIND_ROOT_PATH_MODE_PACKAGE变量(和其他CMAKE_FIND_ROOT_PATH_MODE_*变量)设置为ONLY,这使得find_package(和其他find_*函数)将CMAKE_FIND_ROOT_PATH列表中的路径作为前缀。
要覆盖此设置,可以执行以下操作:

  • CMAKE_FIND_ROOT_PATH_MODE_*设置为BOTHNEVER
  • 对每个find_*调用使用CMAKE_FIND_ROOT_PATH_BOTHNO_CMAKE_FIND_ROOT_PATH

它还应该修复CMake忽略来自HINTSPATHS和其他前缀构造步骤的路径时的问题。

相关问题