Mixed static and shared linking of libraries

When distributing binary executable or libraries it is sometimes desirable to link some of their dependencies statically while it may be more efficient to link some of the other dependencies dynamically. For example, we have recently been doing a fair amount of cross-compilation from Linux to Windows using mingw32 and prefer to statically link in the Boost libraries while leaving the standard Windows libraries dynamically linked.

Fortunately GNU ld and gcc support a very simple syntax for doing this sort of mixed linking: it is possible to specify multiple -static and -shared options on the command line. All libraries specified after -static and before any further -shared options will statically linked in. Conversely all libraries specified after -shared and before any -static will be dynamically linked. From the man page for ld(1):

-static
    Do not link against shared libraries.  This is only meaningful on platforms for which shared libraries are supported.  The different
    variants of this option are for compatibility with various systems.  You may use this option multiple times on the command line: it
    affects library searching for -l options which follow it.  This option also implies --unresolved-symbols=report-all.  This option can be
    used with -shared.  Doing so means that a shared library is being created but that all of the library's external references must be
    resolved by pulling in entries from static libraries.