0

I linked OpenCV to Qt Creator with the help of this guide. However while compiling a simple code to read and display the image, I am getting undefined reference errors.

test.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

TARGET = opencvtest
TEMPLATE = app
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

INCLUDEPATH += D:\opencv\build\include

LIBS += D:\opencv-build\bin\libopencv_core320.dll
LIBS += D:\opencv-build\bin\libopencv_highgui320.dll
LIBS += D:\opencv-build\bin\libopencv_imgcodecs320.dll
LIBS += D:\opencv-build\bin\libopencv_imgproc320.dll
LIBS += D:\opencv-build\bin\libopencv_features2d320.dll
LIBS += D:\opencv-build\bin\libopencv_calib3d320.dll


# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    cv::Mat image = cv::imread("img101.jpg", 1);
       cv::namedWindow("My Image");
       cv::imshow("My Image", image);
}

MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

These are the errors I am getting while building the file. Using Windows 10, MinGW 7.3.0 32-bit C and C++ compiler.

Koushik
  • 65
  • 12
  • You can check this: https://stackoverflow.com/questions/15881913/how-to-link-opencv-in-qtcreator-and-use-qt-library – KimKulling Apr 19 '22 at 09:59
  • I think adding the *.dll files to LIBS is not sufficient here. Maybe this will help: https://stackoverflow.com/questions/718447/adding-external-library-into-qt-creator-project – CodingWithMagga Apr 19 '22 at 11:07
  • Thanks, I have tried the both the methods mentioned (although I have already tried the one @KimKulling linked). Unfortunately I am not able to use OpenCV functions in Qt. Both the methods give the exact same error as before. – Koushik Apr 19 '22 at 15:57

1 Answers1

0

Everything is working now. Apparently I had to link the libraries by right-clicking on the project folder on the left-sidebar, selecting Add Libraries and choosing the External Libraries option (added one by one).

win32: LIBS += -LD:/opencv-build/install/x86/mingw/lib/ -llibopencv_core320.dll
win32: LIBS += -LD:/opencv-build/install/x86/mingw/lib/ -llibopencv_highgui320.dll
win32: LIBS += -LD:/opencv-build/install/x86/mingw/lib/ -llibopencv_imgcodecs320.dll 
INCLUDEPATH += D:/opencv-build/install/include
DEPENDPATH += D:/opencv-build/install/include

////NOTE: imgcodecs library is required for imread to work//////

References:

imread OpenCV docs

imgcodecs and imread post

Koushik
  • 65
  • 12