动态库和静态库

参考文档:
Linux Basics: Static Libraries vs. Dynamic Libraries
参考视频:
什么是静态库、动态链接库(共享库)
手把手教你linux下面静态库和动态库的制作

库的作用:实现代码的解耦、代码的重用、对外提供服务(export)

Libraries play their role at run time or compile time. 库在运行时或编译时发挥其作用。

Libraries have object files created by the “-c” gcc flag and end in “.o” by convention. They are the result of the output of the compiler and contain function definitions in binary form.
库具有由创建 “-c” gcc flag 的目标文件,并 “.o” 按约定结束。它们是编译器输出的结果,包含二进制形式的函数定义。

动态库具有“.so”命名约定,静态库具有“.a”。

动态库或共享库作为可执行文件之外的单独文件出现。
因此,它在运行时只需要库文件的一个副本。
在编译时,静态库被锁定在程序中。它包含文件的程序在编译时保存库文件的副本。

典型应用:exe调用.dll。打开大型软件的目录里面会有很多.dll文件,少量的.exe。
小软件会把所有的功能写在exe中,大软件会把代码解藕出来因为其中的很多代码是要重用的。

库的分类

库分为静态库和动态链接库(共享库)。
静态库:与exe打包成exe(一个文件)
windows就是.lib文件
linux就是.a文件

动态链接库(共享库):与exe是独立的文件。
windows就是.dll文件。
linux就是.so文件。

衍生技术
黑客 hook dll,代码替换或者代码劫持

关于Linux的动态库和静态库更详细的解释

When using a dynamic library, the programmer is referencing that library when it needs to at runtime. For instance, to access the string length function from the standard input/output header file — you can access it dynamically. It will find the program’s library reference at runtime because of the dynamic loader. It then loads that string length function into memory. Thus, the dynamic library accessibility must be readily available or it becomes powerless.
使用动态库时,程序员在运行时需要引用该库。例如,要从 standard input/output header file — 访问字符串长度函数,您可以动态访问它。由于动态加载器,它将在运行时找到程序的库引用。然后,它将该字符串长度函数加载到内存中。因此,动态库的可访问性必须随时可用,否则它将变得无能为力。


Advantages and Disadvantages of Dynamic Libraries
动态库的优缺点
1.It only needs one copy at runtime. It is dependent on the application and the library being closely available to each other.
它在运行时只需要一个副本。它依赖于应用程序和库彼此紧密可用。

2.Multiple running applications use the same library without the need of each file having its own copy.
多个正在运行的应用程序使用相同的库,而不需要每个文件都有自己的副本。

3.However, what if the dynamic library becomes corrupt? The executable file may not work because it lives outside of the executable and is vulnerable to breaking.
但是,如果动态库损坏怎么办?可执行文件可能无法工作,因为它位于可执行文件之外,并且容易被破坏。

4.They hold smaller files. 它们保存较小的文件。

5.Dynamic libraries are linked at run-time. It does not require recompilation and relinking when the programmer makes a change.
动态库在运行时链接。当程序员进行更改时,它不需要重新编译和重新链接。


At compile time, applications utilize static libraries. All the copies of the functions get placed into the application file because they are needed to run the process.
在编译时,应用程序使用静态库。函数的所有副本都放入应用程序文件中,因为运行进程需要它们。

Advantages and Disadvantages of Static Libraries
静态库的优缺点

1.Static libraries resist vulnerability because it lives inside the executable file.
静态库可以抵抗漏洞,因为它位于可执行文件中。

2.The speed at run-time occurs faster because its object code (binary) is in the executable file. Thus, calls made to the functions get executed quicker. Remember, the dynamic library lives outside of the executable, so calls would be made from the outside of the executable.
运行时的速度更快,因为它的目标代码(二进制)位于可执行文件中。因此,对函数的调用执行速度更快。请记住,动态库位于可执行文件之外,因此将从可执行文件的外部进行调用。

3.Changes made to the files and program require relinking and recompilation.
对文件和程序所做的更改需要重新链接和重新编译。

4.File size is much larger.
文件大小要大得多。

如何创建静态和动态库?

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


.c->.so所有的动态链接库
.o目标文件->.a静态库文件
目标文件:存放目标代

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃小酥肉的小波

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值