Technology Sharing

How does the linker work? The difference between static linking and dynamic linking. How to create and use dynamic link libraries?

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

The role of the linker in program development is crucial. It is responsible for integrating multiple target files and library files into an executable file. Before we delve into the working principle of the linker, the difference between static linking and dynamic linking, and how to create and use dynamic link libraries, let's first outline the basic functions of the linker.

How the linker works

Linker It is a tool responsible for combining one or more target files and library files into an executable file. Its main functions include:

  1. Symbol resolution: Identify and process all symbols (names of functions and variables) in the program, ensuring that each symbol has a unique definition. For symbols that are referenced but not defined (external symbols), the linker looks for definitions in the provided libraries or other object files.

  2. reset: Adjust the code and data addresses in each module to the final memory address. Relocation includes address correction in the code and position adjustment of the data segment to ensure that all references point to the correct memory location.

  3. Merge segments: Merge segments of the same type (such as code segments, data segments, etc.) from different target files into one continuous segment.

  4. Processing Library: Link the library code needed by the program with the object file. The linker can handle two types of libraries: static libraries and dynamic libraries.

  5. Generate executable file: The final output is an executable file that can be run on the operating system.

The difference between static linking and dynamic linking

Static Linking andDynamic Linking There are two working modes of the linker, each with different characteristics and usage scenarios.

Static Linking
  • concept: In static linking, the library code is copied at compile time and embedded into each executable file that uses it. In this way, the generated executable file contains all the required code and does not depend on external library files.

  • advantage

    • Strong independence: The generated executable file contains all dependencies and does not require additional library files at runtime.
    • Good compatibility: The runtime does not depend on the library version installed in the system, and will not encounter the "library version conflict" problem.
  • shortcoming

    • Large file size: Each executable file contains the complete library code, which increases the file size.
    • Update trouble: If the library is updated, all programs using the library need to be recompiled.
  • Static library extension

    • Windows: .lib
    • Unix/Linux: .a
Dynamic Linking
  • concept: In dynamic linking, the library code is loaded at runtime and is not embedded into the executable file. The executable file only contains a reference to the library and the library code is loaded by the operating system at runtime.

  • advantage

    • Small file size: The executable file does not contain library code, only references to the library.
    • Easy to update: Updating the library does not require recompiling the program, just replacing the library file.
    • High memory efficiency: multiple programs can share the same memory instance of the library file, reducing memory usage.
  • shortcoming

    • Strong dependency: The executable file needs to be able to find and load the correct version of the library file when it runs.
    • Compatibility issues: Mismatched library file versions may cause program failure.
  • Dynamic library extension

    • Windows: .dll(Dynamic-Link Library)
    • Unix/Linux: .so(Shared Object)

Creating and using dynamic link libraries

Creating a dynamic link library

The method of creating a dynamic link library is slightly different on different operating systems. Here are some common steps and commands:

Creating a dynamic link library on Linux
  1. Writing library code

    Create a C source file containing the functions we want to put in the dynamic library.

    // example.cpp
    #include