0

I tried to set up opencv in qt and followed the steps exactly from here https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows, but got linking error like

"undefined reference to cv::imread(cv::String const&, int)' debug/mainwindow.o: In function MainWindow::MainWindow(QWidget*)': C:\Users\Han\Desktop\QT_projects\build-TEST_OPENCV-Desktop_Qt_5_15_2_MinGW_64_bit->Debug/../TEST_OPENCV/mainwindow.cpp:17: undefined reference to `cv::imread(cv::String const&, >int)'"

Here's the path of my opencv: C:\opencv-build\install\x86\mingw\lib

Things I tried: Using the "add library" on Qt. However, qt coudlnt find the file that I specified.

My code: .pro file

#-------------------------------------------------
#
# Project created by QtCreator 2017-03-05T12:30:06
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = opencvtest
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#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 += C:\opencv\build\include

LIBS += C:\opencv-build\bin\libopencv_core343.dll
LIBS += C:\opencv-build\bin\libopencv_highgui343.dll
LIBS += C:\opencv-build\bin\libopencv_imgcodecs343.dll
LIBS += C:\opencv-build\bin\libopencv_imgproc343.dll
LIBS += C:\opencv-build\bin\libopencv_features2d343.dll
LIBS += C:\opencv-build\bin\libopencv_calib3d343.dll

# more correct variant, how set includepath and libs for mingw
# add system variable: OPENCV_SDK_DIR=D:/opencv/opencv-build/install
# read http://doc.qt.io/qt-5/qmake-variable-reference.html#libs

#INCLUDEPATH += $$(OPENCV_SDK_DIR)/include

#LIBS += -L$$(OPENCV_SDK_DIR)/x86/mingw/lib \
#        -lopencv_core320        \
#        -lopencv_highgui320     \
#        -lopencv_imgcodecs320   \
#        -lopencv_imgproc320     \
#        -lopencv_features2d320  \
#        -lopencv_calib3d320

main

#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);

    // read an image
    cv::Mat image = cv::imread("f://1.jpg", 1);
    // create image window named "My Image"
    cv::namedWindow("My Image");
    // show the image on window
    cv::imshow("My Image", image);
}

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

I appreciate any kind of help. Thanks!

3
  • -lopencv_core320 -- this should be the correct way for mingw, once you adjust the version numbers Commented Jul 11, 2022 at 16:49
  • hi there. Another error pop up as "cannot find -lopencv_core 343". I set my OPENCV_SDK_DIR = C:\opencv-build\install in system environment variables Commented Jul 11, 2022 at 16:59
  • look again for it. here it is: C:\p\opencv\build\install\x64\mingw\lib (and you probably need a -L path/to/libs directive there) Commented Jul 11, 2022 at 17:12

1 Answer 1

1

Try specifying LIBS as LIBS += -Lpath/to/lib -llibname qmake reference

LIBS += -LC:\opencv-build\bin -lopencv_core343 -lopencv_highgui343 -lopencv_imgcodecs343 -lopencv_imgproc343 -lopencv_features2d343 -lopencv_calib3d343
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answering! It didn't fix the error immediately. However, it showed another error msg like "skipping incompatible xxxxx", which led me to find out that the real reason for this error ---- I used a 64 bit MinGW compiler for a 32 bit binary library! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.