共享库和静态库

发布于 2024-11-17 03:54:22 字数 316 浏览 3 评论 0原文

谁能解释一下 gcc(makefile) 中的共享库和静态库之间有什么区别

我读到静态库是独立的代码,但它增加了可执行文件的大小

,但是共享库它动态链接函数,但它不增加可执行文件的大小

我无法理解这两者之间的区别。

谁能告诉我什么时候应该创建静态库以及什么时候应该创建共享库。

他们说共享库是位置相关代码,

位置相关代码是什么意思?

如果共享库不会增加代码,如果静态库会增加代码大小 那么我们就可以直接使用共享库了。

但是为什么我们也有静态库,它的真正用途是什么?

请帮助我

Can anyone explain me what is the difference between a shared and static library in gcc(makefile)

I read that static library is independent code but it increases the size of your exectuable file

But whereas the shared library it links the functions dynamically and it does not increase the size of your executable file

I cannot understand the difference between these two.

Can anyone tell me when i should create a static library and when i should create a shared library.

They say shared library is a position dependent code

What do we mean by position dependent code?

If shared library does not increase the code and if static library increases the code size
then we can just go for shared library right.

But why do we have static library too what is the real use of it.

please help me guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱绻入梦 2024-11-24 03:54:22

当你用gcc编译代码时,它可以直接生成目标文件,并且可以将多个目标文件链接在一起生成可执行文件。

静态库只是目标文件的集合(通常带有索引),链接器在创建可执行文件时可以使用它。

仅将目标文件链接在一起和与静态库链接之间的区别在于,对于库,链接器只会从中选取绝对需要的目标文件,而与目标文件链接时,链接器被迫获取所有文件。

动态库又不同了——非常不同。它是目标文件的集合,但这次它是链接过程的输出,其所有内部链接都已解析。

与动态库链接意味着链接器仅解析最终可执行文件中的符号,但在链接时不会添加库中的任何目标代码。

运行与动态库链接的可执行文件意味着在运行时某些特殊软件必须要求操作系统将指定的动态库加载到内存中(这就是为什么动态库必须由链接器)通常在程序到达 main() 之前。

该可执行文件可以是磁盘上的一个非常小的文件,但是当加载到内存中并加载动态库之后,总内存映像可能会非常大,因为整个库都将被加载。

我相信情况是这样的,如果有几个不同的文件程序需要相同的动态库,操作系统可以为每个程序使用虚拟内存来加载库,但这只使用物理内存的一份副本 - 在某些情况下这可以节省很大的空间。

如果您发布需要动态库的可执行文件,那么您可能很幸运,该文件可能已经在用户的计算机上。如果没有,您也必须安装它。但是,如果您在库中发现错误,那么只需发布动态库的新版本即可修复这些错误。

使用静态库构建的可执行文件已准备好运行,但修复错误将涉及重新发布整个可执行文件。

总之:

               object files  static library  dynamic library
produced by    compiler      librarian       linker
exe            large         smaller         smallest
ram x 1 copy   large         smaller         largest
ram x n copies large         smaller         smallest
dep on files   independent   independent     dependant
upgrade lib    no            no              yes

When you compile code with gcc, it can directly produce object files and a number of object files can be linked together to produce an executable file.

A static library is simply a collection of object files (usually with an index) which can be used by a linker when it creates an executable.

The difference between just linking object files together and linking with a static library is that with the library, the linker will only pick the object files from it that are absolutely needed, whereas linking with object files, the linker is forced to take all of the files.

A dynamic library is different again - very different. It is a collection of object files, but this time its the output of a linking process with all of its internal links already resolved.

Linking with a dynamic library means that the linker only resolves symbols in the final executable, but adds none of the object code from the library at link time.

Running an executable linked with dynamic libraries means that at run-time some special software has to ask the operating system to load a specified dynamic library into memory (this is why a dynamic library has to be created by a linker) usually before the program gets to main().

This executable can be a very small file on disk, but when loaded into memory and after loading dynamic libraries the total memory image can be quite large as the whole of the library will have been loaded in.

I believe its the case that if several different programs need the same dynamic library, the operating system can use virtual memory for each program to load the library, but this only uses one copy of physical memory - which can be a big saving in some cases.

If you ship an executable which needs a dynamic library, then you might be lucky and the file might already be on the user's machine. If not, you would have to install that as well. However, if you find bugs in your library, then just shipping a new version of your dynamic library will fix the bugs.

An executable built with static libraries comes ready to run, but fixing bugs would involve reshipping the whole executable.

In Summary:

               object files  static library  dynamic library
produced by    compiler      librarian       linker
exe            large         smaller         smallest
ram x 1 copy   large         smaller         largest
ram x n copies large         smaller         smallest
dep on files   independent   independent     dependant
upgrade lib    no            no              yes
对你的占有欲 2024-11-24 03:54:22
  • 您可能想要创建一个没有库依赖项的单个可执行文件。
  • 据我所知,静态库应该更快一些。

缺点:链接共享库不仅会增加可执行文件的大小,还会随每个可执行文件一起加载。如果您在多个可执行文件中使用该库,那么它将多次出现在内存中。

位置相关代码意味着在创建库时,不知道库在内存中的确切位置,因此只能使用相对内存寻址(例如:从此指令跳转到 +50 字节并从那里执行)

  • you might want to create a single executable without the dependencies of the libraries.
  • as far as I know static libraries should be somewhat faster.

Drawbacks: linking shared libraries is not only increasing the executable size it also gets loaded with each executable. If you use the library in multiple executables then it will be in memory multiple times.

Position dependent code means that at the creation of the library the exact location in memory of the library isn't known so only relative memory addressing is available (eg: jump to +50 bytes from this instruction and follow execution from there)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文