当前位置:网站首页>Making and using of dynamic library (shared library)
Making and using of dynamic library (shared library)
2022-06-28 11:01:00 【MoyangCitta】
add.c div.c mult.c sub.c
Making dynamic library
1. First, all the files that need to generate the library file “.c” File compiled into “.o” file
gcc -c -fpic/fPIC add.c div.c sub.c mult.c
2. Use gcc The command will all the files compiled in the first step “.o” File generation dynamic library
gcc -shared add.o div.o mult.o sub.o -o ../lib/libcalc.so
The use of dynamic libraries
1. Generate executable files , among “-L” Specify the storage path of the library ,“-l” Specify the name of the library ( Unwanted “lib” And extension “.so”), If there are dynamic and static libraries with the same name in the storage path of the library ,gcc Dynamic library is used by default
gcc main.c -o main -I ./include -L ./lib -l clac
2. Execution method :
After the program starts , The dynamic library will be dynamically loaded into memory , adopt ldd(list dynamic dependencies) Command to check dynamic library dependencies
You can see that the path of the shared library is not specified
When the system loads executable code , Be able to know the name of the library it depends on , But we still need to know the absolute way
path . At this time, the dynamic loader of the system is required to obtain the absolute path . about elf Format executable program , yes
from ld-linux.so To complete , It searches for elf Of documents DT_RPATH paragraph ——> environment variable
LD_LIBRARY_PATH ——> /etc/ld.so.cache File list ——> /lib/, /usr/lib
The directory finds the library file and loads it into memory .
There are five ways to specify the path to a shared library
(1) Temporary modification Linux environment variable LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/ The directory where the dynamic library is located ( Replace with your own )
(2) Changes at the user level Linux environment variable
There are... In the user directory .bashrc( This file is hidden ) file , Add the above statement on the last line
vim /home/ User name /.bashrc
or
vim ~/.bashrc
After that, you need to update the environment variables
source ~/.bashrc
(3) Changes at the system level Linux environment variable
sudo vim /etc/profile
After opening the document , Fill in the above sentence on the last line , Update environment variables at the same time
source /etc/profile
(4) By modifying the /etc/ld.so.cache File list To use dynamic libraries
sudo vim /etc/ld.so.conf
Load the path into the document
to update
sudo ldconfig
(5) Put dynamic inventory in /lib/ perhaps /usr/lib Under the table of contents ( Not recommended , It may be confused with the library file of the system itself , There are some unnecessary problems )
Static library and dynamic library comparison
The methods used by the static library when linking are included in the final generated executable program , The shared library does not contain , Only do
Mark , When running a program , To dynamically load .
1. Advantages and disadvantages of static library :
- advantage : It is packaged into the application and loaded quickly ; The publisher does not need to provide a static library , Transplant convenience
- shortcoming : Consume system resources , Waste of memory ; to update 、 Deploy 、 Publishing trouble
2. Advantages and disadvantages of dynamic library :
- advantage : Inter process resource sharing can be realized ( Shared library ); to update 、 Deploy 、 Easy to release ; You can control when dynamic libraries are loaded
- shortcoming : Loading speed is slower than static library ; When publishing programs, you need to provide dependent dynamic libraries
边栏推荐
猜你喜欢
Information hidden in the trend chart of Hong Kong London gold market
AGCO AI frontier promotion (6.28)
MySQL(一)
Move command
Fastposter v2.8.4 release e-commerce poster generator
【实操】Appium Settings app is not running after 5000ms
Mysql database overview and installation process
静态库的制作和使用
Katalon当中的output使用方法
动态库(共享库)的制作和使用
随机推荐
获取系统当前日期
Katalon framework tests a web page operation example code
Gee: mcd64a1 based globfire daily fire data set
MySQL common commands for viewing database performance
Summary of spatial temporal time series prediction modeling methods
NFT卡牌链游系统开发dapp搭建技术详情
压缩解压
MySQL(二)
Metersphere uses JS to refresh the current page
Secretary of the Ukrainian national security and National Defense Commission: will carry out precision strikes against targets in Russia
Debug debugging in katalon
Realization of a springboard machine
使用API快捷创建ECS
Threads and thread pools
Compareto() and equals() methods of BigDecimal class
静态库的制作和使用
JS基础5
JS foundation 6
线程和线程池
个人买场内基金选择什么证券公司开户好,更安全