In Linux, there are two types of function libraries:
1. Static Library
The link of the static library is completed in the program compilation phase.
Each program that needs to call the static database has a copy of the database,
Therefore, the absolute position of the static library in the program is determined in the compilation phase;
Because there are multiple copies of static databases in multiple programs, the program size is increased.
2. Dynamic Link Library
The dynamic link library will not be copied to the program during compilation.
It is used to link the function library to the executable program only when the library is called during the running stage of the program.
Because it is dynamically loaded, its absolute position in the program is uncertain;
However, the relative positions of each function in the function library are determined during compilation.
The dynamic link library must be supported by the operating system. Fortunately, the current operating system basically supports the dynamic link library.
Because the program is running and loaded into the database in real time as needed,
Therefore, if the function library cannot be found or a library function is missing in the function library, execution errors may occur.
For dynamic link libraries, such errors can only be detected when the program is running.
If it is a static library, these errors can be found early in the compilation phase.
Benefits of Using Dynamic Link Libraries include:
1. Truly realizes the separation of the function library and program. The library function provider does not have to care about other parts of the program.
They only need to provide available dynamic function libraries.
2. Make the upgrade of the program easy
If the dynamic link library is changed, you do not have to compile the entire executable program, making program development easier.
You only need to compile the dynamic link library, copy it to the original storage location of the dynamic link library, and re-run the program.
Next, we will learn how to apply the dynamic link library feature to facilitate program development.
The procedure is as follows:
1. determine the location of the system where the function library is located. You can use the find command to find
Sh-3.2 # find/-name "libtest. so"
2. copy the compiled. so file to the location found in step 1,
Sh-3.2 # mount-o sync-t vfat/dev/sda1/mnt/
The-o parameter is used to specify that the partition is written in sync mode;
The-t parameter specifies the file system type of the partition to be mounted.
3. Run the program again. Then the program loads a new dynamic link library and runs it.
4. The ldd command can be used to view the dynamic link libraries that the program depends on.
Sh-# ldd./test_main
Libc. so.6 =>/lib/libc. so.6 (0x40113000)
/Lib/ld-linux.so.3 (0x40022000)
Sh -#
For ldd information, refer to the following article,
If the program is large, how can we determine which dynamic link library a module or function belongs to (which is possible in a large system )?
Assuming that the function is located in a dynamic link library, you can use objdump to disassemble the dynamic link library and then find the specified function in the result,
Sh-3.2 # objdump-d./libtest. so | grep function
Neither readelf nor nm can accurately locate the. so file in which the function is located,
Sh-3.2 # readelf-s./libtest. so | grep function
Sh-3.2 # nm./libtest. so | grep function
The reason is that this symbol can be found both in the library where the function is located and in the library where the function is called.
Control Linux dynamic link library export Function
How to create and use dynamic link libraries in Ubuntu
Linux dynamic link library export Function
Use Dynamic Link Library in Linux
Notice on developing Makefile in Linux (in-depth understanding of Dynamic Link Library)