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 load开发者_开发技巧ed on-demand at runtime, why is it a fatal error for a library to be missing at compile-time?
- 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.
- 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.
- 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.)
精彩评论