How can I link the opencv libraries correctly?
I would like to work with the pre-built version of the opencv libraries in qt creator with either cmake or qmake in windows.
I have already tried various tutorials and asked colleagues at university without much success. However maybe someone sees, what could be wrong and how to fix it. I would be extremely happy, if someone could help me: I updated and use the latest versions qt creator, mingw, cmake and opencv. In "C:/myPath/C++/opencv/opencv/build/x64/vc15/lib" lie the files: "opencv_world341.lib" and "opencv_world341d.lib", they are each about the size: 2.2MB.
I have the CMake File:
cmake_minimum_required(VERSION 2.8)
project(OpenCV_CMake)
set(OpenCV_DIR "C:/myPath/C++/opencv/opencv/build/x64/vc15/lib")
message("OpenCV_FOUND='${OpenCV_FOUND}'")
find_package( OpenCV REQUIRED )
message("OpenCV_FOUND='${OpenCV_FOUND}'")
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(${PROJECT_NAME} "main.cpp")
message("These are my opencv libs....: '${OpenCV_LIBS}'" )
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
And .cpp file:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char **argv) {
Mat image;
image = imread(argv[1], 1);
if (argc != 2 || !image.data) {
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
cmake compiles properly for me and gives me the output:
Running Windows Runtime device detection.
No winrtrunner.exe found.
Running "C:\cmake\bin\cmake.exe -E server "--pipe=\\.\pipe\{f67b9536-cd8a-43be-8b06-aa9c90087f1d}" --experimental" in C:\myPath\C++\Workingbay\build-CMAKE_TEST-cmake_test-Default.
Running "C:\cmake\bin\cmake.exe -E server "--pipe=\\.\pipe\{250b89c5-6f34-4d67-bfc6-9b0df039ab3b}" --experimental" in C:\myPath\C++\Workingbay\build-OpenCV_CMake-cmake_test-Debug.
Starting to parse CMake project.
Starting to parse CMake project.
OpenCV_FOUND=''
Configuring done
OpenCV_FOUND='1'
These are my opencv libs....: 'opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_world'
Configuring done
Generating done
Generating done
CMake Project was parsed successfully.
CMake Project was parsed successfully.
Starting to parse CMake project.
OpenCV_FOUND=''
OpenCV_FOUND='1'
These are my opencv libs....: 'opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_world'
Configuring done
Generating done
CMake Project was parsed successfully.
However building my project and trying to run the code results in:
18:56:36: Running steps for project OpenCV_CMake...
18:56:36: Starting: "C:\cmake\bin\cmake.exe" --build . --target all
C:\cmake\bin\cmake.exe -HC:\myPath\C++\Workingbay\OpenCV_CMake -BC:\myPath\C++\Workingbay\build-OpenCV_CMake-cmake_test-Debug --check-build-system CMakeFiles\Makefile.cmake 0
C:\cmake\bin\cmake.exe -E cmake_progress_start C:\myPath\C++\Workingbay\build-OpenCV_CMake-cmake_test-Debug\CMakeFiles C:\myPath\C++\Workingbay\build-OpenCV_CMake-cmake_test-Debug\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'C:/myPath/C++/Workingbay/build-OpenCV_CMake-cmake_test-Debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\OpenCV_CMake.dir\build.make CMakeFiles/OpenCV_CMake.dir/depend
mingw32-make.exe[2]: Entering directory 'C:/myPath/C++/Workingbay/build-OpenCV_CMake-cmake_test-Debug'
C:\cmake\bin\cmake.exe -E cmake_depends "MinGW Makefiles" C:\myPath\C++\Workingbay\OpenCV_CMake C:\myPath\C++\Workingbay\OpenCV_CMake C:\myPath ...
It is not set(OpenCV_DIR "C:/myPath/C++/opencv/opencv/build/x64/vc15/lib") but set(OpenCV_DIR "C:/myPath/C++/opencv/opencv/build) and you cannot use vc15 lib with mingw
@LBerger, can you please explain what and why? Simply writing "set(OpenCV_DIR "C:/myPath/C++/opencv/opencv/build) " makes CMake fail parsing the project. I have not yet tried the other part of your comment, nonetheless, I will try it.
In path given to cmake you must have a file OpenCVConfig.cmake. I think you can remove this line : a good example.
If cmake cannot find opencv in cmake gui you can give path where is file OpenCVConfig.cmake ou use -DOpenCV_DIR=path_to_OpenCVConfig.cmake