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

如果动态库比静态库有更大的优势,为什么还有很多静态库呢?什么时候我应该尝试将代码构建到。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

当前回答

文件类型分解

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

.a文件是静态库。后缀代表“存档”,因为它们实际上只是原始.o目标文件的存档(用ar命令创建——tar的前身,现在只用于创建库)。

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

静态vs动态

静态

利:用户总是使用你在应用程序中测试过的库版本,所以不应该有任何令人惊讶的兼容性问题。 弊:如果一个库中的问题被修复了,你需要重新分发你的应用程序来利用它。但是,除非它是一个用户可能自行更新的库,否则无论如何您都可能需要这样做。

动态

Pro: Your process's memory footprint is smaller, because the memory used for the library is amortized among all the processes using the library. 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. 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. Con: Link-Time Optimization is generally not possible, so there could possibly be efficiency implications in high-performance applications. See the Wikipedia discussion of WPO and LTO.

动态库对于libc这样的系统库特别有用。这些库通常需要包含依赖于特定操作系统和版本的代码,因为内核接口已经发生了变化。如果将程序与静态系统库链接,则该程序将只在该库版本所针对的操作系统版本上运行。但是如果您使用动态库,它将自动拾取安装在您运行的系统上的库。

其他回答

On top of the given answer, also to say that static library is like a collection of object files. When there is a call to a function or anything else in static library, the linker searches for the referenced item in the library and add that object file that defines the referenced item to the executable. On the other hand, shared library is like a single big object file that is composed of all other object files. Therefore, if you call a symbol in shared lib, then all object file is referenced in the executable.

文件类型分解

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

.a文件是静态库。后缀代表“存档”,因为它们实际上只是原始.o目标文件的存档(用ar命令创建——tar的前身,现在只用于创建库)。

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

静态vs动态

静态

利:用户总是使用你在应用程序中测试过的库版本,所以不应该有任何令人惊讶的兼容性问题。 弊:如果一个库中的问题被修复了,你需要重新分发你的应用程序来利用它。但是,除非它是一个用户可能自行更新的库,否则无论如何您都可能需要这样做。

动态

Pro: Your process's memory footprint is smaller, because the memory used for the library is amortized among all the processes using the library. 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. 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. Con: Link-Time Optimization is generally not possible, so there could possibly be efficiency implications in high-performance applications. See the Wikipedia discussion of WPO and LTO.

动态库对于libc这样的系统库特别有用。这些库通常需要包含依赖于特定操作系统和版本的代码,因为内核接口已经发生了变化。如果将程序与静态系统库链接,则该程序将只在该库版本所针对的操作系统版本上运行。但是如果您使用动态库,它将自动拾取安装在您运行的系统上的库。