Linking to external shared library.
-
Hello,
i am using Qt 6.3.1 with Qt Creator 7 on Linux. When i try to call a function in my added C Shared Library i get undefined reference <function>. The linker shows the -L and the l<shared name> in the compile output. There are no errors if I comment out the shared C function . I have moved the shared library and its symbolic links to a library folder and re-ran ldconfig -v and it shows it finds the shared library. When I run ldd on the generated binary it does not list my shared library in the output. Any ideas?
Thanks..
-
The most common reason for such behaviour is that you link to another (version of the) library. It can also be a dependency of that shared library to another one, but you’d see that in the include statement already.
Please paste your .pro / CMakeLists.txt, compiler command and output. In Qt creator the latter is in the compiler tab. -
@Axel-Spoerl Hello and thank you for replying...
Here is requested snippets
QT += core gui 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 \ bbsmonitor.cpp HEADERS += \ bbsmonitor.h FORMS += \ bbsmonitor.ui INCLUDEPATH += /home/chris/sourceCode/gitCode/falkenbbs/include # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../gitCode/falkenbbs/shared/release/ -lfalken else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../gitCode/falkenbbs/shared/debug/ -lfalken else:unix: LIBS += -L$$PWD/../../../gitCode/falkenbbs/shared/ -lfalken DEPENDPATH += $$PWD/../../../gitCode/falkenbbs/shared
And the compiled output
07:32:34: Running steps for project bbsMonitor... 07:32:34: Configuration unchanged, skipping qmake step. 07:32:34: Starting: "/usr/bin/make" -j2 /home/chris/Qt/6.3.1/gcc_64/libexec/uic ../bbsMonitor/bbsmonitor.ui -o ui_bbsmonitor.h g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../bbsMonitor -I. -I../../../gitCode/falkenbbs/include -I../../../../Qt/6.3.1/gcc_64/include -I../../../../Qt/6.3.1/gcc_64/include/QtWidgets -I../../../../Qt/6.3.1/gcc_64/include/QtGui -I../../../../Qt/6.3.1/gcc_64/include/QtCore -I. -I. -I../../../../Qt/6.3.1/gcc_64/mkspecs/linux-g++ -o main.o ../bbsMonitor/main.cpp g++ -pipe -g -std=gnu++1z -Wall -Wextra -dM -E -o moc_predefs.h ../../../../Qt/6.3.1/gcc_64/mkspecs/features/data/dummy.cpp g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../bbsMonitor -I. -I../../../gitCode/falkenbbs/include -I../../../../Qt/6.3.1/gcc_64/include -I../../../../Qt/6.3.1/gcc_64/include/QtWidgets -I../../../../Qt/6.3.1/gcc_64/include/QtGui -I../../../../Qt/6.3.1/gcc_64/include/QtCore -I. -I. -I../../../../Qt/6.3.1/gcc_64/mkspecs/linux-g++ -o bbsmonitor.o ../bbsMonitor/bbsmonitor.cpp /home/chris/Qt/6.3.1/gcc_64/libexec/moc -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB --include /home/chris/sourceCode/qtCode/falkenbbs/build-bbsMonitor-Desktop_Qt_6_3_1_GCC_64bit-Debug/moc_predefs.h -I/home/chris/Qt/6.3.1/gcc_64/mkspecs/linux-g++ -I/home/chris/sourceCode/qtCode/falkenbbs/bbsMonitor -I/home/chris/sourceCode/gitCode/falkenbbs/include -I/home/chris/Qt/6.3.1/gcc_64/include -I/home/chris/Qt/6.3.1/gcc_64/include/QtWidgets -I/home/chris/Qt/6.3.1/gcc_64/include/QtGui -I/home/chris/Qt/6.3.1/gcc_64/include/QtCore -I. -I/usr/include/c++/9 -I/usr/include/x86_64-linux-gnu/c++/9 -I/usr/include/c++/9/backward -I/usr/lib/gcc/x86_64-linux-gnu/9/include -I/usr/local/include -I/usr/include/x86_64-linux-gnu -I/usr/include ../bbsMonitor/bbsmonitor.h -o moc_bbsmonitor.cpp g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../bbsMonitor -I. -I../../../gitCode/falkenbbs/include -I../../../../Qt/6.3.1/gcc_64/include -I../../../../Qt/6.3.1/gcc_64/include/QtWidgets -I../../../../Qt/6.3.1/gcc_64/include/QtGui -I../../../../Qt/6.3.1/gcc_64/include/QtCore -I. -I. -I../../../../Qt/6.3.1/gcc_64/mkspecs/linux-g++ -o moc_bbsmonitor.o moc_bbsmonitor.cpp g++ -Wl,-rpath,/home/chris/Qt/6.3.1/gcc_64/lib -Wl,-rpath-link,/home/chris/Qt/6.3.1/gcc_64/lib -o bbsMonitor main.o bbsmonitor.o moc_bbsmonitor.o -L/home/chris/sourceCode/qtCode/falkenbbs/bbsMonitor/../../../gitCode/falkenbbs/shared/ -lfalken /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Widgets.so /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Gui.so /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Core.so -lpthread -lGL 07:32:38: The process "/usr/bin/make" exited normally. 07:32:38: Elapsed time: 00:04.
And when you run ldd against the binary this is the output...
linux-vdso.so.1 (0x00007ffec90f4000) libQt6Widgets.so.6 => /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Widgets.so.6 (0x00007f1e5f0c3000) libQt6Core.so.6 => /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Core.so.6 (0x00007f1e5ea60000) libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1e5e86a000) libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1e5e84f000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1e5e65d000) libQt6Gui.so.6 => /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Gui.so.6 (0x00007f1e5dcda000) libGL.so.1 => /lib/x86_64-linux-gnu/libGL.so.1 (0x00007f1e5dc50000) libxkbcommon.so.0 => /lib/x86_64-linux-gnu/libxkbcommon.so.0 (0x00007f1e5dc0e000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f1e5dbeb000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1e5da9c000) libicui18n.so.56 => /home/chris/Qt/6.3.1/gcc_64/lib/libicui18n.so.56 (0x00007f1e5d603000) libicuuc.so.56 => /home/chris/Qt/6.3.1/gcc_64/lib/libicuuc.so.56 (0x00007f1e5d24b000) libicudata.so.56 => /home/chris/Qt/6.3.1/gcc_64/lib/libicudata.so.56 (0x00007f1e5b866000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f1e5b860000) libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f1e5b737000) libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f1e5b71b000) libgthread-2.0.so.0 => /lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f1e5b716000) librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f1e5b70c000) /lib64/ld-linux-x86-64.so.2 (0x00007f1e5f7f5000) libEGL.so.1 => /lib/x86_64-linux-gnu/libEGL.so.1 (0x00007f1e5b6f5000) libfontconfig.so.1 => /lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f1e5b6ae000) libX11.so.6 => /lib/x86_64-linux-gnu/libX11.so.6 (0x00007f1e5b571000) libQt6DBus.so.6 => /home/chris/Qt/6.3.1/gcc_64/lib/libQt6DBus.so.6 (0x00007f1e5b4a1000) libfreetype.so.6 => /lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f1e5b3e2000) libGLdispatch.so.0 => /lib/x86_64-linux-gnu/libGLdispatch.so.0 (0x00007f1e5b328000) libGLX.so.0 => /lib/x86_64-linux-gnu/libGLX.so.0 (0x00007f1e5b2f4000) libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f1e5b281000) libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f1e5b253000) libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f1e5b24a000) libxcb.so.1 => /lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f1e5b220000) libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007f1e5b1cd000) libpng16.so.16 => /lib/x86_64-linux-gnu/libpng16.so.16 (0x00007f1e5b195000) libXau.so.6 => /lib/x86_64-linux-gnu/libXau.so.6 (0x00007f1e5b18f000) libXdmcp.so.6 => /lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f1e5b187000) libsystemd.so.0 => /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007f1e5b0d8000) libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f1e5b0bc000) liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f1e5b093000) liblz4.so.1 => /lib/x86_64-linux-gnu/liblz4.so.1 (0x00007f1e5b072000) libgcrypt.so.20 => /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007f1e5af54000) libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007f1e5af31000)
As you can see libfalken is not linked to the binary. Even if I was missing a symbolic link it would still show the library with a Not Found error.
I've tried with CMake and it showed the library being linked in compiler tab but same outcome so I switched to QMake and added the library via the Add Library tab. If I put a function in my code from the library it will error out.
Thanks...
-
Then you maybe don't use any function from this library so the linker doesn't need to add it. qmake pass the correct flags to g++/the linker so it's not a qmake (or cmake) problem.
-
When I try to use a function from that library it fails to compile
09:40:55: Running steps for project bbsMonitor... 09:40:55: Configuration unchanged, skipping qmake step. 09:40:55: Starting: "/usr/bin/make" -j2 g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../bbsMonitor -I. -I../../../gitCode/falkenbbs/include -I../../../../Qt/6.3.1/gcc_64/include -I../../../../Qt/6.3.1/gcc_64/include/QtWidgets -I../../../../Qt/6.3.1/gcc_64/include/QtGui -I../../../../Qt/6.3.1/gcc_64/include/QtCore -I. -I. -I../../../../Qt/6.3.1/gcc_64/mkspecs/linux-g++ -o bbsmonitor.o ../bbsMonitor/bbsmonitor.cpp g++ -Wl,-rpath,/home/chris/Qt/6.3.1/gcc_64/lib -Wl,-rpath-link,/home/chris/Qt/6.3.1/gcc_64/lib -o bbsMonitor main.o bbsmonitor.o moc_bbsmonitor.o -L/home/chris/sourceCode/qtCode/falkenbbs/bbsMonitor/../../../gitCode/falkenbbs/shared/ -lfalken /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Widgets.so /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Gui.so /home/chris/Qt/6.3.1/gcc_64/lib/libQt6Core.so -lpthread -lGL /usr/bin/ld: bbsmonitor.o: in function `bbsMonitor::bbsMonitor(QWidget*)': /home/chris/sourceCode/qtCode/falkenbbs/build-bbsMonitor-Desktop_Qt_6_3_1_GCC_64bit-Debug/../bbsMonitor/bbsmonitor.cpp:10: undefined reference to `util_init()' collect2: error: ld returned 1 exit status make: *** [Makefile:342: bbsMonitor] Error 1 09:40:58: The process "/usr/bin/make" exited with code 2. Error while building/deploying project bbsMonitor (kit: Desktop Qt 6.3.1 GCC 64bit) When executing step "Make" 09:40:58: Elapsed time: 00:03.
The util_init function resides within the library I have linked to the binary. Actually calling any function within the library generates the same error...
-
Then you maybe don't use any function from this library so the linker doesn't need to add it. qmake pass the correct flags to g++/the linker so it's not a qmake (or cmake) problem.
@Christian-Ehrlicher said in Linking to external shared library.:
Then you maybe don't use any function from this library so the linker doesn't need to add it. qmake pass the correct flags to g++/the linker so it's not a qmake (or cmake) problem.
Hey, thanks, you made me rethink things... And you were 100% correct. I needed to declare the function as an extern "C" function. Once that was done it compiles and works great!
Thanks for the insight..