While it is commonplace to combine multiple object files in a library, it is possible (at least in Linux) to combine multiple object files into another object file.
虽然在库中组合多个目标文件是很常见的,但是(至少在Linux中)可以将多个目标文件组合到另一个目标文件中。
(See combine two GCC compiled .o object files into a third .o file)
(请参阅将两个GCC编译的.o对象文件合并到第三个.o文件中)
As there are downsides to using libraries instead of just combined object files:
由于使用库而不仅仅是组合对象文件存在缺点:
1: It's easier to work with only one type of file (object) when linking, especially if all files do the same thing.
1:链接时只使用一种类型的文件(对象)更容易,特别是如果所有文件都做同样的事情。
2: When linking (At least in GCC), libraries (by default) need to be ordered and can't handle cyclic dependencies.
2:链接时(至少在GCC中),库(默认情况下)需要排序,不能处理循环依赖。
I want to know what advantages there are to libraries (apart from the catch 22 that they're used lots).
我想知道图书馆有什么优势(除了捕获22他们使用了很多)。
After searching for a while, the only explanation I get seems to be that single libraries are better than multiple object files.
在搜索了一段时间之后,我得到的唯一解释似乎是单个库比多个目标文件更好。
4 个解决方案
#1
10
While it depends on the linker being used, object files are being included in the final binary in their entirety. So, if you combine several object files into one object file, then the resulting (combined) object file is included in the resultant binary.
虽然它取决于所使用的链接器,但目标文件完整地包含在最终二进制文件中。因此,如果将多个目标文件合并为一个目标文件,则生成的(组合)目标文件将包含在生成的二进制文件中。
In contrast, a library is just that, a library of object files. The linker will only pull the object files from the library that it needs to resolve all the symbolic links. If an object file (in the library) is not needed, then it is not included int the binary.
相比之下,库只是一个目标文件库。链接器将仅从库中提取它需要解析所有符号链接的目标文件。如果不需要对象文件(在库中),则它不包含在二进制文件中。
#2
5
Generally library is better since linker can optimize out unused .o files in library.
通常库更好,因为链接器可以优化库中未使用的.o文件。
Combining the files somehow has some advantages too:
以某种方式组合文件也有一些优点:
- If you combine all your source files into one then that tremendously increases compilation speed.
- Sometimes in C++ the linker may optimize out .o file that you did not want to be optimized out. For example when you need side-effects of constructors of objects defined there but not used in any other translation unit.
如果将所有源文件合并为一个,那么极大地提高了编译速度。
有时在C ++中,链接器可能会优化您不希望优化的.o文件。例如,当您需要在那里定义但未在任何其他翻译单元中使用的对象的构造函数的副作用时。
#3
1
If you use object files, then all the code in the object file is placed in your application. If you use libraries, then only the required code is.
如果使用目标文件,则目标文件中的所有代码都将放在应用程序中。如果使用库,则只需要所需的代码。
#4
0
One reason is that objects in a .a
library will only be pulled in to satisfy undefined symbol references - so if you want to allow the possibility for the calling application to define a symbol, or use a default definition in the "library" code, it's possible to do this with a real library but not with a single .o
file.
一个原因是.a库中的对象只会被引入以满足未定义的符号引用 - 因此,如果您希望允许调用应用程序定义符号,或者在“库”代码中使用默认定义,可以使用真正的库来执行此操作,但不能使用单个.o文件。
#1
10
While it depends on the linker being used, object files are being included in the final binary in their entirety. So, if you combine several object files into one object file, then the resulting (combined) object file is included in the resultant binary.
虽然它取决于所使用的链接器,但目标文件完整地包含在最终二进制文件中。因此,如果将多个目标文件合并为一个目标文件,则生成的(组合)目标文件将包含在生成的二进制文件中。
In contrast, a library is just that, a library of object files. The linker will only pull the object files from the library that it needs to resolve all the symbolic links. If an object file (in the library) is not needed, then it is not included int the binary.
相比之下,库只是一个目标文件库。链接器将仅从库中提取它需要解析所有符号链接的目标文件。如果不需要对象文件(在库中),则它不包含在二进制文件中。
#2
5
Generally library is better since linker can optimize out unused .o files in library.
通常库更好,因为链接器可以优化库中未使用的.o文件。
Combining the files somehow has some advantages too:
以某种方式组合文件也有一些优点:
- If you combine all your source files into one then that tremendously increases compilation speed.
- Sometimes in C++ the linker may optimize out .o file that you did not want to be optimized out. For example when you need side-effects of constructors of objects defined there but not used in any other translation unit.
如果将所有源文件合并为一个,那么极大地提高了编译速度。
有时在C ++中,链接器可能会优化您不希望优化的.o文件。例如,当您需要在那里定义但未在任何其他翻译单元中使用的对象的构造函数的副作用时。
#3
1
If you use object files, then all the code in the object file is placed in your application. If you use libraries, then only the required code is.
如果使用目标文件,则目标文件中的所有代码都将放在应用程序中。如果使用库,则只需要所需的代码。
#4
0
One reason is that objects in a .a
library will only be pulled in to satisfy undefined symbol references - so if you want to allow the possibility for the calling application to define a symbol, or use a default definition in the "library" code, it's possible to do this with a real library but not with a single .o
file.
一个原因是.a库中的对象只会被引入以满足未定义的符号引用 - 因此,如果您希望允许调用应用程序定义符号,或者在“库”代码中使用默认定义,可以使用真正的库来执行此操作,但不能使用单个.o文件。