Why are static and dynamic linkable libraries different?

前端 未结 6 1288
独厮守ぢ
独厮守ぢ 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:26

    The code in an object file isn't linked. It contains implicit references to external functions that have not yet been resolved.

    When object files are linked to create a DLL, the linker looks through all those external references and finds other libraries (static or dynamic) that can satisfy them. A reference to a name in a static library is resolved by including the body of that function (or whatever) into the DLL. If it refers to a dynamic library, the name of both the DLL and the referenced function are included in the DLL.

    Ultimately, there's no reason this would have to be the case. In theory, you could write the loader to do all of this every time you loaded a file. It's basically just an optimization: the linker does the relatively slow parts of the job. References to DLLs are left, but they're resolved to the point that it's fairly fast for the loader to find and load the target file (if necessary) and resolve the referenced functions. When the linker is doing its job, it does a lot more by scanning through long lists of definitions to find the ones you care about, which is quite a bit slower.

提交回复
热议问题