Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically link to dependencies of shared library

Tags:

c++

g++

linker

qt

ld

I am trying to build a small Qt (C++) GUI application on Linux but it fails to build with numerous linker errors, complaining about missing dependencies for the Qt library I am linking against. I used ldd on the Qt libraries to verify that the libraries are indeed there - and they are.

My problem seems to be related to the discussion in this thread: Linking dependencies of a shared library And while that thread helped me identify my precise problem, it seems that the conclusion of that thread was that my application should link!

The application is compiled with the following command:

g++ -m64 -Wl,-O1 -o Executable some-object.o some-other-object.o -lQtCore -lQtGui -lQtXml -L/usr/lib64 -L/usr/X11R6/lib64 -lpthread

Running this generates warnings of the following form, and linking eventually fails with undefined reference errors (to symbols defined in the 'missing' libraries):

.../ld: warning: libglib-2.0.so.0, needed by /usr/lib64/libQtGui.so, not found (try using -rpath or -rpath-link)
.../ld: warning: libpng14.so.14, needed by /usr/lib64/libQtGui.so, not found (try using -rpath or -rpath-link)
.../ld: warning: libz.so.1, needed by /usr/lib64/libQtGui.so, not found (try using -rpath or -rpath-link)
.../ld: warning: libfreetype.so.6, needed by /usr/lib64/libQtGui.so, not found (try using -rpath or -rpath-link)

and so on (in total there are 18 dependencies that could not be found.)

I can get this to compile if I go and explicitly add -lglib, -lpng14, -lz -lfreetype and so on, but as I mentioned there are 18 dependencies - and I would rather not do that. It also seems that I should not have to do it.

I have compiled the same project on my laptop computer which uses the exact same Linux Distro (openSuse 12.2) without any troubles. All libraries, including Qt, were installed from the distro repositories.

I think this might be some sort of setup problem on my openSuse install, but I have no idea where to start looking to fix this.

Cheers, Craig

like image 471
Craig Dillabaugh Avatar asked Nov 13 '22 17:11

Craig Dillabaugh


1 Answers

It appears that /usr/lib64/libQtGui.so have hardcoded rpaths in them to locate dependent shared libraries. On one of your hosts the needed libraries are in the expected location while on the other host they are not.

You can use something like elfdump to get the RPATH out of the QT shared library to find out where it will look. Then you can use (I believe) -R on your link command line to point it to where the libraries are actually installed on that host.

EDIT: I think you can do something like objdump -x <binary/library> | grep -i rpath

like image 101
Mark B Avatar answered Nov 15 '22 06:11

Mark B