1

I've been trying for a few days to correctly setup OpenCV with CLion with little success, so asking on SO.

Here's what my CMakeLists looks like:

cmake_minimum_required(VERSION 3.12)
project(ocv_test)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

find_package(OpenCV REQUIRED)

set(SOURCE_FILES main.cpp)
add_executable(ocv_test ${SOURCE_FILES})

include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(ocv_test ${OpenCV_LIBS})

Here's the error I get:

"C:\Program Files\JetBrains\CLion 2018.2.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Owner\CLionProjects\ocv-test
Could not find OpenCV_CORE_INCLUDE_DIR
Could not find OpenCV_HIGHGUI_INCLUDE_DIR
Include dir: OFF
CMake Error at C:/Program Files/JetBrains/CLion 2018.2.2/bin/cmake/win/share/cmake-3.12/Modules/FindOpenCV.cmake:220 (MESSAGE):
  OpenCV required but some headers or libs not found.  Please specify it's
  location with OpenCV_ROOT_DIR env.  variable.
Call Stack (most recent call first):
  CMakeLists.txt:6 (find_package)


-- Configuring incomplete, errors occurred!
See also "C:/Users/Owner/CLionProjects/ocv-test/cmake-build-debug/CMakeFiles/CMakeOutput.log".

I primarily followed these steps from another SO answer, but here are the steps:

  1. Installed MinGW-64
    • Architecture: x86_64, Threads: posix, Exception: sjlj
  2. Installed CMake 3.12.2 x64 msi
  3. In System variables, set/create the following:
    • _CMAKE_HOME (C:\Program Files (x86)\CMake)
    • _MINGW_HOME (C:\mingw\mingw64)
  4. Then, add the following to Path variable:
    • %_CMAKE_HOME%\bin
    • %_MINGW_HOME%\bin
  5. Download OpenCV 3.4.3, and Extract to:
    • C:\opencv\opencv-3.4.3
  6. Using CMake, Configure w/ MinGW Makefiles and specifying Native compilers:
    • C: C:/mingw/mingw64/bin/x86_64-w64-mingw32-gcc.exe
    • C++: C:/mingw/mingw64/bin/x86_64-w64-mingw32-g++.exe
  7. Then, Generate (without Tests, Docs, Python, WITH_IPP, WITH_MSMF) to:
    • C:_dev_sw\opencv\opencv-3.4.3\build_mingw
  8. Run mingw32-make, then mingw32-make install in C:_dev_sw\opencv\opencv-3.4.3\build_mingw
  9. In System variables, set/create the following:
    • _OPENCV_HOME (C:\opencv\opencv-3.4.3\build_mingw\install\x64\mingw)
  10. Then, add the following to Path variable:
    • %_OPENCV_HOME%\bin
  11. Add FindOpenCV.cmake to:
    • C:\Program Files\JetBrains\CLion 2018.2.2\bin\cmake\win\share\cmake-3.12\Modules
  12. Create new C++ executable project in CLion (ocv-test)
  13. Update MakeLists.txt file (see above)
  14. Reload MakeLists.txt and get errors shown above

I tried to update the CMakeLists as below, but still same errors:

cmake_minimum_required(VERSION 3.12)
project(ocv_test)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Where to find CMake modules and OpenCV
set(OpenCV_DIR "C:\\opencv\\opencv-3.4.3\\build_mingw\\install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

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

add_executable(ocv_test main.cpp)

# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)

# linking
target_link_libraries(ocv_test ${OpenCV_LIBS})

Unlike this SO answer, I do not see OpenCV_DIR name in my CMake build. Also, I tried updating _OPENCV_HOME to OpenCV_ROOT_DIR (as error says), but that didn't work either.

Does anything seem off?

===

Edit 1:

FindOpenCV was the issue (so skip step 11). Setting the OPENCV_DIR var in CMakeLists fixed the errors, and built successfully (Thanks Tsyvarev!).

I'm not sure if setting OPENCV_DIR in CMakeLists will be an issue if the project is ran on another PC and/or OS, so I added OPENCV_DIR entry (pointing to /install directory) into CMake GUI, Repeated steps 6-8, created new but similar CLion project, and got the following error:

CMake Error at CMakeLists.txt:10 (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.

Again, this is fixed if I set the OPENCV_DIR variable. But how can it be avoided since it's already configured in GUI?

  • Setting `OpenCV_ROOT_DIR` variable (either as environment one or as CMake one) to the OpenCV installation directory should work. Have you checked that you set this variable correctly? (under given directory should be `lib/` and `include/` subdirectories). "Unlike this SO answer, I do not see OpenCV_DIR name in my CMake build." - That is OK. You ship your project with `FindOpenCV.cmake`, and this script doesn't use the variable. BTW, nice step by step description of your actions - it actually helps others to understand your problem. – Tsyvarev Sep 08 '18 at 23:29
  • @Tsyvarev Thanks for the reply. I set system variable OpenCV_ROOT_DIR to C:\opencv\opencv-3.4.3\build_mingw\install which has include, however bin is under install\x64\mingw (and error still persisted). Then I set the variable to C:\opencv\opencv-3.4.3\build_mingw which contains lib, include, install (from mingw32-make install), etc. and the error still persisted. – Silent Knight Sep 09 '18 at 01:09
  • Script `FindOpenCV.cmake` which you use looks not accurate. Message `Could not find OpenCV_HIGHGUI_INCLUDE_DIR` doesn't mean absence of that the include file. You install OpenCV via CMake, so it should have `OpenCVConfig.cmake` file. Find this file under **install directory** of OpenCV. If you have this file, then: 1. Remove `FindOpenCV.cmake` script which you have. 2. Clear build directory of your project, and re-run configuration for it. 3. You will got error message about `OpenCV_DIR` variable. Set this variable in GUI to the **directory** with `OpenCVConfig.cmake` in it. 4. Configure. – Tsyvarev Sep 09 '18 at 07:59
  • @Tsyvarev I found OpenCVConfig under /install, so removed FindOpenCV. That, along with using the CMakeLists at the bottom fixed the errors, so FindOpenCV was the culprit. Thank you!! However, I tried setting OpenCV_DIR in GUI, Configuring, and using CMakeLists at the top, but got an error (see edit 1). Then I set the OpenCV_DIR var again and repeated steps 6-8 again, create a new CLion project using the top CMakeLists, but the errors persisted. So I can build if I set OpenCV_DIR var, but will that be an issue if someone runs the project on another PC and/or OS? – Silent Knight Sep 09 '18 at 14:58
  • If you want to set `OpenCV_DIR` in the `CMakeLists.txt`, set it as a CACHE variable: `set(OpenCV_DIR <..> CACHE PATH "")`. Otherwise it doesn't work. – Tsyvarev Sep 09 '18 at 16:25
  • @Tsyvarev Ideally, I would want OpenCV_DIR from CMake GUI to work, but it isn't for some reason. Any idea why it is not working? **Edit:** If I add CACHE PATH "", It fails and gives the same error as the one under Edit – Silent Knight Sep 09 '18 at 21:37

0 Answers0