3

I'm attempting to use OpenCV for Windows as supplied by opencv.org in a project I'm building with JetBrains' CLion IDE. I've installed the opencv library and configured CLion (set(OpenCV_DIR) to reference the build directory under it, but CMake issues the warning:

Found OpenCV Windows Pack but it has no binaries compatible with your configuration.    
You should manually point CMake variable OpenCV_DIR to your build of OpenCV library.

I've tried some of the older distributions from opencv.org with the same results. It appears CMake is locating the OpenCV libraries, but doesn't want to use them. Why, and how do I get the OpenCV libraries to work under CLion?

CODE-REaD
  • 2,819
  • 3
  • 33
  • 60
  • 2
    This has been downvoted with no explanation several times. Note that it's OK to [Answer My Own Question](https://stackoverflow.com/help/self-answer). – CODE-REaD Nov 11 '17 at 18:06

1 Answers1

9

The short answer is, you will probably need to build OpenCV from source in order to use it with CLion. But given the number and range of partially answered and unanswered questions here* and elsewhere on using JetBrains' CLion IDE with the OpenCV library, I think an overview is needed (my notes are from CLion 2016.3 and OpenCV 3.1, YMMV):

  • Though not produced by JetBrains, CMake is very central to CLion's operation. Understanding CMake therefore helps greatly in diagnosing CLion build problems. In particular CMake maintains a disk "cache" of settings which you may need to clear to incorporate changes to your environment (Tools->CMake->Reset Cache and Reload Project).
  • To make use of OpenCV in your build you must specify it in your project's CMakeLists.txt file. You request that CMake locate your OpenCV location and link it to your TARGET. An example of a sequence of commands from CMakeLists.txt for an executable named mushroom follows: add_executable(mushroom ${SOURCE_FILES}) FIND_PACKAGE(OpenCV REQUIRED) TARGET_LINK_LIBRARIES(mushroom ${OpenCV_LIBS})

    (For more on FIND_PACKAGE, see CMake:How To Find Libraries.)

  • FIND_PACKAGE for package XXX works either by way of FindXXX.cmake files located at CMake's Modules directory, or by consulting environment variable XXXX_DIR. On my system, no FindOpenCV.cmake file was present, so I relied on the OpenCV_DIR environment variable instead. This must be set, not to the root of your OpenCV installation, but to the build folder beneath it. I used an entry in CMakeLists.txt to set this variable, e.g.:

    set(OpenCV_DIR C:/Users/myacct/AppData/Local/opencv-3.0.0/build)
  • To link with OpenCV, CMake uses either FindOpenCV.cmake or OpenCV_DIR (see previous point above) to locate a file named OpenCVConfig.cmake. This file is generated by and ships with a particular build of OpenCV in order to document what components are present and where they are located.

  • Problems may occur when variable names used by OpenCVConfig.cmake conflict with those CLion has stored in its environment. In particular, if your OpenCV was built by Microsoft Visual C (MSVC), as is the Windows distribution from opencv.org, it won't work with CLion.

    Because CLion's build toolchain (ControlAltS-toolchain) uses either MinGW or Cygwin, OpenCVConfig.cmake will search for OpenCV binaries under a subdirectory named mingw or cygwin and will find none because the binaries were built with MSVC (it will look in a directory like vc11 or vc12 instead). This probably means you will need to build OpenCV from source in order to use it with CLion.

    Would reconfiguring OpenCVConfig.cmake to point to the MSVC binaries make this work? you may ask. Unfortunately the answer is still no, because libraries built with one compiler typically cannot be linked with another one.

  • OpenCVConfig.cmake or FindOpenCV.cmake likely contain diagnostic messages, but when CLion executes CMake for you, message(STATUS) calls are not displayed. To make them display, change them to message(WARNING) or message(FATAL_ERROR). But CLion 2016.3 EAP relieves this problem; see https://stackoverflow.com/a/39398373/5025060.

  • CLion does not indicate which .cmake script issued which diagnostics; don't assume they all come from the same script.

Hopefully this provides some general guidance on resolving CLion / CMake / OpenCV compatibility problems. Note that this does not cover compiler or linker issues; these will not appear until CMake completes its initial makefile build. Compiler or linker issues occur at a later stage and are controlled by include*(), link*() and other commands in CMakeLists.txt.

*Some related SO questions:
OpenCV Windows setup with CLion
OpenCV CLion (Cmake) linking issue - cmake reports a strange error
use OpenCV with Clion IDE on Windows
Compiling OpenCV on Windows with MinGW
Could not find module FindOpenCV.cmake ( Error in configuration process)
CMake: Of what use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?

CODE-REaD
  • 2,819
  • 3
  • 33
  • 60
  • 1
    Clion now support MSVC toolchain in that case will it work if I use clion with MSVC toolchain for opencv build following this guide https://jamesbowley.co.uk/build-compile-opencv-3-4-in-windows-with-cuda-9-0-and-intel-mkl-tbb/? – SaintTail Mar 21 '18 at 05:13
  • @SaintTail, sorry, I don't know the answer to your question, but **thank you for updating us on changes to CLion.** In particular, the information at [Ability to open a single file or folder in CLion, with other new CMake actions](https://blog.jetbrains.com/clion/2018/01/clion-starts-2018-1-eap-wsl-cpp17-cmake/#cmake) indicates that some of the issues I describe in this answer are getting addressed, e.g.: *The process of decoupling CMake from CLion has started....* If you do get CLion working as you describe, please let us know here! – CODE-REaD Mar 21 '18 at 13:19
  • It is working! I successfully make it work using msvc toolchain – SaintTail Apr 22 '18 at 09:10