gcclinkerdynamic-linking

Why is a Missing Shared Library Fatal?


g++ -o Test Test.cpp -lTest
/usr/bin/ld: cannot find -lTest
collect2: ld returned 1 exit status

If symbols from shared/dynamic libraries are loaded on-demand at runtime, why is it a fatal error for a library to be missing at compile-time?


Solution

    1. Inter-library dependencies can be difficult to deal with at run time; the compile-time check lets the linker detect and sometimes resolve dependency loops and add any additional library dependencies to the runtime link list.
    2. Would you prefer to find out that you misspelled a symbol name at compile/link time, or at runtime? Most people prefer the former, so the linker checks that all symbols are resolved instead of deferring it to the runtime loader; this of course requires all the libraries to be present.
    3. Suppose a dynamic library requires a static library to be present which otherwise wouldn't be linked in. On most platforms you can't (or can't sanely or safely) load static libraries at runtime, so the linker needs to deal with these at link time. (This is actually a sub-case of #1.)