当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Section 7 - structures
How to lay copper in AD (aluminum designer)
[LeetCode]-二分查找
MySQL8.0.13安装教程(有图)
Robot pose description and coordinate transformation
C language learning log 11.7
Section 8 - Practical commissioning techniques
CMB written test graphical reasoning
Kaggle time series tutorial
Advanced C - Section 2 - pointers
随机推荐
What is the difference between ROM, ram and flash? SRAM、DRAM、PROM、EPROM、EEPROM
Luogu p1036 number selection
C language learning log 10.19
Spread your wings and soar
rainbow
Explain the role of key attribute in V-for
Section 6 - pointers
Mind mapping series - Database
QT using layout manager is invalid or abnormal
lookup
Four methods for judging JS data types and user-defined methods
Advanced C language - Section 1 - data storage
QT realizes message sending and file transmission between client and server
Binary search and binary answer
Clause 48: understand template metaprogramming
shell变量学习笔记
Bm1z002fj-evk-001 startup evaluation
RuoYi-Cloud启动教程(手把手图文)
Wang Dao Chapter II linear table exercises
Sort (internal sort) + external sort