.a .o 和 .lo 文件之间的区别
C 语言中 .a
.o
和 .lo
文件有什么区别?
What is the difference between .a
.o
and .lo
file in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C 语言中 .a
.o
和 .lo
文件有什么区别?
What is the difference between .a
.o
and .lo
file in C?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
.o、.a、.lo 和 .so 之间的区别。
执行摘要
注意:
简介
虽然我喜欢上面的答案,但它们并未涵盖 .a/archive 库形式。因此,在这里我将解决所有三个问题,并添加 .so 库格式。另外,本着 stackexchange 的精神,我将使用更多文本,以防链接损坏(请注意,我不需要此链接的参考链接)。
文件类型 .o
当编译 .o 文件时,它是一个目标文件,其中包含编译器为目标平台发出的目标代码。要创建 .o 文件:
请注意,此示例未创建位置无关代码 (PIC)。我们认为这是一个可能包含在静态库或可执行文件中的对象。也就是说,当我们将可执行文件与 .o 文件链接时,.o 文件中的代码将被插入到可执行文件中 --- 它是在构建时绑定的,而不是在运行时绑定的。这意味着可执行文件可以在不包含 .o 文件的情况下重新分发。警告:按照惯例,.o 文件被视为非 PIC。我们通常使用 .lo 扩展名命名 PIC 对象文件。
文件类型 .a
.a 文件类型是“归档”库。它包含一个或多个 .o 文件,通常用于创建静态可执行文件。
我们使用ar命令来操作存档库。下面的示例中,(1) 从 .o 文件创建存档库,然后 (2) 列出其中的内容。
创建库
显示存档库的内容
文件类型 .lo
.lo 的使用是一种惯例,通常用于与位置无关的对象文件。在当前目录中,libtoolcompile 命令创建一个 .lo 文件和一个 .o 文件,一个包含 PIC 代码,一个不包含 PIC 代码。请参阅下面的输出:
另请注意,.libs 子目录是使用其中的ao 创建的。尽管名称如此,但该文件是 PIC 代码。 Libtool 将此文件移至当前目录,并将扩展名更改为 .lo。
您始终可以在编译时通过使用 gcc 的 PIC 选项来手动创建 .lo 文件。将生成的 .o 文件移动到 .lo 扩展名。
文件类型 .so
按照约定,.so 意味着“共享对象”库文件。我们将 PIC 目标文件放入共享库中。与 .o 和 .a 文件不同,当我们链接 .so 文件时,代码不会包含在生成的编译文件中。也就是说,我们使用运行时绑定(如 .lo 情况)。运行时绑定有不止一种形式,但我们不会在这里讨论。
Difference Between .o, .a, .lo and .so.
Executive Summary
Note:
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).
Filetype .o
When compiling a .o file is an object file containing the compiler emitted object code for the target platform. To create a .o file:
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 .o file, 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 .o file is considered non-PIC. We typically name PIC object files with a .lo extension.
Filetype .a
The .a file type is an "archive" library. It contains one or more .o files and it is typically used to for creating static executable files.
We use the ar command to manipulate archive libraries. Below in an example that (1) creates an archive library from .o files then (2) lists the contents of one.
Create the Library
Display the Contents of an Archive Library
Filetype .lo
The use of .lo is a convention that is often used for position independent object files. In the current directory the libtool compile command creates both a .lo file and a .o file, one with PIC code and one without PIC code. See the output below:
Also note that the .libs subdirectory was created with a.o in it. This file is PIC code, despite the name. Libtool moved this file to the current directory and changed the extension to .lo.
You can always manually create .lo files simply by using the PIC option(s) to gcc when you compile. Move the resulting .o files to .lo extension.
Filetype .so
By convention .so implies a "shared object" library file. We put PIC object files into shared libraries. In contract to .o and .a files, when we link with .so files the code is not included in the resulting compiled file. That is we use run time binding (as in the .lo case). There is more than one form of runtime binding, but we won't go into that here.
“.lo”文件是一个库对象,可以构建它到共享库中,“.o”文件是标准目标文件
.lo 文件是 libtool 对象,Libtool 使用它来确定哪些对象文件可以构建到共享库中
The '.lo' file is a library object, which may be built into a shared library, and the '.o' file is a standard object file
The .lo file is the libtool object, which Libtool uses to determine what object file may be built into a shared library
.lo
文件是一个库对象,可以内置到共享库中,.o
文件是标准对象文件。更多信息:如何安装和使用 libtool 共享库( .lo 文件)?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)?