Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved symbols: Qt and OpenCV

I have been trying to use OpenCV in my Qt-based application. However, I am running into linking errors for some reason.

I am using Qt 5.5.0, MSVC 12.0, OpenCV 3.0, Windows 7. My OpenCV is unpacked in C:\opencv folder. I am not trying to compile the library myself, just using what came in the package. As shown in the code below, I am linking to libraries located in vc12 folder.

I have removed all my code and made it really basic for troubleshooting purposes, so I have a default starter QtWidget project (with an empty screen), to which I added a single line:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core.hpp>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
   ui->setupUi(this);
   cv::Mat M;
}

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

If I comment out the only openCV command (cv::Mat M;) the code compiles and runs. With this line present I am getting two unresolved symbol errors:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPEAX@Z) referenced in function "public: __cdecl cv::Mat::~Mat(void)" (??1Mat@cv@@QEAA@XZ)
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl cv::Mat::deallocate(void)" (?deallocate@Mat@cv@@QEAAXXZ) referenced in function "public: void __cdecl cv::Mat::release(void)" (?release@Mat@cv@@QEAAXXZ)

I have tried various ways to statically link openCV libraries, but I keep getting the same errors (I always clean, re-run QMake, and then re-build the project for every attempt). Here is what my .pro file looks like right now (I ended up adding all static libs hoping that would help - it did not):

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Test
TEMPLATE = app


SOURCES += main.cpp\
    mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

win32:CONFIG(release, debug|release): LIBS += -LC:/opencv/build/x86/vc12/staticlib \
-lopencv_core300 \
-lopencv_highgui300 \
-lopencv_imgproc300 \
-lIlmImf \
-lippicvmt \
-llibjasper \
-llibjpeg \
-llibpng \
-llibtiff \
-llibwebp \
-lopencv_calib3d300 \
-lopencv_features2d300 \
-lopencv_flann300 \
-lopencv_hal300 \
-lopencv_imgcodecs300 \
-lopencv_ml300 \
-lopencv_objdetect300 \
-lopencv_photo300 \
-lopencv_shape300 \
-lopencv_stitching300 \
-lopencv_superres300 \
-lopencv_ts300 \
-lopencv_video300 \
-lopencv_videoio300 \
-lopencv_videostab300 \
-lzlib

else:win32:CONFIG(debug, debug|release): LIBS += -LC:/opencv/build/x86/vc12/staticlib \
-lopencv_core300d \
-lopencv_highgui300d \
-lopencv_imgproc300d \
-lIlmImfd \
-lippicvmt \
-llibjasperd \
-llibjpegd \
-llibpngd \
-llibtiffd \
-llibwebpd \
-lopencv_calib3d300d \
-lopencv_features2d300d \
-lopencv_flann300d \
-lopencv_hal300d \
-lopencv_imgcodecs300d \
-lopencv_ml300d \
-lopencv_objdetect300d \
-lopencv_photo300d \
-lopencv_shape300d \
-lopencv_stitching300d \
-lopencv_superres300d \
-lopencv_ts300d \
-lopencv_video300d \
-lopencv_videoio300d \
-lopencv_videostab300d \
-lzlibd

INCLUDEPATH += C:/opencv/build/include/

DEPENDPATH += C:/opencv/build/include/

Is there something in particular I am missing about the configuration? I am pretty sure the basic syntax (like slashes, etc) is ok since I was able to link another library in Qt Creator in another project, but I will be more than happy to try any suggestions at this point.

like image 321
Ilya Avatar asked Dec 10 '25 20:12

Ilya


1 Answers

So after searching around and multiple attempts I found out what was wrong. I was linking to x86 library instead of x64. If I changed to x64 libs, I was getting mismatch between Static and Dynamic linker directives. Linking OpenCV statically would not work since OpenCV libraries themselves link to libcmt.lib, whereas Qt (pre-built) links dynamically to the counter-part of libcmt.lib, i.e. to msvcrt.lib. And since these two MS libraries are one and the same (just one is for static linking, the other for dynamic), I had two options: either rebuild Qt or OpenCV (which I don't want to do - I am very new to this kind of issues), or link OpenCV dynamically. I opted for the second option. All you have to do is to remove all references to /staticlib folder and libs that it contains, and replace it with the following:

win32:CONFIG(release, debug|release): LIBS += -LC:/opencv/build/x64/vc12/lib -lopencv_world300
else:win32:CONFIG(debug, debug|release): LIBS += -LC:/opencv/build/x64/vc12/lib -lopencv_world300d

Note that one has to distribute corresponding DLLs with the application (located in build\x64\vc12\bin\ folder).

like image 197
Ilya Avatar answered Dec 13 '25 10:12

Ilya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!