text: Q:
  
  Is g++ linker statically or dynamically linking libusb and pthreads?
  
  When adding -lusb-1.0 and -pthread to compilation command, are they statically or dynamically linked?
  g++ -pthread -o myprog obj1.o obj2.o -lusb-1.0
  
  ldd gives the following output
  libusb-1.0.so.0 => /lib/x86_64-linux-gnu/libusb-1.0.so.0 (0x00007fc4662b7000)
  libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc465af7000)
  
  .so is a shared object. I am confused. If it is dynamically loading, how are the symbols resolved? I thought .so can be loaded only with dlopen and to obtain address of a symbol one would use dlsym
  
  A:
  
  When adding -lusb-1.0 and -pthread to compilation command, are they statically or dynamically linked?
  
  By default, the shared libraries are linked on Linux. Unless you ask for static linking, the respective shared libraries
  for -pthread and -lusb-1.0 will be linked. You can tell the linker that you want static linking via -static option if you need).
  
  .so is a shared object. I am confused. If it is dynamically loading, how are the symbols resolved?
  
  It's dynamically linked - not loaded at compile time. But there does some work done by the static linker (ld) at compile time so that when the libraries are loaded at run time by the dynamic linker (ld.so), it knows how to locate the symbols.
  What happens at compile time is that the static linker creates "stub" addresses for the symbols in shared libraries in the PLT section (Procedure Link Table). At program load time (or when the symbols are referenced), the dynamic linker/loader "fills" those addresses to the actual addresses This is done via the GOT (Global Offset Table). So the actual symbol resolution & loading is done at runtime but with some help from the static linker at compile time.
  
  I thought .so can be loaded only with dlopen and to obtain address of a symbol one would use dlsym.
  
  That's not true. Shared libraries can be linked both at compile time as well as opened at runtime via dlopen. It's far more common to link at compile time than opened via dlopen. Pretty much all system binaries are in fact dynamically linked on Linux; dynamic linking is the default on all modern Linux systems.
  Check out Drepper's how to write shared libraries if you're interested in details.
  
  