7

I'm trying to build project that has dependency to OpenCV. I installed Opencv using macports and when I try to build project, cmake gives the following output:

CMake Error at CMakeLists.txt:47 (FIND_PACKAGE):
  By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "OpenCV", but
  CMake did not find one.

  Could not find a package configuration file provided by "OpenCV" with any
  of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

  Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
  "OpenCV_DIR" to a directory containing one of the above files.  If "OpenCV"
  provides a separate development package or SDK, be sure it has been
  installed.

I searched a little bit about this problem and added the following env. variables to my $HOME/.profile file

export DYLD_LIBRARY_PATH=/opt/local/lib:$DYLD_LIBRARY_PATH
export CMAKE_PREFIX_PATH=/opt/local

without success. I checked and I have all opencv files istalled in /opt/local/lib and /opt/local/include/opencv directories. There is also OpenCVConfig.cmake in the following path:

/opt/local/lib/cmake/OpenCVConfig.cmake

How to make cmake know the path where opencv is installed? Previously I've build OpenCV on my own using cmake and installed into /usr/local and then it worked fine without any other fix. However I had some problems with ffmpeg and right now I switched to using macports.

pzo
  • 2,087
  • 3
  • 24
  • 42

7 Answers7

9

Another option that works for me was set the env value OpenCV_DIR at the cmake opencv dir:

export OpenCV_DIR=/opt/local/lib/cmake/
alhuelamo
  • 135
  • 1
  • 3
  • 10
1

macports traditionally installs OpenCV to /opt/local/ instead of the standard /usr/local/.

The solution to your problem is stated at:

Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to a directory containing one of the above files.

When building your project in the command-line, make sure you execute:

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/local/lib

And then set the flag CMAKE_PREFIX_PATH for cmake:

cmake -D CMAKE_PREFIX_PATH=/opt/local ../
karlphillip
  • 92,053
  • 36
  • 243
  • 426
1

Not a MACPORT problem, but someone may find this helpful. Followed @hugh-pearse 's and @leszek-hanusz 's answers in this question, 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 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.

Community
  • 1
  • 1
aguadopd
  • 553
  • 8
  • 17
1

I finally read the header of the OpenCVConfig.cmake file. It instructs to include these lines to use from an external project:

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(MY_TARGET_NAME ${OpenCV_LIBS})

(adding include_directories to CMakeLists.txt fixed it for me)

kengregson
  • 71
  • 1
1

Make sure you have compiled OpenCV, once you compiled, OpenCVConfig.cmake will be generated in build directory.

follow these steps to compile

then , export OpenCV_DIR=<path to build directory with OpenCVConfig.cmake>

It should work now !

Manish S
  • 801
  • 11
  • 12
0

I tried all the above ideas in vain. I eventually found a way to have the compilation work: Besides using @kengregson steps, I simply renamed the folder /usr/local/include/opencv2 so that it is not chosen when compiling my cpp file.

JeromeK
  • 1
  • 1
0

Since I compile my own OpenCV in a multiple-users server, so I can't install the OpenCV libs in /usr/local directly, but in my home folder instead, then exactly the same issue occurs to me.

Below is how I fix it:

  1. Flowing the steps in OpenCV official doc: Using OpenCV with gcc and CMake

  2. Add displayImage test program, and use this CMake file

cmake_minimum_required(VERSION 2.8) project( DisplayImage ) find_package( OpenCV) add_executable( DisplayImage DisplayImage.cpp ) target_link_libraries( DisplayImage ${OpenCV_LIBS} )

  1. The same error happened, so add one environment variable in my .tcshrc

setenv OpenCV_DIR "${folder where contains OpenCVConfig.cmake }" \# mine is: setenv OpenCV_DIR "~/local/OpenCV2.4.13/share/OpenCV"

ouxiaogu
  • 151
  • 1
  • 11