当前位置:网站首页>Dynamic and static libraries
Dynamic and static libraries
2022-06-13 05:01:00 【Clown clown clown】
List of articles
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 :
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 :
Dynamic links
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)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 .
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 :
Execute static library 
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 :
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
- Options -I + route ------- Used to find header files
- Options -L + route ------ Used to find the .a File directory
- 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 
Execute dynamic library code :
First import environment variables LD_LIBRARY_PATH = Dynamic library .so The path of 
And then execute .
Otherwise you will report an error :
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 .
边栏推荐
- CMB written test graphical reasoning
- Section 2 - branch and loop statements
- 详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
- Clause 33: decltype is used for auto & type formal parameters, with std:: forward
- Chapter 2 process management
- C # get all callable methods of WebService interface [webmethod]
- Analysis on the usage, response and global delivery of provide/inject
- Explain the differences and usage scenarios between created and mounted
- BM1Z002FJ-EVK-001开机测评
- shell变量学习笔记
猜你喜欢

Time display of the 12th Blue Bridge Cup

Leetcode game 297 (20220612)

Analysis on the usage, response and global delivery of provide/inject

Advanced C - Section 3 - character functions and string functions

QT direction key to move focus

C language learning log 10.19

Solution to sudden font change in word document editing

PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes

Shell built-in string substitution

Advantages of win8.1 and win10
随机推荐
String()和toString()方法得区别
lookup
Clause 34: lambda is preferred over std:: bind
1.2 differences caused by keywords
QT client development -- driver loading problem of connecting to MySQL database
Simple SR: best buddy Gans for highly detailed image super resolution
RuoYi-Cloud启动教程(手把手图文)
The games that you've tasted
17.6 unique_lock详解
Regular expressions in QT
What is the saturate operation in opencv
RMQ、LCA
Red Treasure Book Reading Notes (continuously updated)
QT interface rendering style
Construction problem of D Xiaohong
Clause 28: understanding reference folding
All blog navigation
C language learning log 12.5
Little C's Notepad
C language learning log 1.22