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:
- Installed MinGW-64
- Architecture: x86_64, Threads: posix, Exception: sjlj
- Installed CMake 3.12.2 x64 msi
- In System variables, set/create the following:
- _CMAKE_HOME (C:\Program Files (x86)\CMake)
- _MINGW_HOME (C:\mingw\mingw64)
- Then, add the following to Path variable:
- %_CMAKE_HOME%\bin
- %_MINGW_HOME%\bin
- Download OpenCV 3.4.3, and Extract to:
- C:\opencv\opencv-3.4.3
- 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
- Then, Generate (without Tests, Docs, Python, WITH_IPP, WITH_MSMF) to:
- C:_dev_sw\opencv\opencv-3.4.3\build_mingw
- Run mingw32-make, then mingw32-make install in C:_dev_sw\opencv\opencv-3.4.3\build_mingw
- In System variables, set/create the following:
- _OPENCV_HOME (C:\opencv\opencv-3.4.3\build_mingw\install\x64\mingw)
- Then, add the following to Path variable:
- %_OPENCV_HOME%\bin
- Add FindOpenCV.cmake to:
- C:\Program Files\JetBrains\CLion 2018.2.2\bin\cmake\win\share\cmake-3.12\Modules
- Create new C++ executable project in CLion (ocv-test)
- Update MakeLists.txt file (see above)
- 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?