簡體   English   中英

qt 和 Opencv 鏈接錯誤“未定義的引用”

[英]qt and Opencv linking error "undefined reference"

我嘗試在 qt 中設置 opencv 並完全按照此處https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows的步驟操作,但出現鏈接錯誤,例如

“未定義對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: 未定義引用 `cv::imread(cv::String const&, >int)'"

這是我的opencv的路徑: C:\opencv-build\install\x86\mingw\lib

我嘗試過的事情:在 Qt 上使用“添加庫”。 但是,qt 無法找到我指定的文件。

我的代碼:.pro 文件

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

主要的

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

我感謝任何形式的幫助。 謝謝!

嘗試將 LIBS 指定為LIBS += -Lpath/to/lib -llibname qmake 參考

LIBS += -LC:\opencv-build\bin -lopencv_core343 -lopencv_highgui343 -lopencv_imgcodecs343 -lopencv_imgproc343 -lopencv_features2d343 -lopencv_calib3d343

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM