Linking to shared libraries that call other shared

2019-09-10 07:25发布

问题:

I have a shared lib that i've written/built that uses libusb

when i build that i have to link with the libusb shared lib - so far so straightforward

but if i then write/build an exe that uses my shared lib, when i build that exe do i have to link to libusb as well

i would have thought the answer should be no, as long as my shared lib knows where to find libusb that should be fine

but in practice, the answer is yes, the exe does have to link to libusb or it complains it can't find references to the libusb functions that my shared lib calls.

why should my exe need to link to libusb as well as the shared lib - surely it should devolve responsibility to the shared lib and not have to worry about it?

UPDATE:

to try and visualise it:

myexe
 |
 | links to:
 |
mylib.so
 |
 | links to:
 |
libusb-1.0.so

it seems superfluous to me that myexe also has to explicitly link to libusb-1.0 as this knowledge is already implicit from previous linking?

to clarify, myexe only calls functions defined in mylib.so, it doesn't call any libusb-1.0 functions 'directly'. The only calls to libusb-1.0 functions are made from functions in mylib.so

回答1:

Yes, in theory you have to link your libusb as well. But there are tools like libtool which can automate the third level library dependency linking. So libtool will link libusb automatically when you link your mylib.so

Refer http://www.gnu.org/software/libtool/manual/html_node/Inter_002dlibrary-dependencies.html