当前位置:网站首页>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
边栏推荐
猜你喜欢

建立自己的网站(11)

Threads and thread pools

How to use output in katalon
![[monkey] Introduction to monkey test](/img/70/5a7152d0b6b77df7f9d6ad0e09e000.png)
[monkey] Introduction to monkey test

Wireless communication module fixed-point transmission - point to multipoint specific transmission application

Debug debugging in katalon

静态库的制作和使用

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

【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)

sentinel
随机推荐
[Agora] get an Agora_ Example usage of refptr object
Summary of characteristics of five wireless transmission protocols of Internet of things
MytipartFile与File的相互转换
Basic 02: variable, remember the mobile number of the object
Installing MySQL database (CentOS) in Linux source code
动态库(共享库)的制作和使用
How to use K-line diagram for technical analysis
GDB简介
Oracle 日期格式化异常:无效数字
合约量化系统开发(搭建讲解)丨合约量化系统开发(源码解析及现成案例)
什么是DAPP系统发展与解析理解
Redis数据库
使用 Calendar 计算时间
爱可可AI前沿推介(6.28)
JS基础4
MySQL(二)
BigDecimal 类的 compareTo() 和 equals()方法
Katalon框架测试一个web页面操作实例代码
Remote connection of raspberry pie in VNC viewer mode without display
Download and install mysql5.7 for windows 10