当前位置:网站首页>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
边栏推荐
- 静态库的制作和使用
- JS foundation 4
- Create ECS using API shortcut
- 阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
- DlhSoft Kanban Library for WPF
- Oracle 日期格式化异常:无效数字
- 【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
- 一种跳板机的实现思路
- Internet of things application case of wireless module transparent transmission technology
- 【实操】Appium Settings app is not running after 5000ms
猜你喜欢
随机推荐
Mysql安装配置以及解决重装Mysql时忘记root password问题
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
获取系统当前日期
Debug debugging in katalon
Transactions proof in appliedzkp zkevm (10)
Wireshark数据抓包分析之FTP协议
Fastposter v2.8.4 release e-commerce poster generator
windows 10下载安装mysql5.7
Remote connection of raspberry pie in VNC viewer mode without display
An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!
Katalon framework tests a web page operation example code
静态库的制作和使用
【Qt】connect 语法参考实现
【实战】1364- 实现一个完美的移动端瀑布流组件(附源码)
JS基础3
JS基础4
树莓派无需显示屏的VNC Viewer方式的远程连接
使用 Calendar 计算时间
选择哪种编程语言,会吸引优秀的人才?
JSON模块、hashlib、base64








