What are the differences between static link libraries and dynamic link libraries under Linux

08-01-2023

This article mainly explains the differences between the static link library and the dynamic link library under Linux. The content of the explanation in the article is simple and clear, easy to learn and understand. Please follow the editor's ideas and study together. What are the differences between static link libraries and dynamic link libraries under learning linux!

Differences: 1. The suffix of the dynamic library is .so, and the suffix of the static library is .a. 2. If the static function library is changed, the program must be recompiled; while the change of the dynamic function library does not affect the program. 3. Compared with the static library, the dynamic library is not compiled into the target code when compiling, and the corresponding function in the function library is called when the user's program executes the relevant function, so the executable file generated by the dynamic function library smaller.

1. The basic concept of the library:

There are a large number of libraries under both the windows platform and the linux platform. Essentially a library is a binary form of executable code that can be loaded into memory by the operating system for execution. Due to the different nature of windows and linux, the binaries of the two libraries are not compatible. In layman's terms, it is to package the object files of these commonly used functions together, and provide the interface of the corresponding functions, which is convenient for programmers to use. When using a function, only the header file corresponding to the package is required. According to the way the library is used, it can be divided into dynamic library and static library, and the corresponding suffixes are different under different platforms.

Under WINDOWS: .dll suffix is ​​dynamic library, .lib suffix is ​​static library;

Under LINUX: .so suffix is ​​dynamic library, .a suffix is ​​a static library.

Second, static library and static link

<1>Static library:

< p>The static library can be simply regarded as a collection of a set of object files, that is, many object files are compressed and packaged to form files. For example, in our daily programming, if we need to use the printf function, we need to include the stdio.h library file, and when using strlen, we need to include the string.h library file, but if we directly compile the corresponding function source code to form .o If the files are directly provided to us, it will cause great inconvenience to our management and use, so we can use the ar compression program to compress these target files together to form a libx.a static library file.

Note: Static library naming format: lib + "library name + .a (suffix) Example: libadd.a is a static library called add

<2> Static link:

For a static library, when the program is compiled and linked, the code of the library is linked into the executable file, and the static library is no longer needed when the program is running. In the process of use, we only need to link the library and the compiled file of our program together to form an executable file.

Through an example to understand how to compile and link our own header files and code at the same time, and finally generate an executable file:

/main. c/ #include #include "add.h" int main() { int ret = add(3, 4); printf("3 + 4 = %d\n", ret); return 0; } /add.c/ #include "add.h" int add( int x, int y) { return x + y; } /add.h/ #pragma once #include int add( int x, int y); /Makefile/ main : main.c libadd.a gcc main.c -L . -ladd -o main //-L is the specified path. It is -l+ library name in the current directory, the compiler can find the library file named add in the specified directory libadd.a : gcc -c add.c -o add.o //ar -rc packages multiple compiled files into a static library file ar -rc libadd.a add.o .PHONY:clean clean: rm main libadd.a

Output screenshot after make:

<3>Disadvantages:

1. Waste of memory and disk space: The static link method is very serious for the waste of memory and disk space of the computer.

If the size of a static library in c language is 1MB, and there are 100 library files that need to be used in the system, if static linking is used, 100M of memory will be wasted. There are more. For example, the figure below: Both program 1 and program 2 need to use Lib.o. If static linking is used, then two copies of this file will be stored in the physical memory.

2. Update trouble:

For example, a program has 20 modules, and each module is only 1MB, so every time you update any module, the user has to HeavyNewly downloaded 20M program.

3. Dynamic library and dynamic link

<1>Dynamic library:

The program is in The code of the dynamic library is only linked at runtime, and the code of the library is shared by multiple programs. An executable file linked with a dynamic library only contains a table of the entry addresses of the functions it uses, rather than the entire machine code of the object file where the external function is located.

Note: Dynamic library naming format: lib + "library name + .so (suffix) Example: libadd.so is a dynamic library called add

<2> Dynamic link:

Due to the waste of memory and the difficulty of module update due to static linking, dynamic linking is proposed. The basic implementation idea is to split the program into relatively independent parts according to the modules, and in the program They are linked together to form a complete program at runtime, instead of linking all program modules into a single executable file like static linking. So dynamic linking is to postpone the linking process until runtime.< /p>

Similarly, if there are three files of program 1, program 2, and Lib.o, both program 1 and program 2 need to use the Lib.o file when executing. When running program 1, the system will first load Program 1, when it finds that the Lib.o file is needed, it is also loaded into the memory, and then loads Program 2. When it is found that the Lib.o file is also needed, there is no need to reload Lib.o, just program 2 Just link with the Lib.o file, and there is always only one Lib.o file in the memory.

The dynamic library and dynamic link examples still use the above code, and the output is the same, the only need What is changed is the Makefile.

/Makefile/ main : main.c libadd.so gcc main.c -L . -ladd -o main libadd.so : gcc -fPIC -shared add.c -o libadd.so //-shared indicates that the output result is a shared library type -fPIC indicates that an odd number of address-independent code is used to produce the output file .PHONY:clean clean: rm main libadd.so

  • When we generate an executable file, we can use the ldd command to view the dynamic library that the executable file depends on.

  • As mentioned earlier, the suffixes of the library files under windows and Linux are different, and the more fundamental reason is that the two file formatsall different. You can view the file type of the dynamic library under Linux through file a dynamic library is actually in ELF format. ELF dynamic link files are called dynamic shared objects (DSO, Dynamic Shared Objects), referred to as shared objects; under windows, dynamic link files are called dynamic link libraries (Dynamic Linking Library), which is the full name of the .dll file suffix.

<3>Advantages:

  • ① There is no doubt that it saves memory;

    • p>

    • ②Reduce the swapping in and out of physical pages;

    • ③When upgrading a module, theoretically only need to The old target file can be overwritten. The new version of the target file will be automatically loaded into the memory and linked;

    • ④The program can dynamically select and load various program modules during runtime to realize program expansion.

    Fourth, the difference between static library and dynamic library

    1. Static library< /p>

    The name of this kind of library is generally libxxx.a; the file compiled by using the static function library is relatively large, because all the data of the entire function library will be integrated into the target code, and its advantages are obvious. That is, the compiled execution program does not need external function library support, because all used functions have been compiled into it. Of course, this will also become his shortcoming, because if the static function library changes, then your program must be recompiled.

    2. Dynamic library

    The name of this type of library is generally libxxx.so; compared to the static function library, the dynamic function library does not It is not compiled into the target code, and the corresponding function in the function library is called when your program executes to the relevant function, so the executable file generated by the dynamic function library is relatively small. Since the function library is not integrated into your program, but is dynamically applied for and called when the program is running, the corresponding library must be provided in the running environment of the program. The change of the dynamic function library does not affect your program, so the upgrade of the dynamic function library is more convenient.

    Thank you for reading. The above is the difference between the static link library and the dynamic link library under linux. I have a deeper understanding of the issue of which ones are different, and the specific usage needs to be verified by everyone in practice. This is Yisu Cloud, the editor will push more articles about relevant knowledge points for you, welcome to pay attention!

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us