当前位置:网站首页>Dynamic and static libraries

Dynamic and static libraries

2022-06-13 05:01:00 Clown clown clown

Static and dynamic libraries

The names of the libraries are all removed lib And the suffix is the library name . That's important

Static library

Static library in linux Next is .a file . When compiling the link, the program links the code of the library to the executable file . The static library will no longer be needed when the program runs .

System perspective :
A static library is a code area that is added to the process address space during the linking process .
Icon :
 Insert picture description here

benefits : If after compiling the link , Deleting the static library can also run . This program is portable .

Disadvantage : Make the program super big , The reason is also simple , It is because a lot of code has been added .

Dynamic library

The dynamic library is in linux So .so Final document . When the program is running, it links the code of the dynamic library , Multiple programs share code that uses the library .
Dynamic library linking occurs when the program is running . The shared area is mapped to the dynamic library in the physical memory through the page table , So you can use it directly .

System perspective :
 Insert picture description here

Dynamic links

  1. Because the dynamic library is only linked to the code of the dynamic library when it is running , It violates our previous understanding , Link first and then run .
    Therefore, we always say that the link of preprocessing compilation assembly link refers to the process of loading the dynamic library code from the hard disk into the memory , It is called dynamic link (dynamic linking)

  2. The link of a dynamic library is not all the code of an external dynamic library , It's what the program needs , Link the corresponding function .

  3. Dynamic libraries can be used by multiple processes

Build a library

A library consists of a header file and a library file , Where the library file is .o file

Generate static libraries

Give the code first :
establish lib Library code :
 Insert picture description here
Execute static library
 Insert picture description here
step :
1. To generate a static library, you must first create .o file . therefore gcc -o Options can be
2. a pile .o The file is not good-looking , Help him pack it into a file . Tools for static library packaging ar, The options are -rc(replace and create)
3. Create a directory lib, Put header files and library files inside . Like this :
 Insert picture description here
4. This library can be linked and used . When the program wants to use this static library , Because the system cannot find where this library is , So there are several ways to help the system find this static library .

The first one is : Options

  1. Options -I + route ------- Used to find header files
  2. Options -L + route ------ Used to find the .a File directory
  3. Options -l + file name ------ Used to find what you need .a file

The second kind : Add to the directory specified by the system

/usr/lib
/usr/local/lib

Generate dynamic library

Give the code first
 Insert picture description here
Execute dynamic library code :
First import environment variables LD_LIBRARY_PATH = Dynamic library .so The path of
 Insert picture description here
And then execute .

Otherwise you will report an error :
 Insert picture description here

Why is this so ?

This is linked to the dynamic library in Run time link It matters .
While the program is running , compiler gcc I already know where this library is , But the system doesn't know .

The problem is coming. : Why does the system need to know where the library is ?
The reason is that dynamic link just loads the dynamic library from the hard disk into the memory , However, the system also helps the process , Let the shared area in the process address space map to the memory with the dynamic library through the page table .

Therefore, the function of importing environment variables is to let the system find the location of the dynamic library , Then change the mapping relationship of the page table , Let the program find the location of the dynamic library .

原网站

版权声明
本文为[Clown clown clown]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280516403454.html