Results 1 to 8 of 8

Thread: Static OpenCV and undefined reference to symbol

  1. #1
    Join Date
    Mar 2016
    Posts
    22
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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):
    Qt Code:
    1. LIBS +=-L/usr/local/lib/
    2. LIBS += -lopencv_calib3d
    3. LIBS += -lopencv_core
    4. LIBS += -lopencv_features2d
    5. LIBS += -lopencv_flann
    6. LIBS += -lopencv_highgui
    7. LIBS += -lopencv_imgcodecs
    8. LIBS += -lopencv_imgproc
    9. LIBS += -lopencv_ml
    10. LIBS += -lopencv_objdetect
    11. LIBS += -lopencv_photo
    12. LIBS += -lopencv_shape
    13. LIBS += -lopencv_stitching
    14. LIBS += -lopencv_superres
    15. LIBS += -lopencv_video
    16. LIBS += -lopencv_videoio
    17. LIBS += -lopencv_videost
    To copy to clipboard, switch view to plain text mode 
    and it's ok. but when I tried to use the static version replacing the previous lines with this code:
    Qt Code:
    1. LIBS +=/usr/local/lib/libopencv_calib3d.a -lopencv_calib3d
    2. LIBS +=/usr/local/lib/libopencv_core.a -lopencv_core
    3. LIBS +=/usr/local/lib/libopencv_features2d.a -lopencv_features2d
    4. LIBS +=/usr/local/lib/libopencv_flann.a -lopencv_flann
    5. LIBS +=/usr/local/lib/libopencv_highgui.a -lopencv_highgui
    6. LIBS +=/usr/local/lib/libopencv_imgcodecs.a -lopencv_imgcodecs
    7. LIBS +=/usr/local/lib/libopencv_imgproc.a -lopencv_imgproc
    8. LIBS +=/usr/local/lib/libopencv_ml.a -lopencv_ml
    9. LIBS +=/usr/local/lib/libopencv_objdetect.a -lopencv_objdetect
    10. LIBS +=/usr/local/lib/libopencv_photo.a -lopencv_photo
    11. LIBS +=/usr/local/lib/libopencv_shape.a -lopencv_shape
    12. LIBS +=/usr/local/lib/libopencv_stitching.a -lopencv_stitching
    13. LIBS +=/usr/local/lib/libopencv_superres.a -lopencv_superres
    14. LIBS +=/usr/local/lib/libopencv_video.a -lopencv_video
    15. LIBS +=/usr/local/lib/libopencv_videoio.a -lopencv_videoio
    16. LIBS +=/usr/local/lib/libopencv_videostab.a -lopencv_videostab
    To copy to clipboard, switch view to plain text mode 
    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!

  2. #2
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. QT += core gui network
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. CONFIG += c++17
    6.  
    7. # You can make your code fail to compile if it uses deprecated APIs.
    8. # In order to do so, uncomment the following line.
    9. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    10.  
    11. SOURCES += \
    12. main.cpp \
    13. mainwindow.cpp
    14.  
    15. HEADERS += \
    16. mainwindow.h
    17.  
    18. FORMS += \
    19. mainwindow.ui
    20.  
    21. INCLUDEPATH +=D:\\OpenCV\\build\\include
    22.  
    23. LIBS +=-LD:\\OpenCV\\mybuild\\lib\\Debug \
    24. -lopencv_calib3d470d \
    25. -lopencv_core470d \
    26. -lopencv_dnn470d \
    27. -lopencv_features2d470d \
    28. -lopencv_flann470d \
    29. -lopencv_gapi470d \
    30. -lopencv_highgui470d \
    31. -lopencv_imgcodecs470d \
    32. -lopencv_imgproc470d \
    33. -lopencv_ml470d \
    34. -lopencv_objdetect470d \
    35. -lopencv_photo470d \
    36. -lopencv_stitching470d \
    37. -lopencv_ts470d \
    38. -lopencv_video470d \
    39. -lopencv_videoio470d
    To copy to clipboard, switch view to plain text mode 
    In the static version of OpenCV, it means having the .a libraries to put in -lopencv_...d (in the debug version).

  3. #3
    Join Date
    Mar 2016
    Posts
    22
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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)
    Qt Code:
    1. LIBS +=-LC:\OpenCV_Static\3.4.16\lib \
    2. -lopencv_calib3d \
    3. -lopencv_core \
    4. -lopencv_features2d \
    5. -lopencv_flann \
    6. -lopencv_highgui \
    7. -lopencv_imgcodecs \
    8. -lopencv_imgproc \
    9. -lopencv_ml \
    10. -lopencv_objdetect \
    11. -lopencv_photo \
    12. -lopencv_shape \
    13. -lopencv_stitching \
    14. -lopencv_superres \
    15. -lopencv_video \
    16. -lopencv_videoio \
    17. -lopencv_videostab
    To copy to clipboard, switch view to plain text mode 
    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.

  4. #4
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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 ?

  5. #5
    Join Date
    Mar 2016
    Posts
    22
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

  6. #6
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  7. #7
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  8. #8
    Join Date
    Mar 2016
    Posts
    22
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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!

Similar Threads

  1. Static timer attribute - undefined reference error
    By Leutzig in forum General Programming
    Replies: 1
    Last Post: 9th May 2016, 12:51
  2. Undefined reference to qt-Lib after compiling qt static for amv7 under windows
    By michael_endres in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 23rd January 2014, 17:15
  3. Replies: 2
    Last Post: 11th August 2012, 17:37
  4. Replies: 0
    Last Post: 10th May 2011, 14:58
  5. Undefined reference in static binding in Linux
    By gcubar in forum Installation and Deployment
    Replies: 9
    Last Post: 28th February 2011, 15:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.