.o .a和.so文件的区别是什么?

时间:2021-12-04 13:26:32

I know .o are object files, .a are static libraries and .so are dynamic libraries? What is their physical significance? When can I use some and when not?

我知道。o是对象文件。a是静态库。动态库是吗?它们的物理意义是什么?我什么时候可以用,什么时候不能用?

3 个解决方案

#1


31  

.a is an "archive". Although an archive can contain any type of file, in the context of the GNU toolchain, it is a library of object files (other toolchains especially on WIndows use .lib for the same purpose, but the format of these is not typically a general purpose archive, and often specific to the toolchain). It is possible to extract individual object files from an archive which is essentially what the linker does when it uses the library.

。是一个“存档”。尽管存档可以包含任何类型的文件,在GNU工具链的背景下,这是一个图书馆的对象文件(其他工具链尤其是在WIndows上使用lib出于同样的目的,但这些通常不是一个通用的格式存档,通常特定于工具链)。可以从归档中提取单个对象文件,这基本上就是链接器在使用库时所做的事情。

.o is an object file. This is code that is compiled to machine code but not (typically) fully linked - it may have unresolved references to symbols defined in other object files (in a library or individually) generated by separate compilation. Object files contain meta-data to support linking with other modules, and optionally also for source-level symbolic debugging (in GDB for example). Other toolchains, again typically on Windows, use the extension .obj rather than .o.

.o是一个对象文件。这是编译成机器码的代码,而不是(通常)完全链接的代码——它可能对由单独编译生成的其他对象文件(在库中或单独的)中定义的符号有未解析的引用。对象文件包含元数据以支持与其他模块的链接,也可以选择用于源代码级的符号调试(例如在GDB中)。其他工具链,通常在Windows上,使用扩展名。obj而不是。o。

.so is a shared object library (or just shared library). This is dynamically linked to an executable when a program is launched rather then statically linked at build time. It allows smaller executables, and a single object library instance to be used by multiple executables. Operating system APIs are typically shared libraries, and they are often used also in GNU for licensing reasons to separate LGPL code from closed-source proprietary code for example (I am not a lawyer - I am making no claims regarding the legitimacy of this approach in any particular situation). Unlike .o or .a files, .so files used by an application must be available on the runtime system. Other systems (again typically Windows) use .dll (dynamic link library) for the same purpose.

所以是共享对象库(或者仅仅是共享库)。当程序启动时,动态地链接到可执行文件,而不是在构建时静态地链接。它允许更小的可执行文件,并允许多个可执行文件使用一个对象库实例。操作系统api通常是共享的库,而且出于许可的原因,在GNU中也经常使用它们来将LGPL代码和封闭源代码的私有代码分开(我不是律师——我对这种方法在任何特定情况下的合法性没有任何要求)。与.o或.a文件不同,所以应用程序使用的文件必须在运行时系统上可用。其他系统(通常是Windows)出于相同的目的使用.dll(动态链接库)。

It is perhaps useful to understand that .o files are linked before object code in .a files such that if a symbol resolution is satisfied by a .o file, any library implementation will not be linked - allowing you to essentially replace library implementations with your own, and also for library implementations to call user-defined code - for example a GUI framework might call an application entry-point.

它可能是有用的了解. o文件链接在对象代码。文件等,如果一个符号解析由. o文件,满足任何图书馆实现不会联系——允许您本质上是图书馆实现替换为你自己的,同时也为库实现调用用户定义的代码——例如一个GUI框架会调用应用程序入口点。

#2


2  

.so are shared library files. .a are static library files.

.a是静态库文件。

You can statically link to .a libraries and dynamically link and load at runtime .so files, provided you compile and link that way.

您可以静态地链接到。a库,并在运行时动态地链接和加载。

.o are object files (they get compiled from *.c files and can be linked to create executables, .a or .so libraries. Read more about it here

.o是对象文件(它们是从*编译的)。c文件,并可以链接创建可执行文件。a或。so库。在这里多读一点。

#3


1  

Static libraries are archives that contain the object code for the library, when linked into an application that code is compiled into the executable.

静态库是包含库的对象代码的归档文件,当链接到将代码编译为可执行文件的应用程序时。

Shared libraries are different in that they aren't compiled into the executable. Instead the dynamic linker searches some directories looking for the library(s) it needs, then loads that into memory. More then one executable can use the same shared library at the same time, thus reducing memory usage and executable size. However, there are then more files to distribute with the executable. You need to make sure that the library is installed onto the user's system somewhere where the linker can find it, static linking eliminates this problem but results in a larger executable file.

共享库的不同之处在于它们没有编译到可执行文件中。相反,动态链接器搜索一些目录来查找它需要的库,然后将其加载到内存中。更多的可执行文件可以同时使用相同的共享库,从而减少内存使用和可执行大小。但是,还有更多的文件要与可执行文件一起分发。您需要确保库被安装到用户的系统中,在链接器可以找到它的地方,静态链接消除了这个问题,但是产生了一个更大的可执行文件。

#1


31  

.a is an "archive". Although an archive can contain any type of file, in the context of the GNU toolchain, it is a library of object files (other toolchains especially on WIndows use .lib for the same purpose, but the format of these is not typically a general purpose archive, and often specific to the toolchain). It is possible to extract individual object files from an archive which is essentially what the linker does when it uses the library.

。是一个“存档”。尽管存档可以包含任何类型的文件,在GNU工具链的背景下,这是一个图书馆的对象文件(其他工具链尤其是在WIndows上使用lib出于同样的目的,但这些通常不是一个通用的格式存档,通常特定于工具链)。可以从归档中提取单个对象文件,这基本上就是链接器在使用库时所做的事情。

.o is an object file. This is code that is compiled to machine code but not (typically) fully linked - it may have unresolved references to symbols defined in other object files (in a library or individually) generated by separate compilation. Object files contain meta-data to support linking with other modules, and optionally also for source-level symbolic debugging (in GDB for example). Other toolchains, again typically on Windows, use the extension .obj rather than .o.

.o是一个对象文件。这是编译成机器码的代码,而不是(通常)完全链接的代码——它可能对由单独编译生成的其他对象文件(在库中或单独的)中定义的符号有未解析的引用。对象文件包含元数据以支持与其他模块的链接,也可以选择用于源代码级的符号调试(例如在GDB中)。其他工具链,通常在Windows上,使用扩展名。obj而不是。o。

.so is a shared object library (or just shared library). This is dynamically linked to an executable when a program is launched rather then statically linked at build time. It allows smaller executables, and a single object library instance to be used by multiple executables. Operating system APIs are typically shared libraries, and they are often used also in GNU for licensing reasons to separate LGPL code from closed-source proprietary code for example (I am not a lawyer - I am making no claims regarding the legitimacy of this approach in any particular situation). Unlike .o or .a files, .so files used by an application must be available on the runtime system. Other systems (again typically Windows) use .dll (dynamic link library) for the same purpose.

所以是共享对象库(或者仅仅是共享库)。当程序启动时,动态地链接到可执行文件,而不是在构建时静态地链接。它允许更小的可执行文件,并允许多个可执行文件使用一个对象库实例。操作系统api通常是共享的库,而且出于许可的原因,在GNU中也经常使用它们来将LGPL代码和封闭源代码的私有代码分开(我不是律师——我对这种方法在任何特定情况下的合法性没有任何要求)。与.o或.a文件不同,所以应用程序使用的文件必须在运行时系统上可用。其他系统(通常是Windows)出于相同的目的使用.dll(动态链接库)。

It is perhaps useful to understand that .o files are linked before object code in .a files such that if a symbol resolution is satisfied by a .o file, any library implementation will not be linked - allowing you to essentially replace library implementations with your own, and also for library implementations to call user-defined code - for example a GUI framework might call an application entry-point.

它可能是有用的了解. o文件链接在对象代码。文件等,如果一个符号解析由. o文件,满足任何图书馆实现不会联系——允许您本质上是图书馆实现替换为你自己的,同时也为库实现调用用户定义的代码——例如一个GUI框架会调用应用程序入口点。

#2


2  

.so are shared library files. .a are static library files.

.a是静态库文件。

You can statically link to .a libraries and dynamically link and load at runtime .so files, provided you compile and link that way.

您可以静态地链接到。a库,并在运行时动态地链接和加载。

.o are object files (they get compiled from *.c files and can be linked to create executables, .a or .so libraries. Read more about it here

.o是对象文件(它们是从*编译的)。c文件,并可以链接创建可执行文件。a或。so库。在这里多读一点。

#3


1  

Static libraries are archives that contain the object code for the library, when linked into an application that code is compiled into the executable.

静态库是包含库的对象代码的归档文件,当链接到将代码编译为可执行文件的应用程序时。

Shared libraries are different in that they aren't compiled into the executable. Instead the dynamic linker searches some directories looking for the library(s) it needs, then loads that into memory. More then one executable can use the same shared library at the same time, thus reducing memory usage and executable size. However, there are then more files to distribute with the executable. You need to make sure that the library is installed onto the user's system somewhere where the linker can find it, static linking eliminates this problem but results in a larger executable file.

共享库的不同之处在于它们没有编译到可执行文件中。相反,动态链接器搜索一些目录来查找它需要的库,然后将其加载到内存中。更多的可执行文件可以同时使用相同的共享库,从而减少内存使用和可执行大小。但是,还有更多的文件要与可执行文件一起分发。您需要确保库被安装到用户的系统中,在链接器可以找到它的地方,静态链接消除了这个问题,但是产生了一个更大的可执行文件。