当前位置:网站首页>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.cMaking 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.c2. 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.soThe 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 clac2. 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 /.bashrcor
vim ~/.bashrcAfter that, you need to update the environment variables
source ~/.bashrc(3) Changes at the system level Linux environment variable
sudo vim /etc/profileAfter 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.confLoad 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
边栏推荐
- An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!
- [Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)
- Create ECS using API shortcut
- GEE:基于 MCD64A1 的 GlobFire 日常火灾数据集
- SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
- 个人买场内基金选择什么证券公司开户好,更安全
- JS基础1-JS引入与运算符
- JS基础10
- Wireshark数据抓包分析之FTP协议
- 数据库系列:有什么办法对数据库的业务表进行无缝升级
猜你喜欢

fastposter v2.8.4 发布 电商海报生成器

Ruoyi integrated building block report (NICE)

Yann Lecun's new paper: the road to building automatic agents

Katalon框架测试web(二十)自定义关键字以及上传弹窗操作

利用soapUI获取freemarker的ftl文件模板

Debug debugging in katalon

Solve the problem of reading package listsdonebuilding dependency treereading state informationdone

MySQL(一)

爱可可AI前沿推介(6.28)

sentinel
随机推荐
毕业季 新的开始
datetime与logging模块
Download and install mysql5.7 for windows 10
Mongo数据库
数据库系列:有什么办法对数据库的业务表进行无缝升级
Hystrix deployment
JS基础4
Internet of things application case of wireless module transparent transmission technology
Metersphere implements UI automation elements that are not clickable (partially occluded)
Katalon当中的使用循环for、while和if...else、break、continue
[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)
JS基础8
[monkey] Introduction to monkey test
Compression and decompression
GCC简介
毕业季,给初入社会的你一些建议
Guangzhou Customs supports the stable supply of food, agricultural products, traditional Chinese medicine and other civilian and biological resources to Hong Kong
MarkDown——基本使用语法
ProCAST有限元铸造工艺模拟软件
DlhSoft Kanban Library for WPF