I know an .so
file 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 .a
and .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?
如果动态libs比静态的有很大的优势,为什么仍然有很多静态库?
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 ld
command by now)
我还想知道加载库的底层机制(两种),以及在某个地方使用时如何调用lib中的一段代码。我应该研究内核的哪一部分?为了了解进程如何运行,我应该知道什么相关的Linux命令/实用程序?(我现在只知道ld的命令)
When should I try to build code into .so
or .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
1 个解决方案
#1
221
.so
files 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.
所以文件是动态库。后缀表示“共享对象”,因为与库链接的所有应用程序都使用相同的文件,而不是在结果可执行文件中复制。
.a
files are static libraries. The suffix stands for "archive", because they're actually just an archive (made with the ar
command -- a predecessor of tar
that's now just used for making libraries) of the original .o object files.
一个文件是静态库。后缀表示“archive”,因为它们实际上只是原始.o对象文件的存档(使用ar命令——tar的前身,现在只用于创建库)。
.la
files are text files used by the GNU "libtools" package to 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.
静态con:如果一个问题被固定在一个库中,你需要重新分配你的应用程序来利用它。然而,除非它是一个用户可能自己更新的库,否则无论如何您都可能需要这样做。
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.
动态pro:库可以在运行时按需加载;这对插件很有好处,所以您不必在编译和安装软件时选择要使用的插件。可以动态地添加新的插件。
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这样的系统库特别有用。这些库通常需要包含依赖于特定操作系统和版本的代码,因为内核接口已经发生了变化。如果您将一个程序与一个静态系统库连接起来,那么它将只运行在这个库版本的操作系统上。但是如果您使用动态库,它将自动选择安装在您运行的系统上的库。
#1
221
.so
files 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.
所以文件是动态库。后缀表示“共享对象”,因为与库链接的所有应用程序都使用相同的文件,而不是在结果可执行文件中复制。
.a
files are static libraries. The suffix stands for "archive", because they're actually just an archive (made with the ar
command -- a predecessor of tar
that's now just used for making libraries) of the original .o object files.
一个文件是静态库。后缀表示“archive”,因为它们实际上只是原始.o对象文件的存档(使用ar命令——tar的前身,现在只用于创建库)。
.la
files are text files used by the GNU "libtools" package to 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.
静态con:如果一个问题被固定在一个库中,你需要重新分配你的应用程序来利用它。然而,除非它是一个用户可能自己更新的库,否则无论如何您都可能需要这样做。
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.
动态pro:库可以在运行时按需加载;这对插件很有好处,所以您不必在编译和安装软件时选择要使用的插件。可以动态地添加新的插件。
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这样的系统库特别有用。这些库通常需要包含依赖于特定操作系统和版本的代码,因为内核接口已经发生了变化。如果您将一个程序与一个静态系统库连接起来,那么它将只运行在这个库版本的操作系统上。但是如果您使用动态库,它将自动选择安装在您运行的系统上的库。