I have two Opencvs, opencv-2.4.10 is installed in /usr and opencv-3.1 is installed in /usr/local. I can set the opencv path in Eclipse easily. In cmake, how can I set the path for one of the opencvs I like to use for that project? Thanks
-
2Possible duplicate of [How to force c++ compiler use one of different installed package's versions, using CMake?](https://stackoverflow.com/questions/28507214/how-to-force-c-compiler-use-one-of-different-installed-packages-versions-usi) – Gabriel Devillers Jun 26 '17 at 08:58
3 Answers
This question is a duplicate of How to force c++ compiler use one of different installed package's versions, using CMake? but it is found by search engines so I add the answer from texasflood that I found to be working with CMake 2.8.12.2 and OpenCV 3.2.0 on Ubuntu 12.4:
Simply change the find_package
command:
find_package(OpenCV REQUIRED PATHS "/usr/opencv-2.4.10")

- 3,155
- 2
- 30
- 53
None of the other answers worked for me (linux, opencv 4.0.0, haven't tested yet with opencv 3.x.x).
Adding the following before the line find_package(...)
in the CMakeLists.txt worked:
set(OpenCV_DIR /path/to/opencv_install_dir/lib/cmake/opencv4)
If opencv was compiled from source with a none standard installation target directory, then /path/to/opencv_install_dir
is (should be) known, otherwise search for the location of the directory structure lib/cmake/opencv*
.

- 5,793
- 1
- 33
- 45

- 2,430
- 20
- 25
-
You might need to add double quotations for the dir path as below: `set(OpenCV_DIR "/path/to/opencv_dir/)"` – Aymen Alsaadi Mar 04 '21 at 19:38
I don't have a Linux machine to test it on but this is how I do it on Windows 10 64bit.
I only added a line in the CmakeLists.txt
in my program to set the variable OpenCV_DIR
used by CMake.
For example, if OpenCV is installed in C:/opencv
, I add
SET("OpenCV_DIR" "C:/opencv")
in my CMakeLists.txt
.
In your case, it should be something like
SET("OpenCV_DIR" "/usr/opencv-2.4.10")
But, as I said, I never tested this on Linux.

- 4,452
- 21
- 33
-
No not yet. I set as SET("OpenCV_DIR" "/usr/share/OpenCV"). But it doesn't link. – batuman Jun 08 '16 at 01:21
-
@batuman Make sure you also have the lines: find_package(OpenCV REQUIRED), include_directories(${OpenCV_INCLUDE_DIRS}) and target_link_libraries(Program ${OpenCV_LIBS}) in your CMakeLists. Otherwise it's not going to do anything – Sunreef Jun 08 '16 at 05:59
-
Make sure also, that the directory you specified is the directory where OpenCV is built, not the sources. – Sunreef Jun 08 '16 at 06:06
-
Did not work for me (CMake 2.8.12.2, OpenCV 3.2.0, Ubuntu 12.4), see my answer instead. – Gabriel Devillers Jun 26 '17 at 09:14