find_package() description may add some detail
description
when cmake>=3.12, find_package
in CONFIG MODE will firstly search <PackageName>_ROOT
cmake variable, e.g. OpenCV_ROOT
. However, if I also define <PackageName>_DIR
cmake variable, <PackageName>_ROOT
will not work for find_package()
.
example
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(hello_cmake)
set(OpenCV_ROOT "/path/to/opencv_249/build")
set(OpenCV_DIR "/path/to/opencv_300/build")
find_package(OpenCV QUIET
NO_MODULE
NO_DEFAULT_PATH
NO_CMAKE_PATH
NO_CMAKE_ENVIRONMENT_PATH
NO_SYSTEM_ENVIRONMENT_PATH
NO_CMAKE_PACKAGE_REGISTRY
NO_CMAKE_BUILDS_PATH
NO_CMAKE_SYSTEM_PATH
NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
This will actually find opencv300, i.e. OpenCV_DIR
cmake variable works before OpenCV_ROOT
cmake variable.
Meanwhile, the cmake documentation(say, 3.14) gives this for find_package()
's CONFIG MODE's first searched PATH:
Search paths specified in the _ROOT CMake variable and the _ROOT environment variable, where is the package to be found. The package root variables are maintained as a stack so if called from within a find module, root paths from the parent’s find module will also be searched after paths for the current package. This can be skipped if NO_PACKAGE_ROOT_PATH is passed. See policy CMP0074.
I'd like to recommend improve this description: if <PackageName>_DIR
cmake variable is already set, then <PackageName>_ROOT
won't work.