当前位置:网站首页>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 .
边栏推荐
- Handwritten promise and its method, with detailed notes
- Common skills in embedded programming
- Clause 28: understanding reference folding
- C language learning log 1.19
- C # get all callable methods of WebService interface [webmethod]
- Opencv image storage and reading
- Luogu p1012 guess
- Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
- 2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
- The games that you've tasted
猜你喜欢
利用Javeswingjdbc基於mvc設計系統
Elliptic curve encryption
C language learning log 1.24
约瑟夫问题
Section 2 - branch and loop statements
Robot pose description and coordinate transformation
Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display
无限循环滚动代码阿里巴巴国际站店铺装修代码底图滚动黑色半透明显示效果自定义内容装修代码全屏显示
Chapter 13 abstraction: address space
Analysis of scoped attribute principle and depth action selector
随机推荐
On switch() case statement in C language
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
Four methods for judging JS data types and user-defined methods
Elliptic curve encryption
LeetCode第297场周赛(20220612)
Section 6 - pointers
Kaggle 时间序列教程
C language learning log 1.22
Embedded hardware: electronic components (1) resistance capacitance inductance
C language learning log 10.19
Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display
Chapter 18 pagination: Introduction
Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
C language learning log 10.8
Avantages de win8.1 et win10
Chapter 13 abstraction: address space
C language learning log 12.14
C language learning log 2.6
JS to realize the conversion between string and array and an interview question
Chapter 14 introduction: memory operation API