Issue
I'm trying to use OpenCV in Clion and I followed this tutorial. I installed opencv with choco install opencv
But after running the program I get this error:
D:\Apps\CLion 2022.3.1\bin\mingw\bin/ld.exe: cannot find -lopencv_core
D:\Apps\CLion 2022.3.1\bin\mingw\bin/ld.exe: cannot find -lopencv_imgproc
D:\Apps\CLion 2022.3.1\bin\mingw\bin/ld.exe: cannot find -lopencv_highgui
D:\Apps\CLion 2022.3.1\bin\mingw\bin/ld.exe: cannot find -lopencv_imgcodecs
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.24)
project(GTSpeed)
set(CMAKE_CXX_STANDARD 17)
set(ENV{OPENCV_DIR} "C:\\tools\\opencv\\build")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(GTSpeed main.cpp gtspeed/Keyboard.cpp gtspeed/Keyboard.h gtspeed/Logger.cpp gtspeed/Logger.h gtspeed/Config.cpp gtspeed/Config.h)
#add_executable(GTSpeed main.cpp)
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
target_link_libraries(GTSpeed ${OpenCV_LIBS})
My system environment variables:
And this is my user variable for path:
C:\tools\opencv\build\x64\vc15\bin
I searched this online but there weren't answers for Clion and opencv in c++.
Also before I got this error it had another issue but I solved it.
CMake Error at CMakeLists.txt:9 (find_package):
Found package configuration file:
C:/tools/opencv/build/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
NOT FOUND.
So I went in the file and changed set(OpenCV_FOUND FALSE) to TRUE. I don't know if this affected anything.
Thanks.
Solution
It appears to me that you are building using mingw
however your
C:\tools\opencv\build\x64\vc15\bin
seems to be pointing to libraries built using MSVC. As is seen from the vc15
.
You either need to build OpenCV libraries using mingw
or you need to use MSVC
for your project.
Hope it helps!
EDIT: I did not notice your EDIT... that is definitely a bad idea. OpenCV_FOUND is set by CMake (specifically the FindCMake.cmake or similar config files). DO NOT set this manually yourself.
EDIT2: Here is a link on how to configure CLion so that it uses MSVC
Answered By - Milan Š. Answer Checked By - Katrina (WPSolving Volunteer)