我一直在尝试用CLion正确设置OpenCV的尝试,但收效甚微,因此请提出要求。
这是我的CMakeList的样子:
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})
这是我得到的错误:
"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".
我主要是从另一个SO答案关注these steps,但步骤如下:
我尝试如下更新CMakeList,但仍然存在相同的错误:
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})
与this答案不同,我在CMake版本中看不到OpenCV_DIR名称。另外,我尝试将_OPENCV_HOME更新为OpenCV_ROOT_DIR(如错误所述),但这也不起作用。
似乎有什么异常吗?
===
编辑1:
FindOpenCV是问题所在(因此跳过步骤11)。在CMakeLists中设置OPENCV_DIR var可以修复错误,并且可以成功构建(感谢Tsyvarev!)。
我不确定如果在其他PC和/或OS上运行项目,则在CMakeLists中设置OPENCV_DIR是否会成为问题,因此我在CMake GUI中添加了OPENCV_DIR条目(指向/ install目录),重复步骤6- 8,创建了新的但类似的CLion项目,并收到以下错误:
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.
同样,如果我设置OPENCV_DIR变量,此问题将得到解决。但是,由于已经在GUI中进行了配置,如何避免这种情况?