41

I have 2 versions of OpenCV installed on my machine. One is in /usr/local/opencv3.1. I presume the install location of the other one (version 3.4) is /usr/local. Anyway, find_package(OpenCV 3.0 REQUIRED) sets OpenCV_DIR:PATH=/usr/local/share/OpenCV. This folder contains:

haarcascades  OpenCVConfig.cmake          OpenCVModules-release.cmake
java          OpenCVConfig-version.cmake  valgrind_3rdparty.supp
lbpcascades   OpenCVModules.cmake         valgrind.supp

In this case, version 3.4 is used. How can I specify in CMakeLists.txt to use the other version (3.1) knowing its install location? I've tried:

find_package(OpenCV 3.0 REQUIRED PATH /usr/local/opencv3.1)

Which returns an error:

Could NOT find OpenCV (missing: PATH /usr/local/opencv3.1) (found suitable version "3.4.1", minimum required is "3.0")

and

set(OpenCV_DIR /usr/local/opencv3.1/OpenCV/*) # also tried OpenCV_ROOT_DIR, OPENCV_ROOT_DIR
find_package(OpenCV 3.0 REQUIRED)

Which does nothing. It still finds version 3.4. I'd be grateful for any help. Thank you.

Gerry
  • 1,938
  • 3
  • 18
  • 25

1 Answers1

56

In the find_package documentation you have that you can set a path to be searched with PATHS you were missing the S... also you can do something like:

find_package (<package> PATHS paths... NO_DEFAULT_PATH)
find_package (<package>)

Which will check for the path you wrote first, the if it is found it will set found to true and the second instruction will be skipped.

Also, you can use the EXACT option to match an specific version, in case it tries to select 3.4 due to being a newer version.

find_package(OpenCV 3.1 EXACT REQUIRED PATHS /usr/local/opencv3.1)

I hope this helps, if not, write a comment

api55
  • 11,070
  • 4
  • 41
  • 57
  • find_package(OpenCV 3.1 EXACT REQUIRED PATHS /usr/local/opencv3.1) works in my case. Thank you. – Gerry Apr 13 '18 at 12:09
  • 1
    I just tested this, and still didn't work, until I commented the second line (i.e., only worked when there was JUST the option with the specified path) – Bersan Feb 22 '19 at 00:43
  • @Bersan The documentation says to use it multiple times... Normally I only use the first one but without `NO_DEFAULT_PATH` when I need one library in a specific path and usually it works good.... However I used it for libraries that are not installed in my system but just compiled.... – api55 Feb 22 '19 at 08:33
  • 3
    it works for me, if I set ```set(SDL2_DIR /usr/local/lib/cmake/SDL2)``` before find_package(SDL2 REQUIRED) – leanid.chaika Mar 29 '20 at 19:05
  • Also works for OpenCV 4.4 which is installed in /usr/local/ (default installed from source) My command: find_package(OpenCV 4.4 EXACT REQUIRED PATHS /usr/local/) Thanks!!! – willSapgreen Apr 11 '22 at 02:03