Location>code7788 >text

Linux_Dynamic_and_Static_Libraries(I)

Popularity:567 ℃/2024-10-02 23:22:47

1. Definition of dynamic and static libraries

Dynamic libraries (.so): Dynamic libraries are shared libraries that are not embedded in the target file after compilation, and the code of dynamic libraries is linked only when the program is running, and can be shared by multiple programs, usually ending with .so.

Static libraries (.a): A static library is a set of target files (.o files) packaged into a single archive. At compile time, the linker will be required to copy the target file code to the final generated executable file, the program will no longer need to run the static library, usually ending in .

Naming rules for libraries:

lib + library name + suffix (.s/.so, etc.)

The library name of eg: is c, which stands for c dynamic library.

The library name of eg: is c, which stands for c static library.

2. The process of running a dynamic and static program:

static executable program

Load: Because the executable program contains all the methods of the header file and the main function, it is loaded into memory relatively large.

The program code in memory corresponds to the body code segment of the process address space through the page table

If other processes also need to use the C library, the C library is not loaded in memory, the C library is in the static executable program, so other processes have to reload the C library, wasting memory space

dynamic executable program (DEP)

Load: In addition to the code being loaded into memory, the methods it uses are also added to memory. So loading is relatively small

Code in memory is mapped to the body code segment of the process address space, and methods used are mapped to the shared area between the stack and heap areas

If other processes also need to use the C libraries, simply adjust the mapping of the other processes to the C libraries already loaded in memory.

Save memory space by not reloading

3. View dynamic libraries (ldd) that the executable program depends on

lddIt is mainly used to view the dependencies of dynamic libraries, whereas static libraries are linked directly into the executable at compile time, so they usually do not show the dependencies.

4. Advantages and disadvantages of static and dynamic libraries

static library

Drawbacks:

1). Takes up a lot of space when loaded into memory

2). Multiple processes using the same library can lead to wasted memory resources

Pros:

1). Library-independent, library is already linked in the executable program, it will still run after deleting the library

2). The program is library-independent and more cross-platform

dynamic library (computing)

Drawbacks:

1). Depends on the library, if the library is removed after the executable is generated, the executable cannot be run.

Pros:

1). Saving memory space resources

Compare and contrast the difference in memory occupied by a dynamic executable program versus a static executable program: