What should be the order in which libraries are linked in GCC?

The linker searches from left to right. While doing so it encounters unresolved symbols which it keeps track of. If a library resolves the unresolved symbol, it takes the object files of that library to resolve the symbol.

Dependencies of static libraries from each other work in the same way. Libraries requiring symbols from other library come before libraries resolving the symbol. If you have cyclic dependencies, you must enclose the libraries having a cyclic dependency in parenthesis. For example, if you have libraries a and b that are cyclically dependent −

$ g++ hello.cpp -L. -( -la -lb -)

Newer linkers are smarter and can keep track of the functions used by preceding static libraries, permanently tossing out those functions that are not used from its lookup tables. So if you link a static library very early, the methods from that library are no longer available to static libraries later on the link line.

Published on 28-Feb-2018 11:32:58