C++ .a .o 和 .lo 文件的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5895649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between .a .o and .lo file
提问by Raj
What is the difference between .a
.o
and .lo
file in C?
C 中的.a
.o
和.lo
文件和有什么区别?
采纳答案by DumbCoder
The '.lo' file is a library object, which may be built into a shared library, and the '.o' file is a standard object file
'.lo' 文件是一个库对象,它可能被内置到共享库中,而 '.o' 文件是一个标准的对象文件
The .lo file is the libtool object, which Libtool uses to determine what object file may be built into a shared library
.lo 文件是 libtool 对象,Libtool 使用它来确定哪些对象文件可以构建到共享库中。
回答by uDude
Difference Between .o, .a, .lo and .so.
.o、.a、.lo 和 .so 之间的区别。
Executive Summary
执行摘要
- .ois typically a non-PIC object file emitted by the compiler (before linker stage) When linked with an exe, the code will be included in the executable -- we bind at link time.
- .ais typically an archive library containing one or more .ofiles [non-PIC]. When linked with an exe, the particular "*.o" files in the archive will be inserted into the executable.
- .lois generally a "library object" that contains PIC code whether manually compiled with gcc -fPICor using libtool.
- .sofiles are "shared object" files. They contains PIC objects.
- .o通常是编译器发出的非 PIC 目标文件(在链接器阶段之前)当与 exe 链接时,代码将包含在可执行文件中——我们在链接时绑定。
- .a通常是包含一个或多个.o文件 [非 PIC]的存档库。当与 exe 链接时,存档中的特定“*.o”文件将被插入到可执行文件中。
- .lo通常是包含 PIC 代码的“库对象”,无论是使用gcc -fPIC手动编译还是使用libtool。
- .so文件是“共享对象”文件。它们包含 PIC 对象。
Note:
笔记:
- If you need static executables then use ".o" and ".a" files.
- If you need/want dynamic executables the bind with libraries at run time, use .loand .sofiles.
- 如果您需要静态可执行文件,请使用“.o”和“.a”文件。
- 如果您需要/想要动态可执行文件在运行时与库绑定,请使用.lo和.so文件。
Introduction
介绍
While I like the answers above, they do not cover the .a/archive library form. So here I will address all three with a bonus of adding in a .so library format, as well. Also, in the vein of stackexchange, I will use more text in case links get broken (note that I did not need reference links for this one).
虽然我喜欢上面的答案,但它们不包括 .a/archive 库形式。所以在这里,我将通过添加 .so 库格式来解决所有三个问题。此外,在 stackexchange 的脉络中,我将使用更多文本以防链接损坏(请注意,我不需要此链接的参考链接)。
Filetype .o
文件类型 .o
When compiling a .ofile is an object file containing the compiler emitted object code for the target platform. To create a .ofile:
编译.o文件时,是一个目标文件,其中包含编译器为目标平台发出的目标代码。创建.o文件:
gcc -c filename.c <==== creates filename.o
Note that this example did not create Position Independent Code (PIC). We consider this an object for possible inclusion in a static library or executable. That is, when we link an executable with a .ofile, the code in the .o file is inserted into the executable --- it is bound at build time, not at run time. That means the executable can be redistributed without including the .o file. Caveat: it is convention that the .ofile is considered non-PIC. We typically name PIC object files with a .loextension.
请注意,此示例未创建位置无关代码 (PIC)。我们认为这是一个可能包含在静态库或可执行文件中的对象。也就是说,当我们将可执行文件与.o文件链接时,.o文件中的代码被插入到可执行文件中——它在构建时绑定,而不是在运行时绑定。这意味着可执行文件可以在不包含 .o 文件的情况下重新分发。警告:按照惯例,.o文件被视为非 PIC。我们通常使用.lo扩展名命名 PIC 目标文件。
Filetype .a
文件类型 .a
The .afile type is an "archive" library. It contains one or more .o files and it is typically used to for creating static executable files.
该.A文件类型是“存档”库。它包含一个或多个 .o 文件,通常用于创建静态可执行文件。
We use the arcommand to manipulate archive libraries. Below in an example that (1) creates an archive library from .ofiles then (2) lists the contents of one.
我们使用ar命令来操作归档库。在下面的示例中,(1) 从.o文件创建一个存档库,然后 (2) 列出其中一个的内容。
Create the Library
创建库
$ ls *.o
a.o b.o c.o <=== the files going in the archive
$ ar q libmyStuff.a *.o <=== put *.o files in an archive (or new one)
ar: creating libmyStuff.a
$ ls *.a <=== just show the library created
libmyStuff.a
Display the Contents of an Archive Library
显示存档库的内容
$ ar t libmyStuff.a
a.o
b.o
c.o
Filetype .lo
文件类型 .lo
The use of .lois a convention that is often used for position independent object files. In the current directory the libtool compilecommand creates both a .lofile and a .ofile, one with PIC code and one without PIC code. See the output below:
.lo的使用是一种常用于位置无关目标文件的约定。在当前目录中, libtool compile命令创建一个.lo文件和一个.o文件,一个带有 PIC 代码,一个没有 PIC 代码。请参阅下面的输出:
$ libtool compile gcc -c a.c
libtool: compile: gcc -c a.c -fPIC -DPIC -o .libs/a.o <== PIC code
libtool: compile: gcc -c a.c -o a.o >/dev/null 2>&1 <== Not-PIC code
$ ls a.lo a.o
a.lo a.o <=== a.lo contains the PIC code.
Also note that the .libssubdirectory was created with a.oin it. This file is PIC code, despite the name. Libtoolmoved this file to the current directory and changed the extension to .lo.
还要注意.libs子目录是用ao创建的。尽管名称如此,但该文件是 PIC 代码。 Libtool将此文件移动到当前目录并将扩展名更改为.lo。
You can always manually create .lofiles simply by using the PIC option(s) to gcc when you compile. Move the resulting .ofiles to .loextension.
你总是可以在编译时简单地通过使用 PIC 选项到 gcc来手动创建.lo文件。将生成的.o文件移动到.lo扩展名。
Filetype .so
文件类型 .so
By convention .so implies a "shared object" library file. We put PIC object files into shared libraries. In contract to .oand .afiles, when we link with .sofiles the code is not included in the resulting compiled file. That is we use run time binding (as in the .locase). There is more than one form of runtime binding, but we won't go into that here.
按照惯例 .so 意味着一个“共享对象”库文件。我们将 PIC 目标文件放入共享库中。根据.o和.a文件,当我们链接.so文件时,代码不包含在生成的编译文件中。那就是我们使用运行时绑定(如在.lo情况下)。运行时绑定的形式不止一种,但我们不会在这里讨论。
回答by Nav
The .lo
file is a library object, which may be built into a shared library, and the .o
file is a standard object file. More info: How to install and use libtool shared library (.lo files)?
该.lo
文件是一个库对象,可以内置到共享库中,该.o
文件是一个标准的目标文件。更多信息:如何安装和使用 libtool 共享库(.lo 文件)?