Why are static and dynamic linkable libraries different?

前端 未结 6 1342
独厮守ぢ
独厮守ぢ 2021-01-31 03:41

If both of them contain compiled code, why can\'t we load the \"static\" files at runtime and why can\'t we link with the dynamic libraries at compile time? Why is there a need

6条回答
  •  野性不改
    2021-01-31 04:27

    Note: The following answer is not platform agnostic, but specific to ELF-based systems and some other similar ones. Someone else can fill in details for other systems.

    What is a static library?

    A static library is a collection of *.o files in an archive. Each file can contain references to undefined symbols which must be resolved by the linker, for example, your library might have a reference to printf. The library doesn't provide any indication about where printf will be found, it's expected that the linker will find it in one of the other libraries it's asked to link in.

    Suppose your library contains the following code:

    read_png.o
    write_png.o
    read_jpg.o
    write_jpg.o
    resize_image.o
    handle_error.o
    

    If an application only uses read_png and write_png, then the other pieces of code won't get loaded into the executable (except handle_error, which is called from read_png and write_png).

    We can't load a static library at runtime because:

    • The linker doesn't know where to find external objects, e.g., printf.

    • It would be slow. Dynamic libraries are optimized for fast loading.

    • Static libraries have no concept of namespaces. I can't define my own handle_error because it would conflict with the library's definition.

    What is a dynamic library?

    A dynamic library, on ELF systems, is the same type of object as an executable. It also exports more symbols, an executable only needs to export _start. Dynamic libraries are optimized so the whole thing can be mapped directly into memory.

    If you have a call to printf in your dynamic library, there are some additional requirements beyond the requirements for static libraries:

    • You have to specify which library has printf.

    • You have to call the function in a special way that lets the linker insert the address for printf. In a static library, the linker can just modify your code and insert the address directly, but this is not possible with shared libraries.

    We don't want to use dynamic libraries to link statically because:

    • We can't link in only part of a dynamic library. Even if our executable never calls read_jpg, it gets included because dynamic libraries are all-or-nothing.

    • The extra overhead for function calls is wasteful, even if it is small.

    Summary

    Compilation looks something like this:

    Source  ==compile==>  Object  ==link==>  Executable / Shared Library
    

    A static library is an archive full of objects that haven't been linked yet. There's a lot of work left to be done.

    A shared library is a linked final product, ready to be loaded into memory.

    Static libraries were invented first. If both were invented at the same time, it's possible they would be much more similar.

提交回复
热议问题