0

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

bph
  • 10,728
  • 15
  • 60
  • 135
  • 1
    The Linker is concerned about your executable and all the functions it need. No matter where they are. – Milind Dumbare Feb 27 '15 at 13:26
  • it seems like i am duplicating information though, what if libusb-1.0 needs to link to a function in stdlib? - do i then have to explicitly tell the linker about the functions myexe indirectly needs in stdlib? – bph Mar 02 '15 at 12:18
  • 1
    Yes I think so. You have to tell linker about stdlib. – Milind Dumbare Mar 02 '15 at 12:24
  • crikey - that makes no sense! thanks for clarifying though ;) – bph Mar 02 '15 at 12:30
  • Does the answer help you? Should I put it as an answer for others then? – Milind Dumbare Mar 02 '15 at 12:33
  • the answer is helpful if it is correct - my observations battling with linker errors suggest it is - but common sense suggests it is not - ideally i'd like to hear a few other opinions to be wholly convinced of what is required - but for sure, you may as well add an answer – bph Mar 02 '15 at 12:59
  • 1
    If you look at the theory of linking. It replaces the symbol names of functions with addresses. So it makes sense to have addresses of third level libraries known to your binary even if that third level library is accessed by second level library – Milind Dumbare Mar 02 '15 at 13:04
  • but there would be no symbol present in myexe for a function in libusb-1.0.so i.e nothing to replace - thats the whole point of dynamic rather than static linking? – bph Mar 02 '15 at 13:12
  • 1
    There are tools which can help you automate the linking of third level library. Look at this http://www.gnu.org/software/libtool/manual/html_node/Inter_002dlibrary-dependencies.html – Milind Dumbare Mar 02 '15 at 13:55
  • i think thats pretty definitive - much apprectiated - will be very sweet if i can get libtool to automate it all – bph Mar 02 '15 at 14:05

1 Answers1

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

Milind Dumbare
  • 3,104
  • 2
  • 19
  • 32