why do we need the shared library during compile time

前端 未结 2 459
长发绾君心
长发绾君心 2021-02-05 07:08

Why we need the presence of the shared library during the compile time of my executable? My reasoning is that since shared library is not included into my executable and is load

2条回答
  •  死守一世寂寞
    2021-02-05 07:28

    Nothing is needed at compile time, because C has a notion of separate compilation of translation units. But once all the different sources have been compiled, it is time to link everything together. The notion of shared library is not present in the standard but is it now a common thing, so here is how a common linker proceeds:

    • it looks in all compiled modules for identifiers with external linkage either defined or only declared
    • it looks in libraries (both static and dynamic) for identifiers already used and not defined. It then links the modules from static libraries, and stores references from dynamic libraries. But at least on Unix-likes, it needs to access the shared library for potential required (declared and not defined) identifiers in order to make sure they are already defined or can be found in other linked libraries be them static or dynamic

    This produces the executable file. Then at load time, the dynamic loader knows all the dynamic modules that are required and loads them in memory (if they are not already there) along with the actual executable and builds a (virtual) memory map

提交回复
热议问题