title: Prefer static libraries but fallback to shared if not found
body: <p>GCC (or the linker) prefers dynamic libraries over static ones by default. That means if I have</p>
  <pre><code>libfoo.so
  libfoo.a
  libbar.a
  </code></pre>
  <p>and if I am building another library <code>libmylib.so</code> as follows:</p>
  <pre><code>gcc -shared -o mylib.so ... -lfoo -lbar
  </code></pre>
  <p>the linker will link to <code>libfoo.so</code> (by preference) and <code>libbar.a</code> <strong>since that is the only one available</strong>.</p>
  <hr />
  <p>One can force linking of static libraries by specifying <code>-static</code> (or <code>-Wl,-Bstatic</code>) instead.</p>
  <pre><code>gcc -shared -o mylib.so ... -static -lfoo -lbar
  </code></pre>
  <p>This all works fine and will link to static versions of both libs.</p>
  <p>However if I do not have the static libs it will not fallback to the shared libs. That is if I only have</p>
  <pre><code>libfoo.so
  libbar.a
  </code></pre>
  <p>the linking will fail because of missing <code>libfoo.a</code> <strong>instead of falling back to libfoo.so since it is the only one available</strong>.</p>
  <hr />
  <p>Is there anyway to force this behavior where static libs are preferred but shared libs are picked if there is no static one that matches? (without having to explicitly specify <code>-Wl,-Bdynamic</code> manually for every such case)</p>