OpenCV undefined references for libQt5Core.so.5

5,215

In short: I solved the problem by rebuilding OpenCV from scratch. Credit for this solution goes to user Tomáš Pospíšek.

The installation of OpenCV relied on the Python3 installation and packages of Anaconda instead of root. No matter the content of PATH, PYTHONPATH, or LD_LIBRARY_PATH, and regardless the fact I had renamed the anaconda folder to anaconda_renamed to avoid it being ever found, OpenCV itself was built to rely on Anaconda's packages. Not surprisingly, at this point they were not found.

The problem has not been easy to fix, as even when I tried to rebuild OpenCV from scratch using the standard cmake installation instruction by OpenCV, Anaconda still ended up in the path. However, by using cmake-gui, and by searching for anaconda in the fields, I managed to find where it was used.

After several attempts to gracefully solve this issue by linking the fields of cmake away from anaconda, and seeing how by default CMake kept finding Anaconda packages here and there, I ended up deleting Anaconda from my system, pulling the latest version of opencv and opencv-contrib and following OpenCV's installation instructions once again.

The make process resulted in some errors with some packages of CUDA (something like cudaarithm and cudatest). Excluding them from the build using cmake-gui worked. I hope I will never regret these exclusions.

PS: if other unlucky users will also find that their make -j4 command results into the building process getting stuck at 99% for hours, try make -j1 instead.

Share:
5,215
raggot
Author by

raggot

Updated on September 18, 2022

Comments

  • raggot
    raggot over 1 year

    Before starting, I would like to point out there actually is a very similar (probably exactly identical) question. However, I have been unable to solve my problem looking at the accepted answers.

    I followed the instructions provided by this website to install OpenCV3 on Ubuntu 16.04. I reached the point where I tried to compile a test program (section 6) with g++. The compilation returns this:

    /usr/bin/ld: warning: libicui18n.so.58, needed by //home/raggot/anaconda3/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)
    /usr/bin/ld: warning: libicuuc.so.58, needed by //home/raggot/anaconda3/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)
    /usr/bin/ld: warning: libicudata.so.58, needed by //home/raggot/anaconda3/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `u_errorName_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_setMillis_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_getAlias_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_inDaylightTime_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `u_strToLower_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_getStandardName_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `u_strToUpper_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_setSubstChars_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_getMaxCharSize_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_getTimeZoneDisplayName_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_fromUnicode_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_open_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_getDefaultName_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_getDefaultTimeZone_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_clone_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_getDSTSavings_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucol_strcoll_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_close_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_countAvailable_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_openCountryTimeZones_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucol_open_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_compareNames_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_close_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_getAvailableName_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_openTimeZoneIDEnumeration_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_open_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucol_setAttribute_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_openTimeZones_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `uenum_close_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_countAliases_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucol_close_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucol_getSortKey_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucal_get_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `uenum_next_58'
    //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to `ucnv_toUnicode_58'
    collect2: error: ld returned 1 exit status
    

    Attempting to apply this answer to my problem, I tried to localise the installation path of Qt5 on my system. I did it by using dpkg --listfiles libqt5core5a. I added the resulting path to LD_LIBRARY_PATH, but it didn't help.

    I tried reinstalling Qt5 by sudo apt install qt5-default and also installed qttools5-dev but it also did not help.

    My problem might have something to do with the fact the compiler is using anaconda's library, while I am actually working in the global environment. Some more research showed also that my Qt5 installation using apt install has version 5.5.1, while if I type qtdiag I see version 5.6.2 showing up. That could be because at some point I might have installed Qt5 under anaconda as well. So, I might have 2 installations. I don't know how to verify this though.

    What could be the cause of the problem?

    Edit:

    Thanks to the comments, I realised anaconda3 should not even be in the PATH in the first place. With some hacking around, I found that it was added to the path at login in .bashrc, probably because I asked so during the installation of Anaconda. I removed it and tried to make again.

    This is what I now see (extract of a very long output):

    /usr/local/lib/libopencv_imgcodecs.so: undefined reference to `jpeg_stdio_dest@LIBJPEG_9.0'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QGraphicsView::staticMetaObject@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QSize::scaled(QSize const&, Qt::AspectRatioMode) const@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QWidget::update()@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QWidget::paintEngine() const@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QAbstractSlider::setMinimum(int)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QCheckBox::minimumSizeHint() const@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QFileInfo::QFileInfo(QString const&)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QWidget::hideEvent(QHideEvent*)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QObject::connectNotify(QMetaMethod const&)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `non-virtual thunk to QBoxLayout::hasHeightForWidth() const@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QWidget::isFullScreen() const@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QWidget::hasHeightForWidth() const@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QPen::QPen(QBrush const&, double, Qt::PenStyle, Qt::PenCapStyle, Qt::PenJoinStyle)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QStatusBar::showMessage(QString const&, int)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QObject::customEvent(QEvent*)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QObject::QObject(QObject*)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QFont::setLetterSpacing(QFont::SpacingType, double)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QGraphicsView::dragLeaveEvent(QDragLeaveEvent*)@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `typeinfo for QHBoxLayout@Qt_5'
    /usr/local/lib/libopencv_highgui.so: undefined reference to `QGLWidget::~QGLWidget()@Qt_5'
    

    This makes me think I now just miss something in the PATH.

    I found where these libraries are defined (in my installation of Qt5) in the subfolders of /usr/include/x86_64-linux-gnu/qt5/, and subsequently added all of them to PATH by means of export commands in .profile, and then run source .profile. If I now run echo $PATH I see them all in place.

    Unfortunately, the error message is still the same if I run the make command.

    Edit #2:

    After adding all subfolders containing the headers of Qt5 to PATH, I tried to add a copy of PATH in LD_LIBRARY_PATH by doing

    export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PATH
    

    Once again however, the error message did not change in nature. I really don't understand why the compiler doesn't see the Qt5 libraries.

    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      The linker is looking for libicui18n.so.58, libicuuc.so.5 and libicudata.so.58, but doesn't find them. If you do not have them installed, then you will need to do so. If you do have them installed, then you'll need to tell the linker how to find them by extending the linker load path as in here:stackoverflow.com/a/40327731/4640820
    • raggot
      raggot about 6 years
      @TomášPospíšek, thanks for the input. I don't think it's those libraries because I checked darknet's make error log and it doesn't refer to them (I edited my question to extend info there). I do think the path is missing or incorrect, but I don't know how to check or fix that. Would you have any suggestions on how I can find out what exact path I should provide, and where?
    • terdon
      terdon about 6 years
      If you want this on Stack Overflow instead, simply delete it here and repost it there. That's better than migrating anyway since that way the question will appear as newly asked on SO.
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      "So, I might have 2 installations. I don't know how to verify this though." -> You can use find / -name 'libqt*'. Also you can do ẁhich qtdiag` and ldd $(which qtdiag).
    • raggot
      raggot about 6 years
      @TomášPospíšek, thanks a lot for the suggestions! I think which qtdiag brought me to the right direction. The answer is /home/raggot/anaconda3/bin/qtdiag where I would have expected something at root level like /usr/lib/x86_64-linux-gnu/qt5/. That strengthens my feeling make is looking in the wrong place. In any case, I tried to add the folders I got using find to LD_LIBRARY_PATH and tried to make again, but no luck :(
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      So the problem is, that when running make the qtdiag (and other things from QT) from anaconda get found and used instead of the system ones. That should not be. Do set | grep anaconda. Probably anaconda is in your $PATH but maybe also in LD_LIBlRARY_PATH. Either you've added it there yourself or anaconda did it when you've installed it. It probably gets added in ~/.bashrc, ~/.profile or such. Remove the anaconda paths from $PATH and elsewhere. Try make and which again.
    • raggot
      raggot about 6 years
      @TomášPospíšek, I just tried it. I did manage to get /usr/bin/qtdiag from which after removing all traces of anaconda from PATH and LD_LIBRARY_PATH, but sadly the problem persists. In the moment I make I receive the same bunch of //home/raggot/anaconda3/lib/libQt5Core.so.5: undefined reference to 'ucnv_toUnicode_58' errors. Is there a command or a smart way to find where exactly all these functions are defined? Perhaps they are not in the same location as qtdiag (which by the way, is already both in PATH and in LD_LIBRARY_PATH, I made sure). Thanks for your time.
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      That path - /home/raggot/anaconda3 must get into the compile procedure somehow, right? It shouldn't. Now the question is: how does it get in there. So one thing you can do is to adduser ra; mv /home/raggot/your_open_cv_project /home/ra; chown -R ra.ra /home/ra (or whereever, but out of /home/raggot) and then launch your compilation and watch when /home/raggot pops up for the first time. You can use strace to do that. You can have strace follow accross forks. And you can have strace only show you file access system calls.
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      Also, maybe you can add options to make, ld and gcc to tell you more about what they are accessing. And also you can grep all the stuff they access for /home/raggot. Also you can recursively find / -exec grep -l /home/raggot \{\}; your entire disk for /home/raggot to find out who is referencing /home/raggot. That way you'll certainly find out. It's the atomic bomb of analysis alas.
    • raggot
      raggot about 6 years
      @TomášPospíšek, I will try it tonight I hope. Thanks for your persistence so far :)
    • raggot
      raggot about 6 years
      @TomášPospíšek, I hadn't thought anaconda3 was not supposed to be searched anymore or included by make. I used strace on make but didn't see anything useful. I am now using the other search commands (I think the one you passed me with find does not work but it still gave me the right idea) to find where anaconda3 is added to path, or who searches it. If this one will not work, I'll attempt the new user. So far the weird thing I see is that despite I unset PATH and redefine it in .profile, when I open a new terminal the PATH still contains the anaconda folder.
    • raggot
      raggot about 6 years
      @TomášPospíšek, after you set me to the right direction I finally had time to make some tests. I found out why anaconda was in the path and edited the question with the latest error message. I think we are getting very close and it's now just a matter of adding the right path
    • raggot
      raggot about 6 years
      @TomášPospíšek, I edited my question with some new info
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      The error messages, f.ex. this one: "/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `jpeg_stdio_dest@LIBJPEG_9.0'" tells you: "hello I am the linker. I am trying to put your program together from various parts. One of those parts, libopencv_imgcodecs.so, requires a function (or symbol) jpeg_stdio_dest@LIBJPEG_9.0 to exist. However there is not part, that provides that function (or symbol). Help. I'm lost.".
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      I guess that you had built libopencv_imgcodecs.so at the time when you had the anaconda directory in your library path and so the build of libopencv_imgcodecs.so contains references (i.e. is trying to use) functions/symbols that it saw in the anaconda-provided libraries. So I suggest you start from scratch - i.e. have a look on what is in /usr/local and delete anything there that is associated with OpenCV. You'll probably most easily see the association via the creation date of the relevant files = same date as /usr/local/lib/libopencv*.
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      Once you've got that cleaned, do rebuild OpenCV.
    • raggot
      raggot about 6 years
      @TomášPospíšek you once again gave me a great clue. I tried to remake OpenCV, and there I realised that the outcome of cmake told me that the source of Python3 and several libraries (such as PNG and TIFF which gave issues in warnings) were going to be found in the path of Anaconda. The thing is, by this point I have already renamed that path to exclude any issue. What I think is that somewhere Python3 has been configured to always look for that path, and has those libraries mapped. Does that make sense? Thanks!
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      I do not know about where/how python gets its parts. Also your outcomes will depend on whether the various components pull their configurations/dependencies/packages from your user environment or from the system environment (that is from root's). What about instead restarting in a fresh environment? F.ex. install a minimal Linux installation in a chroot environment, or make a virtual machine, and then rebuild your stuff there. You then can take a tool like checkinstall and do the final make install step with checkinstall.
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      checkinstall would track the installed files and you should be able to install the files then in your original environment. However you'll need to make sure that your original environment and the build environment are the same distribution, in order to remain compatible...
    • raggot
      raggot about 6 years
      @TomášPospíšek, I appreciate the suggestion, but reinstalling everything would include also CUDA, which has been a nightmare to get working!... I must pass on that idea. Perhaps uninstalling Anaconda will clean its mess though. I'll try that and see if anything gets better.
    • raggot
      raggot about 6 years
      @TomášPospíšek, it finally worked. Thanks for your heroic patience.
    • Tomáš Pospíšek
      Tomáš Pospíšek about 6 years
      "Thanks for your heroic patience" - likewise ;-) I'm happy you finally got it to work :-)!
  • otto
    otto over 3 years
    thx. helped me a lot. I removed anaconda complety and removed opencv and installed it with this script: gist.github.com/Mahedi-61/804a663b449e4cdb31b5fea96bb9d561