1

I'm trying to set up a really basic project with Cmake and OpenCV 3.0.

My folder structure looks like this:

OpenCVTest
|
|--- build
|--- data
|--- include
|--- src
|--- CMakeLists.txt

The CMakeLists.txt file has the following content:

cmake_minimum_required(VERSION 3.4)
project(OpenCVTest)

find_package(OpenCV REQUIRED)

include_directories(
    ${CMAKE_SOURCE_DIR}/include
    ${OpenCV_INCLUDE_DIRS})

set(SOURCES
    src/main.cpp
    src/MyData.cpp

    include/MyData.h)

add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

I extracted OpenCV, set the environment variable OPENCV_DIR = <PATH_TO_OPEN_CV>\opencv\build\x86\vc12, and added %OPENCV_DIR%\bin to the PATH variable in Windows, as suggested by the OpenCV + CMake tutorial.

When running CMake, the following error message occurs:

CMake Error at CMakeLists.txt:4 (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 therefore changed the value of the environment variable OpenCV_DIR = <PATH_TO_OPEN_CV>\opencv\build, where the OpenCVConfig.cmake file is located.

After doing so (and restarting to Windows to update the environment variables!!!), CMake is able to configure and generate the project successfully.

However, CMake uses static library linking...

OpenCV STATIC: ON
Found OpenCV 3.0.0 in <PATH_TO_CMAKE>/opencv/build/x86/vc12/staticlib

... and you have to switch the Runtime Library in Visual Studio (C/C++ --> Code Generation --> Runtime Library) to /MTd.

Adding...

set(BUILD_SHARED_LIBS ON)

before

find_package(OpenCV REQUIRED)

will force CMake to use dynamic linking, however, then the PATH variable does not contain the correct path to the OpenCV DLL's, and <PATH_TO_OPEN_CV>\opencv\build\x86\vc12\bin needs to be added (for 32bit and VS2013).

All ob the above solutions seem quite inelegant and not really portable to me. I also tried several FindOpenCV.cmake files, however, none of them were able to correctly find the OpenCV directory.

My question is: Can anybody provide a solution for setting up a very basic CMake + OpenCV 3.0 example project, that does not have the above described shortcomings?

Saeid
  • 4,147
  • 7
  • 27
  • 43
Schnigges
  • 1,284
  • 2
  • 24
  • 48
  • Possible duplicate of [Compiling an OpenCV project using CMake on Cygwin, with OpenCV installed for Windows](http://stackoverflow.com/questions/33749216/compiling-an-opencv-project-using-cmake-on-cygwin-with-opencv-installed-for-win) – usr1234567 Nov 24 '15 at 15:40
  • 2
    Where is the source for using OPENCV_DIR? You are the second person asking this question within a week, probably we should ping the author of the text. – usr1234567 Nov 24 '15 at 15:41
  • http://docs.opencv.org/3.0-beta/doc/tutorials/introduction/windows_install/windows_install.html - assuming you mean the source for using that exact same capitalization – Schnigges Nov 24 '15 at 15:56
  • Your link is for beta 3.0, but with 3.0.0 release it did not improve: http://docs.opencv.org/3.0.0/d3/d52/tutorial_windows_install.html Did it work for you using the right capitalization? – usr1234567 Nov 24 '15 at 16:05
  • It did not solve my issue, no. The CMake error occurs both if the environment variable is called "OPENCV_DIR" or "OpenCV_DIR" – Schnigges Nov 25 '15 at 09:22
  • Ok, there seems to be two problems: One is that FindOpenCV.cmake is not in the right path. You could do that, by adding it directly to `cmake -DOpenCV=.. ` When you changed BUILD_SHARED_LIBS, you should clear your CMakeCache.txt file and re-run cmake. – usr1234567 Nov 25 '15 at 09:28
  • At this point, I am not using any `FindOpenCV.cmake` file, as none that I found seemed to be working with version 3.0.0. Furthermore, I always clear the cache and delete the previous `build` folder when I try something new. This has no effect on `BUILD_SHARED_LIBS` and I still have to manually change the Runtime Library. – Schnigges Nov 25 '15 at 09:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96126/discussion-between-schnigges-and-usr1234567). – Schnigges Nov 25 '15 at 09:46
  • Adding %OPENCV_DIR%\bin to the PATH is not a good Idea from my experience. Especially if you have a computer with different opencv versions. What is the version of your cmake? I had some problems with OpenCV3.0 because my cmake was to old – Tobias Senst Sep 27 '19 at 20:25

0 Answers0