What is the purpose of specifying library dependencies with gcc
?
I have written a shared library, libPulse_IO.so
, that calls functions from both libusb-1.0.so
and libpcap.so
.
i.e. in pulse_IO.c
there is stuff like:
#include <libusb-1.0/libusb.h>
#include <pcap.h>
ret = libusb_init(&pdw_io->context);
pdw_io->pcap=pcap_open_live(pdw_io->eth_name, ETH_SNAPLEN, ETH_PROMISCUOUS, ETH_TIMEOUT, pcap_errbuf);
Now when I come to build my library, I notice that it seems to make no difference if I specify -l libusb-1.0 -l pcap
in the call to gcc
i.e. I can run (no mention of -l usb-1.0 or -l pcap):
gcc -fPIC -g -Wall -fvisibility=hidden -I../../../g2/src -I../../core/src pulse_IO.c -c -o ../build/linux/debug/pulse_IO.o
gcc -fPIC -g -Wall -fvisibility=hidden -I../../../g2/src -I../../core/src -shared -Wl,-soname,libPulse_IO_dbg.so ../build/linux/debug/pulse_IO.o -o ../build/linux/debug/libPulse_IO_dbg.so
or (-l usb-1.0 or -l pcap specified):
gcc -fPIC -g -Wall -fvisibility=hidden -I../../../g2/src -I../../core/src -lusb-1.0 -lpcap pulse_IO.c -c -o ../build/linux/debug/pulse_IO.o
gcc -fPIC -g -Wall -fvisibility=hidden -I../../../g2/src -I../../core/src -lusb-1.0 -lpcap -shared -Wl,-soname,libPulse_IO_dbg.so ../build/linux/debug/pulse_IO.o ../build/linux/debug/shared.o -o ../build/linux/debug/libPulse_IO_dbg.so
Both instances build without error.
Running ldd
in both cases yields the same output:
ldd ../build/linux/debug/libPulse_IO_dbg.so
linux-vdso.so.1 => (0x00007ffea93ee000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd43af68000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd43b53f000)
No mention of a dependency on libusb-1.0.so
or libpcap.so
- how?
Examining the shared library produced in both instances shows that there are undefined references to the libusb
and pcap
functions, i.e.
nm ../build/linux/debug/libPulse_IO_dbg.so
U __assert_fail@@GLIBC_2.2.5
U atof@@GLIBC_2.2.5
U atoi@@GLIBC_2.2.5
...
U libusb_init
U libusb_kernel_driver_active
U libusb_open_device_with_vid_pid
...
U pcap_open_live
U pcap_perror
U pcap_sendpacket
U pcap_setfilter
So the library is built in both instances, with or without the -l
library specifiers, and in both instances we can see calls to the libusb-1.0
and libpcap
functions in the symbol table.
Which brings me back to my original question.. what is the purpose of specifying library dependencies with gcc
?
My expectation was that the calls to gcc would have failed without specifying the libusb-1.0
and libpcap
library dependencies?