Static OpenCV and undefined reference to symbol
Hello to everyone,
I know this problem is already discussed, but I can't find a way to solve it.
I built the OpenCV (3.4.15) libraries in dynamic and static versions in Linux Mint 21.1, without problems, but I have some difficulties using them in my QT project (using qtcreator).
The dynamic version work very well, but the static ones give me some errors I can't solve (I googled to find a solution but without result)
In my pro file I called (dynamic version):
Code:
LIBS +=-L/usr/local/lib/
LIBS += -lopencv_calib3d
LIBS += -lopencv_core
LIBS += -lopencv_features2d
LIBS += -lopencv_flann
LIBS += -lopencv_highgui
LIBS += -lopencv_imgcodecs
LIBS += -lopencv_imgproc
LIBS += -lopencv_ml
LIBS += -lopencv_objdetect
LIBS += -lopencv_photo
LIBS += -lopencv_shape
LIBS += -lopencv_stitching
LIBS += -lopencv_superres
LIBS += -lopencv_video
LIBS += -lopencv_videoio
LIBS += -lopencv_videost
and it's ok. but when I tried to use the static version replacing the previous lines with this code:
Code:
LIBS +=/usr/local/lib/libopencv_calib3d.a -lopencv_calib3d
LIBS +=/usr/local/lib/libopencv_core.a -lopencv_core
LIBS +=/usr/local/lib/libopencv_features2d.a -lopencv_features2d
LIBS +=/usr/local/lib/libopencv_flann.a -lopencv_flann
LIBS +=/usr/local/lib/libopencv_highgui.a -lopencv_highgui
LIBS +=/usr/local/lib/libopencv_imgcodecs.a -lopencv_imgcodecs
LIBS +=/usr/local/lib/libopencv_imgproc.a -lopencv_imgproc
LIBS +=/usr/local/lib/libopencv_ml.a -lopencv_ml
LIBS +=/usr/local/lib/libopencv_objdetect.a -lopencv_objdetect
LIBS +=/usr/local/lib/libopencv_photo.a -lopencv_photo
LIBS +=/usr/local/lib/libopencv_shape.a -lopencv_shape
LIBS +=/usr/local/lib/libopencv_stitching.a -lopencv_stitching
LIBS +=/usr/local/lib/libopencv_superres.a -lopencv_superres
LIBS +=/usr/local/lib/libopencv_video.a -lopencv_video
LIBS +=/usr/local/lib/libopencv_videoio.a -lopencv_videoio
LIBS +=/usr/local/lib/libopencv_videostab.a -lopencv_videostab
the result is:
:-1: error: /usr/local/lib/libopencv_objdetect.a(qrcode.cpp.o): undefined reference to symbol '_ZN2cv11contourAreaERKNS_11_InputArrayEb'
:-1: error: /usr/local/lib//libopencv_imgproc.so: error adding symbols: DSO missing from command line
:-1: error: collect2: error: ld returned 1 exit status
:-1: error: [Makefile:806: TestApp] Error 1
I can't understand the reason for the undefined symbols (includes and loading order of libraries should be ok), so I am asking for help.
Note: I'm not sure about the need for -l+library parameter, but without it I received tons of errors.
Thank a lot!
Re: Static OpenCV and undefined reference to symbol
I have created an application in Qt 6.5 in which I create a connection via WiFi to the camera mounted on an Elegoo robot. To use OpenCV 4.6.0 libraries (which I compiled in dll and debug version), in project file I have:
Code:
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# 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 +=-LD:\\OpenCV\\mybuild\\lib\\Debug \
-lopencv_calib3d470d \
-lopencv_core470d \
-lopencv_dnn470d \
-lopencv_features2d470d \
-lopencv_flann470d \
-lopencv_gapi470d \
-lopencv_highgui470d \
-lopencv_imgcodecs470d \
-lopencv_imgproc470d \
-lopencv_ml470d \
-lopencv_objdetect470d \
-lopencv_photo470d \
-lopencv_stitching470d \
-lopencv_ts470d \
-lopencv_video470d \
-lopencv_videoio470d
In the static version of OpenCV, it means having the .a libraries to put in -lopencv_...d (in the debug version).
Re: Static OpenCV and undefined reference to symbol
Thank you very much for your reply.
I compiled my code under Windows to compare the result, and OpenCV's behavior is the same (dynamic ok, static ko)
In my understanding, the method
LIBS += -LLIBS_PATH -lopencv_... is for shared libraries. If I try to use it with static libs (and the folder "C:\OpenCV_Static\3.4.16\lib" contains only the *.a files)
Code:
LIBS +=-LC:\OpenCV_Static\3.4.16\lib \
-lopencv_calib3d \
-lopencv_core \
-lopencv_features2d \
-lopencv_flann \
-lopencv_highgui \
-lopencv_imgcodecs \
-lopencv_imgproc \
-lopencv_ml \
-lopencv_objdetect \
-lopencv_photo \
-lopencv_shape \
-lopencv_stitching \
-lopencv_superres \
-lopencv_video \
-lopencv_videoio \
-lopencv_videostab
the result is
:-1: error: cannot find -lopencv_calib3d
:-1: error: cannot find -lopencv_core
:-1: error: cannot find ...
because, probably, it tries to load the dll versions, without founding them.
Re: Static OpenCV and undefined reference to symbol
Have you checked that they are actually the static libraries and not those to be able to load the dlls ?
Re: Static OpenCV and undefined reference to symbol
Yes, I use different folders for static and shared libs, in order to avoid incorrect loading, so I confirm I'm trying to load the static version.
After some tests, I suppose that a possible error can be caused by the wrong order or lack of calling of some libraries, but I thought that if it works as dynamic should work in static...
I wrote "Note: I'm not sure about the need for -l+library parameter, but without it I received tons of errors.", now I'm pretty sure it's wrong, and -l+library parameter should be avoided in the loading of the static libs.
Re: Static OpenCV and undefined reference to symbol
In my opinion, it's correct as you put in the .pro file, ie inserting the list of static libraries used in the project. You need to better control the project files instead. I've seen in the Qt documentation that you need to insert the right macros to recognize that the project wants the OpenCV static libraries. So make sure that the OpenCV libraries you use are actually the static ones.
Re: Static OpenCV and undefined reference to symbol
I don't know if the idea can solve your problem, but if you are developing directly using the Qt Creator ide, then try to open the .pro project file and in a free point of the file press the right mouse button so that it appears a pop-up menu and click on the "Add Library" item. In the dialog that appears click on the "External Library" option. In the new window select the type of library you want to insert (in your case Statica) and also on which platform you are using (I suppose Windows, so you have to deselect Linux and Mac) and then pressing the "Browse..." buttons go to the path of the directory where your OpenCV static libraries are and the path where the OpenCV include file is. Then press the "Next" button and confirm by pressing the "Finish" button. The lines for inserting static libraries and their path will appear in your .pro file. At this point, the compilation should be successful.
Re: Static OpenCV and undefined reference to symbol
In this project I used different static libs, as cryptopp and others, so I'm quite sure my .pro file includes the right macros. I had already tried to do what giorgik suggested, but the result was the same. By now, I'm using the shared opencv library, to avoid spending too much time, and after the end of the project, I'll return to face this matter.
Thanks to everyone!