find_package config search order

CMake 3.10/Ubuntu 18.04
I’m looking for a custom OpenCV installation
I set OpenCV_DIR environment variable to the path to the OpenCV.cmake directory
find_package finds the default installed version (in /usr/something…).

Looking more precisely at the documentation I misunderstand 2 points:

  • what is the actual function of the _DIR variable;
  • what is the <prefix> mentioned here?

What is the best way to give a client specific-path to locate a package (here, I’d prefer not to use PATH option and that the client sets some env variable that CMAKE can use to locate the desired installation of the package, when different installations exists)?


OpenCV_DIR should be set to the directory of the opencv-config.cmake file; OpenCV.cmake doesn’t mean anything to find_package. You can use cmake --debug-find to debug the find_ command routines.


Yes, I was speaking about opencv-config.cmake.
On CMake 3.10, I can’t find --debug-find command or an equivalent. Is there another way to debug? (It is indeed useful, I tested it on more recent version).

But actually setting the environment variable OpenCV_DIR works after cleaning cmake cache.

I suppose that there was already a local OpenCV_DIR variable in the cache, thus find_package was not looking into the environment variable version.


That could certainly do it.