title: Linking with dynamic library with dependencies
body: <p>Consider the following scenario:</p>
  
  <ul>
  <li>Shared Library libA.so ,with no dependencies.</li>
  <li>Shared Library libB.so, with libA.so as its dependency.</li>
  </ul>
  
  <p>I want to compile a binary file that links with the libB.
  Should I link the binary with libB only or with libA either?</p>
  
  <p>Is there any way to link only with the direct dependencies, letting the resolution of unresolved symbols from the dependencies for runtime?</p>
  
  <p>I'm worried about the fact that the library libB implementation may change in the future, introducing other dependencies (libC, libD, libE for instance). Am I going to have problems with that?</p>
  
  <p>In other words:</p>
  
  <ul>
  <li>libA files: a.cpp a.h</li>
  <li>libB files: b.cpp b.h</li>
  <li>main program files: main.cpp</li>
  </ul>
  
  <p>Of course, b.cpp includes a.h and main.cpp includes b.h.</p>
  
  <p>Compilation commands:</p>
  
  <pre><code>g++ -fPIC a.cpp -c
  g++ -shared -o libA.so a.o
  
  g++ -fPIC b.cpp -c -I.
  g++ -shared -o libB.so b.o -L. -lA
  </code></pre>
  
  <p>Which of the bellow options should I use?</p>
  
  <pre><code>g++ main.cpp -o main -I. -L. -lB
  </code></pre>
  
  <p>or</p>
  
  <pre><code>g++ main.cpp -o main -I. -L. -lB -lA
  </code></pre>
  
  <p>I couldn't use the first option. The linker complains about the unresolved symbols from the library libA. But it sound a little strange to me.</p>
  
  <p>Thanks very much.</p>
  
  <p><strong>-- Updated comments:</strong></p>
  
  <p>When I link the binary, the linker will try to resolve all symbols from the main and the libB. However, libB has undefined symbols from the libA. That's why the linker complains about that.</p>
  
  <p>That's why I need to link with the libA too.
  However I found a way to ignore unresolved symbols from shared libraries.
  Looks like I should use the following command line to do that:</p>
  
  <pre><code>g++ main.cpp -o main -I. -L. -lB -Wl,-unresolved-symbols=ignore-in-shared-libs
  </code></pre>
  
  <p>Looks like it is still possible to use the <code>-rpath</code> option.
  However I need to understand it a little better.</p>
  
  <p>Does anyone knows any possible pitfalls when using the <code>-Wl,-unresolved-symbols=ignore-in-shared-libs</code> option?</p>
  
  <p><strong>-- Updated comments 2:</strong></p>
  
  <p><code>-rpath</code> should not be used for this purpose. It is useful to force a library to be found in a given directory. The <code>-unresolved-symbol</code> approach looks much better.</p>
  
  <p>Thanks again.</p>