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

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

What is the difference between a .o file and a .lib file?

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

3 个解决方案

#1


8  

A .LIB file is a collection of .OBJ files concatenated together with an index. There should be no difference in how the linker treats either.

lib文件是. obj文件的集合,并与一个索引连接在一起。链接器的处理方式也应该没有区别。

Quoted from here:

引用:

What is the difference between .LIB and .OBJ files? (Visual Studio C++)

.LIB和.OBJ文件的区别是什么?Visual Studio c++)(

#2


9  

Conceptually, a compilation unit (the unit of code in a source file/object file) is either linked entirely or not at all. While some implementations, with significant levels of cooperation between the compiler and linker, are able to remove unused code from object files at link time, it doesn't change the issue that including 2 compilation units with conflicting symbol names in a program is an error.

从概念上讲,编译单元(源文件/对象文件中的代码单元)要么完全链接,要么完全不链接。虽然有些实现在编译器和链接器之间具有显著的协作水平,但是可以在链接时从对象文件中删除未使用的代码,但它不会改变包含两个编译单元的问题,在程序中有冲突的符号名是一个错误。

As a practical example, suppose your library has two functions foo and bar and they're in an object file together. If I want to use bar, but my program already has an external symbol named foo, I'm stuck with an error. Even if or how the implementation might be able to resolve this problem for me, the code is still incorrect.

作为一个实际的例子,假设您的库有两个函数foo和bar,它们在一个对象文件中。如果我想使用bar,但是我的程序已经有一个名为foo的外部符号,我就会被错误卡住。即使实现可能能够解决这个问题,代码仍然是不正确的。

On the other hand, if I have a library file containing two separate object files, one with foo and the other with bar, only the one containing bar will get pulled into my program.

另一方面,如果我有一个包含两个单独的对象文件的库文件,一个是foo,另一个是bar,只有一个包含bar的文件会被拉入我的程序。

When writing libraries, you should avoid including multiple functions in the same object file unless it's essential that they be used together. Doing so will bloat up applications which link your library (statically) and increase the likelihood of symbol conflicts. Personally I prefer erring on the side of separate files when there's a doubt - it's even useful to put foo_create and foo_free in separate files if the latter is nontrivial so that short one-off programs that don't need to call foo_free can avoid pulling in the code for deep freeing (and possibly even avoid pulling in the implementation of free itself).

在编写库时,您应该避免在同一个对象文件中包含多个函数,除非它们必须在一起使用。这样做会增加链接库(静态)的应用程序,增加符号冲突的可能性。我个人更喜欢犯错的单独的文件有一个疑问——甚至有用foo_create foo_free在单独的文件,如果后者是重要的,这样短的一次性项目,不需要在代码中调用foo_free可以避免拉深释放(甚至可能避免拉在*的实现本身)。

#3


5  

They are actually quite different, specially with older linkers.

它们实际上是完全不同的,特别是老的连接器。

The .o (or .obj) files are object files, they contain the output of the compiler generated code. It is still in an intermediate format, for example, most references are still unresolved. Usually there is a one to one mapping between the source file and the object file.

.o(或.obj)文件是对象文件,它们包含编译器生成的代码的输出。它仍然是一种中间格式,例如,大多数引用仍然没有解决。通常在源文件和对象文件之间有一个映射。

The .a (or .lib) files are archives, also known as library, and are a set of object files.

a(或.lib)文件是存档,也称为库,是一组对象文件。

All operating systems have tools that allow you to add/remove/list object files to library files.

所有的操作系统都有允许您将/删除/列出对象文件到库文件的工具。

Another difference, specially with older linkers is how the files are dealt with, when linking them. Some linked will place the complete object file into the final binary, regardless of what is actually being used, while they will only extract the useful information out of library files.

另一个不同之处是,在链接这些文件时,这些文件是如何处理的。有些链接会将完整的对象文件放入最终的二进制文件中,而不管实际使用的是什么,而它们只会从库文件中提取有用的信息。

Nowadays most linkers are smart enough to remove all stuff that is not being used.

现在大多数的链接器都很聪明,可以删除所有不被使用的东西。

#1


8  

A .LIB file is a collection of .OBJ files concatenated together with an index. There should be no difference in how the linker treats either.

lib文件是. obj文件的集合,并与一个索引连接在一起。链接器的处理方式也应该没有区别。

Quoted from here:

引用:

What is the difference between .LIB and .OBJ files? (Visual Studio C++)

.LIB和.OBJ文件的区别是什么?Visual Studio c++)(

#2


9  

Conceptually, a compilation unit (the unit of code in a source file/object file) is either linked entirely or not at all. While some implementations, with significant levels of cooperation between the compiler and linker, are able to remove unused code from object files at link time, it doesn't change the issue that including 2 compilation units with conflicting symbol names in a program is an error.

从概念上讲,编译单元(源文件/对象文件中的代码单元)要么完全链接,要么完全不链接。虽然有些实现在编译器和链接器之间具有显著的协作水平,但是可以在链接时从对象文件中删除未使用的代码,但它不会改变包含两个编译单元的问题,在程序中有冲突的符号名是一个错误。

As a practical example, suppose your library has two functions foo and bar and they're in an object file together. If I want to use bar, but my program already has an external symbol named foo, I'm stuck with an error. Even if or how the implementation might be able to resolve this problem for me, the code is still incorrect.

作为一个实际的例子,假设您的库有两个函数foo和bar,它们在一个对象文件中。如果我想使用bar,但是我的程序已经有一个名为foo的外部符号,我就会被错误卡住。即使实现可能能够解决这个问题,代码仍然是不正确的。

On the other hand, if I have a library file containing two separate object files, one with foo and the other with bar, only the one containing bar will get pulled into my program.

另一方面,如果我有一个包含两个单独的对象文件的库文件,一个是foo,另一个是bar,只有一个包含bar的文件会被拉入我的程序。

When writing libraries, you should avoid including multiple functions in the same object file unless it's essential that they be used together. Doing so will bloat up applications which link your library (statically) and increase the likelihood of symbol conflicts. Personally I prefer erring on the side of separate files when there's a doubt - it's even useful to put foo_create and foo_free in separate files if the latter is nontrivial so that short one-off programs that don't need to call foo_free can avoid pulling in the code for deep freeing (and possibly even avoid pulling in the implementation of free itself).

在编写库时,您应该避免在同一个对象文件中包含多个函数,除非它们必须在一起使用。这样做会增加链接库(静态)的应用程序,增加符号冲突的可能性。我个人更喜欢犯错的单独的文件有一个疑问——甚至有用foo_create foo_free在单独的文件,如果后者是重要的,这样短的一次性项目,不需要在代码中调用foo_free可以避免拉深释放(甚至可能避免拉在*的实现本身)。

#3


5  

They are actually quite different, specially with older linkers.

它们实际上是完全不同的,特别是老的连接器。

The .o (or .obj) files are object files, they contain the output of the compiler generated code. It is still in an intermediate format, for example, most references are still unresolved. Usually there is a one to one mapping between the source file and the object file.

.o(或.obj)文件是对象文件,它们包含编译器生成的代码的输出。它仍然是一种中间格式,例如,大多数引用仍然没有解决。通常在源文件和对象文件之间有一个映射。

The .a (or .lib) files are archives, also known as library, and are a set of object files.

a(或.lib)文件是存档,也称为库,是一组对象文件。

All operating systems have tools that allow you to add/remove/list object files to library files.

所有的操作系统都有允许您将/删除/列出对象文件到库文件的工具。

Another difference, specially with older linkers is how the files are dealt with, when linking them. Some linked will place the complete object file into the final binary, regardless of what is actually being used, while they will only extract the useful information out of library files.

另一个不同之处是,在链接这些文件时,这些文件是如何处理的。有些链接会将完整的对象文件放入最终的二进制文件中,而不管实际使用的是什么,而它们只会从库文件中提取有用的信息。

Nowadays most linkers are smart enough to remove all stuff that is not being used.

现在大多数的链接器都很聪明,可以删除所有不被使用的东西。