QT5在windows下调用OpenCV库出现: undefined reference to `xxxxx' 错误解决办法(适用MinGW编译器)。

2022-01-12 11:08:29 浏览数 (1)

一、环境介绍

window系统:win10 X64

QT版本: 5.12

QT5.12自带的MinGW编译器版本:mingw730_32 与mingw730_64

在QT的安装目录下,可以查看MinGW编译器的版本:

二、使用OpenCV出现的问题

在QT框架代码里使用老版本的分类器(cvLoad、cvHaarDetectObjects)处理图像时,正常编译没有问题,当使用新版本级联分类器(CascadeClassifier)时,程序编译就会出现一堆没有定义的错误:

代码语言:javascript复制
release/widget.o:widget.cpp:(.text 0x2a1): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text 0x465): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text 0x631): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text 0x63b): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
release/widget.o:widget.cpp:(.text 0x64b): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
release/widget.o:widget.cpp:(.text 0x66a): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
release/widget.o:widget.cpp:(.text 0x6c9): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
release/widget.o:widget.cpp:(.text 0x6de): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
release/widget.o:widget.cpp:(.text 0x6f9): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
release/widget.o:widget.cpp:(.text 0x786): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x796): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x7a4): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x7b3): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x7c6): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x7d4): more undefined references to `cv::Mat::deallocate()' follow
release/widget.o:widget.cpp:(.text 0x150b): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
release/widget.o:widget.cpp:(.text 0x151e): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
release/widget.o:widget.cpp:(.text 0x1540): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
release/widget.o:widget.cpp:(.text 0x15e1): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text 0x15f2): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
release/widget.o:widget.cpp:(.text 0x1605): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
release/widget.o:widget.cpp:(.text 0x1627): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
release/widget.o:widget.cpp:(.text 0x1634): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x23a9): undefined reference to `cv::CascadeClassifier::CascadeClassifier()'
release/widget.o:widget.cpp:(.text 0x23b4): undefined reference to `cv::CascadeClassifier::CascadeClassifier()'
release/widget.o:widget.cpp:(.text 0x23bf): undefined reference to `cv::CascadeClassifier::CascadeClassifier()'
release/widget.o:widget.cpp:(.text 0x2458): undefined reference to `cv::CascadeClassifier::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
release/widget.o:widget.cpp:(.text 0x24f5): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
release/widget.o:widget.cpp:(.text 0x2500): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
release/widget.o:widget.cpp:(.text 0x250b): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
release/widget.o:widget.cpp:(.text 0x25af): undefined reference to `cv::CascadeClassifier::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
release/widget.o:widget.cpp:(.text 0x268f): undefined reference to `cv::CascadeClassifier::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
release/widget.o:widget.cpp:(.text 0x280a): undefined reference to `cv::Mat::create(int, int const*, int)'
release/widget.o:widget.cpp:(.text 0x28b1): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
release/widget.o:widget.cpp:(.text 0x28c8): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
release/widget.o:widget.cpp:(.text 0x28f3): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
release/widget.o:widget.cpp:(.text 0x2907): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
release/widget.o:widget.cpp:(.text 0x291e): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
release/widget.o:widget.cpp:(.text 0x2939): undefined reference to `cv::equalizeHist(cv::_InputArray const&, cv::_OutputArray const&)'
release/widget.o:widget.cpp:(.text 0x29cb): undefined reference to `cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)'
release/widget.o:widget.cpp:(.text 0x2aac): undefined reference to `cv::ellipse(cv::Mat&, cv::Point_<int>, cv::Size_<int>, double, double, double, cv::Scalar_<double> const&, int, int, int)'
release/widget.o:widget.cpp:(.text 0x2acc): undefined reference to `cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)'
release/widget.o:widget.cpp:(.text 0x2b61): undefined reference to `cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)'
release/widget.o:widget.cpp:(.text 0x2bf6): undefined reference to `cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)'
release/widget.o:widget.cpp:(.text 0x2cce): undefined reference to `cv::circle(cv::Mat&, cv::Point_<int>, int, cv::Scalar_<double> const&, int, int, int)'
release/widget.o:widget.cpp:(.text 0x2d90): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text 0x2ec0): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text 0x2f42): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text 0x2fc8): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text 0x3047): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x3057): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x3067): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x3077): undefined reference to `cv::Mat::deallocate()'
release/widget.o:widget.cpp:(.text 0x3094): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
release/widget.o:widget.cpp:(.text 0x309f): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
release/widget.o:widget.cpp:(.text 0x30aa): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
release/widget.o:widget.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev] 0x64): undefined reference to `cv::fastFree(void*)'
release/widget.o:widget.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev] 0x71): undefined reference to `cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Release:71: release/qt_opencv.exe] Error 1
mingw32-make: *** [Makefile:38: release] Error 2
mingw32-make[1]: Leaving directory 'D:/linux-share-dir/QT/build-qt_opencv-Desktop_Qt_5_12_6_MinGW_32_bit-Release'
21:11:46: 进程"C:QtQt5.12.6Toolsmingw730_32binmingw32-make.exe"退出,退出代码 2 。
Error while building/deploying project qt_opencv (kit: Desktop Qt 5.12.6 MinGW 32-bit)
When executing step "Make"

三、解决办法

方法1: 使用当前QT使用的MinGW编译器,重新编译一份OpenCV的源码再使用。

方法2: 使用已经编译好的MinGW版本OpenCV库,直接调用。

既然已经有编译好的MinGW库,就不用麻烦自己再去编译了,各种OpenCV对应的MinGW编译器编译的库下载地址: https://github.com/huihut/OpenCV-MinGW-Build 。

由于我的源代码里使用了OpenCV3.x和2.X版本的很多函数,在OpenCV4.X里已经不存在,为了兼容代码,我这里下载OpenCV3.X版本的库。

下载之后,将bin目录加到系统的环境变量PATH里,方便程序在运行时能够找到对应的动态库。

在QT的工程文件里加入OpenCV头文件的路径和库路径即可:

代码语言:javascript复制
QT        = core gui
QT        = multimediawidgets
QT        = xml
QT        = multimedia
QT        = network
QT        = widgets
QT        = serialport
greaterThan(QT_MAJOR_VERSION, 4): QT  = widgets

CONFIG  = c  11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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 
    widget.cpp

HEADERS  = 
    widget.h

FORMS  = 
    widget.ui

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

#linu平台的路径设置
linux {
message('运行linu版本')
#添加opencv头文件的路径,需要根据自己的头文件路径进行修改
INCLUDEPATH =/home/wbyq/work_pc/opencv-3.4.9/_install/install/include
             /home/wbyq/work_pc/opencv-3.4.9/_install/install/include/opencv
             /home/wbyq/work_pc/opencv-3.4.9/_install/install/include/opencv2

LIBS =/home/wbyq/work_pc/opencv-3.4.9/_install/install/lib/libopencv_*
}

win32
{
    message('运行win32版本')
    #添加opencv头文件的路径,需要根据自己的头文件路径进行修改
    INCLUDEPATH =C:/OpenCV_3.4.7/OpenCV-MinGW-Build-OpenCV-3.4.7/include 
                 C:/OpenCV_3.4.7/OpenCV-MinGW-Build-OpenCV-3.4.7/include/opencv 
                 C:/OpenCV_3.4.7/OpenCV-MinGW-Build-OpenCV-3.4.7/include/opencv2
    LIBS =C:/OpenCV_3.4.7/OpenCV-MinGW-Build-OpenCV-3.4.7/x86/mingw/bin/libopencv_*.dll
}

RESOURCES  = 
    image.qrc

清除之前编译报错的文件,再次编译即可。

0 人点赞