共享库和静态链接库之间有什么区别,以及如何实现它们? [英] What's the difference between Shared libraries and Statically linked libraries and how to implement them?
问题描述
我要开发一个库,这两者之间有什么区别,以及如何实现呢?实现我的意思是,如何使用它们作为库?
I'm going to develop a library, what are the differences between these two and how to implement it? By implement I mean, how to use them as a library?
推荐答案
(我回答关注Linux )
共享图书馆(部分)(通过某些 mmap
调用)。
Shared libraries are (partly) shared (by some mmap
call). Static libraries are copied into each executable.
共享库应为位置无关代码(因此使用 g ++ -fPIC -O -Wall
....)编译它们。有关详细信息,请参见此答案。静态库不需要是PIC。
Shared libraries should be position independent code (so compile them using g++ -fPIC -O -Wall
....). See this answer for details. Static libraries do not need to be PIC.
共享库可以与其他库链接(当构建共享库时)
Shared libraries can be linked (when building a shared library) to other libraries.
共享库可以动态链接为 plugins 使用 dlopen(3)。在C ++中注意名称修改(因此请使用 externC code>)。请参阅 C ++ dlopen mini-howto
Shared libraries can be dynamically linked as plugins using dlopen(3). Beware of name mangling in C++ (so use extern "C"
). See C++ dlopen mini-howto
先阅读计划库教程,然后阅读 Levine的书:链接器和加载程序 ,然后 Drepper的论文:如何写共享库
Read first the Program Library HowTo, then Levine's book: Linkers and Loaders, then Drepper's paper: How To Write Shared Libraries
让您的图书馆成为共享库和免费软件,例如 LGPL 许可(强烈建议共享库链接)。
I would invite you to make your library a shared library and free software, e.g. under LGPL license (which strongly suggests shared library linking).
这篇关于共享库和静态链接库之间有什么区别,以及如何实现它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!