Home > database >  Undefined reference errors in Qt Creator | OpenCV
Undefined reference errors in Qt Creator | OpenCV

Time:04-22

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.

CodePudding user response:

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

  • Related