81

I wrote a CMakeLists.txt for a project in C++, which uses OpenCV libraries. When I try to create the project using cmake, I get the next configuration problem:

CMake Error at CMakeLists.txt:15 (find_package):
  Could not find module FindOpenCV.cmake or a configuration file for package
  OpenCV.

  Adjust CMAKE_MODULE_PATH to find FindOpenCV.cmake or set OpenCV_DIR to the
  directory containing a CMake configuration file for OpenCV.  The file will
  have one of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

The fact is that I have an environment variable for the path which I use in Visual Studio with no problems. If I don't include OpenCV, then I can configure and generate with no problem, but I need to solve the problem. I don't understand why cmake cannot find the OpenCV path or how to fix it.

I also used the recommendations mentioned in this link: FindOpenCV.cmake

Does anybody had this problem too?

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164

14 Answers14

41

The error you're seeing is that CMake cannot find a FindOpenCV.cmake file, because cmake doesn't include one out of the box. Therefore you need to find one and put it where cmake can find it:

You can find a good start here. If you're feeling adventurous you can also write your own.

Then add it somewhere in your project and adjust CMAKE_MODULE_PATH so that cmake can find it.

e.g., if you have

CMakeLists.txt
cmake-modules/FindOpenCV.cmake

Then you should do a

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)

In your CMakeLists.txt file before you do a find_package(OpenCV)

Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48
alanxz
  • 1,966
  • 15
  • 17
  • 7
    Thanks, but I have tried all of this. I read forums ffrom people with similar problems and they suggested to set the paths. I downloaded the FindOpenCV, I defined the variables with the paths, I moved the OpenCV folder to different paths, I directly linked the opencvConfig.cmake, but the error is always the same. cmake cannot find my opencv folder! At least now I understand cmake language, xD. – Jav_Rock Jan 03 '12 at 16:28
  • 3
    Ok so cmake is actually using FindOpenCV.cmake? Now its having trouble finding OpenCV library/headers? – alanxz Jan 03 '12 at 16:44
  • 2
    No, just cannot find the OpenCV_DIR, even if I manually write it, even if I set a variable inside the cmakelists. – Jav_Rock Jan 03 '12 at 16:52
  • 3
    CMake is actually using FindOpenCV.cmake. Problem 1 solved. Have you tried setting it manually using a command line option like cmake -DOpenCV_DIR=/path/to/opencv ? Another option is to set this using ccmake or cmake-gui. – alanxz Jan 03 '12 at 16:57
  • 2
    I use cmake GUI, and I tried to manually modify the variable OpenCV_DIR in the graphic interface, but when I press configure the GUI says it cannot be found. I will keep trying and if I find the solution I will port it here. thanks. – Jav_Rock Jan 04 '12 at 08:04
  • 3
    Try to define CMAKE_PREFIX_PATH variable and set it to the dir, where OpenCV is installed. – arrowd Jan 04 '12 at 08:26
  • 2
    The problem was that my FindOpenCV.cmake wasn't installed. I had to run cmake on OpenCV folder, then the FindOpenCV.make appeared in the propper format and finally cmake could find the package as the CMAKE_MODULE_PATH was correct. – Jav_Rock Sep 06 '12 at 08:22
  • 1
    The link supplied after "Edit:" above (by @GWW?) is actually not to an example of FindOpenCV.cmake but to an example of OpenCVConfig.cmake, which has a slightly different purpose. I can see how one might confuse them though, since that is what I did initially. Modern CMake can actually do without FindOpenCV.cmake, which makes things a little simpler. See [my question and answer](http://stackoverflow.com/questions/39278472/what-obstacles-must-i-overcome-to-install-opencv-with-clion-under-windows/39278762#39278762) for details on Windows, some of which apply to CMake / OpenCV generally. – CODE-REaD Sep 02 '16 at 14:03
  • 1
    The answer works for me & the `OpenCVConfig.cmake` located at the OpenCV distribution. For example, [in official release binary for Windows](http://opencv.org/releases.html) could be located in `build\x64\vc14\lib\OpenCVConfig.cmake`. Just export it like `cmake .. -DOpenCV_DIR=\build\x64\vc14\lib` – N0dGrand87 Sep 29 '17 at 12:45
  • The answer worked for me, but only if I copied the suggested FindOpenCV.cmake to C:\OpenCV\build. Then, it can find the appropriate libraries by appending the architecture (e.g. x64), the runtime (e.g. vc14) and 'lib' to the path -> C:\OpenCV\build\x64\vc14\lib". In this folder, it can find the necessary OpenCVConfig.cmake – gebbissimo Mar 10 '20 at 12:44
  • On Ubuntu, despite having installed OpenCV (via apt-get), there is apparently no `OpenCVConfig.cmake` or `opencv-config.cmake` *anywhere* on my hard drive. – sh37211 Nov 19 '20 at 02:42
  • Doesn't that come with the -dev packages? It should... (linux) Yes, it does. Just install libopencv-dev (debian) or similar. – Sandburg Jan 15 '22 at 10:18
31

If you are on Linux, you just need to fill the OpenCV_DIR variable with the path of opencv (containing the OpenCVConfig.cmake file)

export OpenCV_DIR=<path_of_opencv>
leszek.hanusz
  • 5,152
  • 2
  • 38
  • 56
24
  1. apt-get install libopencv-dev
  2. export OpenCV_DIR=/usr/share/OpenCV
  3. the header of cpp file should contain: #include #include "opencv2/highgui/highgui.hpp"

#include #include

not original cv.h

gaoxincun
  • 241
  • 2
  • 2
20

find / -name "OpenCVConfig.cmake"

export OpenCV_DIR=/path/found/above

user6630590
  • 201
  • 2
  • 2
  • 6
    please provide some explanation with your answer – Milad Faridnia Jul 24 '16 at 04:00
  • `find / -name "OpenCVConfig.cmake"` searches your file system for that file. `export OpenCV_DIR=/path/found/above` lets CMake know where that file is located. – koko Apr 12 '21 at 04:54
17

I had this exact same problem. I fixed it by adding the following line to my FindOpenCV.cmake file. Put it anywhere at the top before the rest of the code.

set (OpenCV_DIR /home/cmake/opencv/compiled) #change the path to match your complied directory of opencv

Basically you are telling FindOpenCV.cmake where to find opencv files assuming the other compilation can find the FindOpenCV.cmake

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
penner
  • 2,707
  • 1
  • 37
  • 48
  • This worked for me. I set the path of OPENCV_DIR to that of the opencv build folder – user27665 Jun 18 '20 at 14:26
  • `_DIR` variables are designed to be set by the user at the command line. Even more importantly, you should _**never**_ put absolute paths to your _home folder_ (of all places!) in your CMakeLists.txt!! – Alex Reinking Jun 16 '21 at 19:27
6

I faced the same error. In my case this "OpenCVConfig.cmake" file is located in /usr/local/share/OpenCV. In CMakeLists.txt add the line

set(OpenCV_DIR /usr/local/share/OpenCV)

as suggested by the error message.

YuZ
  • 445
  • 5
  • 18
  • I had installed opencv in a new location /usr/local/opencv-3.3.0. I used set(OpenCV_DIR /usr/local/opencv-3.3.0/share/OpenCV). I didnt have to set CMAKE_MODULE_PATH. CMakeLists.txt :project( opencv_detect_version ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") cmake_minimum_required(VERSION 2.8) set(OpenCV_DIR /usr/local/opencv-3.3.0/share/OpenCV) find_package( OpenCV CONFIG REQUIRED ) add_executable( opencv_detect_version opencv_detect_version.cpp ) target_link_libraries( opencv_detect_version ${OpenCV_LIBS} ) – user27665 Oct 04 '17 at 05:45
  • on Ubuntu: `ls: cannot access '/usr/local/share/OpenCV': No such file or directory` – sh37211 Nov 19 '20 at 02:44
3

Another possibility is to denote where you can find OpenCV_DIR in the CMakeLists.txt file. For example, the following cmake scripts work for me:

cmake_minimum_required(VERSION 2.8)

project(performance_test)

set(OpenCV_STATIC ON)
set(OpenCV_CUDA OFF)
set(OpenCV_DIR "${CMAKE_SOURCE_DIR}/../install")

find_package(OpenCV REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})

link_directories(${OpenCV_LIB_DIR})

file(GLOB my_source_files ./src/*)

add_executable( performance_test ${my_source_files})

target_link_libraries(performance_test ${OpenCV_LIBS})

Just to remind that you should set OpenCV_STATIC and OpenCV_CUDA as well before you invoke OpenCVConfig.cmake. In my case the built library is static library that does not use CUDA.

feelfree
  • 11,175
  • 20
  • 96
  • 167
3

if you are on windows, you can add opencv path to OpenCV_DIR yourself. (OpenCV_DIR is in the red region)

the path is like "D:/opencv244/build".

you can find file "OpenCVConfig.cmake" under the path.

2

For me (on Ubuntu), I just run:

sudo apt-get install libopencv-dev
1

On my Fedora machine, when I typed "make" I got an error saying it could not find "cv.h". I fixed this by modifying my "OpenCVConfig.cmake" file.

Before:

SET(OpenCV_INCLUDE_DIRS "${OpenCV_INSTALL_PATH}/include/opencv;${OpenCV_INSTALL_PATH}/include")

SET(OpenCV_LIB_DIR "${OpenCV_INSTALL_PATH}/lib64")

After:

SET(OpenCV_INCLUDE_DIRS "/usr/include/opencv;/usr/include/opencv2")

SET(OpenCV_LIB_DIR "/usr/lib64")

Hugh Pearse
  • 699
  • 1
  • 7
  • 18
1

I am using Windows and get the same error message. I find another problem which is relevant. I defined OpenCV_DIR in my path at the end of the line. However when I typed "path" in the command line, my OpenCV_DIR was not shown. I found because Windows probably has a limit on how long the path can be, it cut my OpenCV_DIR to be only part of what I defined. So I removed some other part of the path, now it works.

Roy
  • 117
  • 1
  • 3
  • 10
1

I had the same error, I use windows. I add "C:\opencv\build" (opencv folder) to path at the control pannel. So, That's Ok!!

Albert Yun
  • 11
  • 1
-1

Followed @hugh-pearse 's and @leszek-hanusz 's answers, with a little tweak. I had installed opencv from ubuntu 12.10 repository (libopencv-)* and had the same problem. Couldn't solve it with export OpenCV_DIR=/usr/share/OpenCV/ (since my OpenCVConfig.cmake whas there). It was solved when I also changed some lines on the OpenCVConfig.cmake file:

# ======================================================
# Include directories to add to the user project:
# ======================================================

# Provide the include directories to the caller

#SET(OpenCV_INCLUDE_DIRS "${OpenCV_INSTALL_PATH}/include/opencv;${OpenCV_INSTALL_PATH}/include")

SET(OpenCV_INCLUDE_DIRS "/usr/include/opencv;/usr/include/opencv2")
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})

# ======================================================
# Link directories to add to the user project:
# ======================================================

# Provide the libs directory anyway, it may be needed in some cases.

#SET(OpenCV_LIB_DIR "${OpenCV_INSTALL_PATH}/lib")

SET(OpenCV_LIB_DIR "/usr/lib")

LINK_DIRECTORIES(${OpenCV_LIB_DIR})

And that worked on my Ubuntu 12.10. Remember to add the target_link_libraries(yourprojectname ${OpenCV_LIBS}) in your CMakeLists.txt.

aguadopd
  • 553
  • 8
  • 17
-2

When you install the libraries in the c drive (windows). the CMakeLists.txt shoud be looking like below:

cmake_minimum_required(VERSION 3.0.0)
project(test_opencv VERSION 0.1.0)

include(CTest)
enable_testing()

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(test_opencv main.cpp)

target_link_libraries(test_opencv ${OPENCV_LIBS})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

finding the package and include directories

when building the project in VS code. Run the visual studio code with admin rights as the OpenCV is installed inside C drive.

Prudhviraj
  • 441
  • 3
  • 12