Linux .so、.la 和 .a 库文件有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12237282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:14:25  来源:igfitidea点击:

What's the difference between .so, .la and .a library files?

linuxmemory-managementstatic-librariesldshared-libraries

提问by hugemeow

I know an .sofile is a kind of dynamic library (lots of threads can share such libraries so there is no need to have more than one copy of it in memory). But what is the difference between .aand .la? Are these all static libraries?

我知道.so文件是一种动态库(很多线程可以共享这样的库,因此内存中不需要有多个副本)。但是.a和之间有什么区别.la?这些都是静态库吗?

If dynamic libs have big advantages over static ones, why there are still lots of static libraries?

如果动态库比静态库有很大的优势,为什么还有很多静态库?

I also want to know the underlying mechanism to load libraries (both kinds) and how a piece of code in a lib is invoked when it is used somewhere. Which part of the kernel should I study? And what related Linux command/utility should I know in order to know how a process is running? (I only know the ldcommand by now)

我还想知道加载库(两种)的底层机制以及在某处使用时如何调用库中的一段代码。我应该研究内核的哪个部分?为了知道进程是如何运行的,我应该知道哪些相关的 Linux 命令/实用程序?(我现在只知道ld命令)

When should I try to build code into .soor .a? Which one is better?

我应该什么时候尝试将代码构建到.so或 中.a?哪一个更好?

[mirror@home ins_openvpn]$ ls lib/openvpn/plugins/ -l
total 96
-rw-r--r-- 1 mirror mirror 22892 Sep  2 23:25 openvpn-plugin-auth-pam.a
-rwxr-xr-x 1 mirror mirror   931 Sep  2 23:25 openvpn-plugin-auth-pam.la
-rwxr-xr-x 1 mirror mirror 23621 Sep  2 23:25 openvpn-plugin-auth-pam.so
-rw-r--r-- 1 mirror mirror 17228 Sep  2 23:25 openvpn-plugin-down-root.a
-rwxr-xr-x 1 mirror mirror   932 Sep  2 23:25 openvpn-plugin-down-root.la
-rwxr-xr-x 1 mirror mirror 18805 Sep  2 23:25 openvpn-plugin-down-root.so

回答by Barmar

.sofiles are dynamic libraries. The suffix stands for "shared object", because all the applications that are linked with the library use the same file, rather than making a copy in the resulting executable.

.so文件是动态库。后缀代表“共享对象”,因为与库链接的所有应用程序都使用相同的文件,而不是在生成的可执行文件中进行复制。

.afiles are static libraries. The suffix stands for "archive", because they're actually just an archive (made with the arcommand -- a predecessor of tarthat's now just used for making libraries) of the original .o object files.

.a文件是静态库。后缀代表“归档”,因为它们实际上只是原始 .o 对象文件的归档(使用ar命令制作——它的前身tar现在仅用于制作库)。

.lafiles are text files used by the GNU "libtools" packageto describe the files that make up the corresponding library. You can find more information about them in this question: What are libtool's .la file for?

.la文件是GNU“libtools”包用来描述组成相应库的文件的文本文件。您可以在以下问题中找到有关它们的更多信息:libtool 的 .la 文件用于什么?

Static and dynamic libraries each have pros and cons.

静态库和动态库各有优缺点。

Static pro: The user always uses the version of the library that you've tested with your application, so there shouldn't be any surprising compatibility problems.

静态专业:用户始终使用您在应用程序中测试过的库版本,因此不应该有任何令人惊讶的兼容性问题。

Static con: If a problem is fixed in a library, you need to redistribute your application to take advantage of it. However, unless it's a library that users are likely to update on their own, you'd might need to do this anyway.

静态骗局:如果问题在库中得到解决,您需要重新分发您的应用程序以利用它。但是,除非它是用户可能自行更新的库,否则您可能需要这样做。

Dynamic pro: Your process's memory footprint is smaller, because the memory used for the library is amortized among all the processes using the library.

Dynamic pro:您的进程的内存占用更小,因为用于库的内存在使用该库的所有进程之间摊销。

Dynamic pro: Libraries can be loaded on demand at run time; this is good for plugins, so you don't have to choose the plugins to be used when compiling and installing the software. New plugins can be added on the fly.

Dynamic pro:可以在运行时按需加载库;这对插件有好处,因此您在编译和安装软件时不必选择要使用的插件。可以即时添加新插件。

Dynamic con: The library might not exist on the system where someone is trying to install the application, or they might have a version that's not compatible with the application. To mitigate this, the application package might need to include a copy of the library, so it can install it if necessary. This is also often mitigated by package managers, which can download and install any necessary dependencies.

动态骗局:该库可能不存在于某人尝试安装该应用程序的系统上,或者他们的版本可能与该应用程序不兼容。为了缓解这种情况,应用程序包可能需要包含库的副本,以便在必要时安装它。这通常也可以通过包管理器来缓解,包管理器可以下载和安装任何必要的依赖项。

Dynamic libraries are especially useful for system libraries, like libc. These libraries often need to include code that's dependent on the specific OS and version, because kernel interfaces have changed. If you link a program with a static system library, it will only run on the version of the OS that this library version was written for. But if you use a dynamic library, it will automatically pick up the library that's installed on the system you run on.

动态库对于系统库特别有用,例如libc. 这些库通常需要包含依赖于特定操作系统和版本的代码,因为内核接口已更改。如果将程序与静态系统库链接,则它只会在为该库版本编写的操作系统版本上运行。但是如果您使用动态库,它会自动选择安装在您运行的系统上的库。