Using OpenCV with QT 6 and CMake
-
Hello dear Members,
I have installed opencv to my local windows computer and created a basic QT core application.
I use QT Creator 6.0.2 and OpenCV 4.5.5
I added find_package(OpenCV REQUIRED) to cmakelists.txt and some messages to trace value of opencv variables such as below.
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
message(STATUS " dir path: ${OpenCV_DIR}")
and these are the values-- version: 4.5.5
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world
-- include path: C:/opencv/build/include
-- dir path: C:/OpenCV/build/x64/vc15/libAs you can see the directory structure of opencv.
content of main.cpp is also below#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);cv::Mat mat; return a.exec();
}
When I build the app, I got the error below
:-1: error: CMakeFiles/QT2.dir/main.cpp.obj:C:/Users/ozgurA/Documents/QT/QT2/main.cpp:10: undefined reference to `cv::Mat::Mat()'
I hope anyone can help me.
Best regards.
-
Your application should link against the OpenCV libs -> target_link_libraries()
-
First of all thank you for your quick reply. As you can see below I have already linked libraries.
target_link_libraries(QT2 ${OpenCV_LIBS} Qt${QT_VERSION_MAJOR}::Core)
OpenCV_LIBS variable contains libraries below;
-- version: 4.5.5
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world -
Hi and welcome to devnet,
Did you try the example shown here ?
It's for Linux however the code and cmake file should work all the same on Windows.
-
Hi, my cmake configuration is almost same. You can check it below.
cmake_minimum_required(VERSION 3.14)
project(QT2 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(QT2
main.cpp
)target_link_libraries(QT2 ${OpenCV_LIBS} Qt${QT_VERSION_MAJOR}::Core)
I still get same error.
:-1: error: CMakeFiles/QT2.dir/main.cpp.obj:C:/Users/ozgurA/Documents/QT/QT2/main.cpp:11: undefined reference to `cv::Mat::Mat()'
-
How did you install OpenCV ?
-
I have downloaded OpenCV 4.5.5 from the link https://opencv.org/releases/
then extracted into c:\opencv folder. After then set C:\OpenCV\build\x64\vc15\bin and C:\OpenCV\build\x64\vc15\lib folders into Path variable in Environment variables. -
Can you check with the previous version to see if the same is happening ?
-
As suggested before, try building your application without Qt in it to ensure that your OpenCV installation is working.
-
This worked for me for the Qt+OpenCV+Windows combo. Extracted the OpenCV windows binary release into C:\Qt\opencv
CMake for opencv includes and libraries
set(OpenCV_DIR "C:/Qt/opencv/build") find_package(OpenCV REQUIRED) include_directories( ${OpenCV_INCLUDE_DIRS} ) add_executable(qtopencv main.cpp ) target_link_libraries(qtopencv Qt${QT_VERSION_MAJOR}::Core ${OpenCV_LIBS})