当前位置:网站首页>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 .
边栏推荐
- What is the difference between ROM, ram and flash? SRAM、DRAM、PROM、EPROM、EEPROM
- Clause 27: alternatives to overloading with familiar universal reference types
- Time display of the 12th Blue Bridge Cup
- Must know must know -c language keywords
- C language learning log 2.6
- Autumn wind, dust, youth
- CMB's written test -- data analysis
- C language learning log 12.25
- How to understand JS expressions and JS statements
- C language learning log 10.11
猜你喜欢
SQL notes
Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
QT realizes message sending and file transmission between client and server
Win8.1和Win10各自的优势
Advantages of win8.1 and win10
CMB's written test -- data analysis
Cesium:cesiumlab makes image slices and loads slices
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
Advanced C language - Section 1 - data storage
使用EasyDarwin+FFmpeg实现rtsp推流
随机推荐
How to understand JS expressions and JS statements
Advanced C - Section 3 - character functions and string functions
Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
C language learning log 2.19
Chinese trumpet creeper
Little C's Notepad
QT client development -- driver loading problem of connecting to MySQL database
QT direction key to move focus
Construction problem of D Xiaohong
[LeetCode]-二分查找
String()和toString()方法得区别
C language learning log 10.11
Differences between string() and tostring() methods
Hidden implementation and decoupling, knowing Pimpl mode
QT interface rendering style
Win8.1和Win10各自的優勢
Regular expressions in QT
PostgreSQL Guide: inside exploration (Chapter 10 basic backup and point in time recovery) - Notes
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
C language learning log 1.17