C 语言中的目标文件是什么?
我正在阅读有关 C 语言库的内容,但尚未找到有关目标文件是什么的解释。任何其他编译文件和目标文件之间的真正区别是什么?
如果有人能用人类语言解释,我会很高兴。
I am reading about libraries in C but I have not yet found an explanation on what an object file is. What's the real difference between any other compiled file and an object file?
I would be glad if someone could explain in human language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
目标文件是编译阶段的实际输出。它主要是机器代码,但具有允许链接器查看其中包含哪些符号以及工作所需的符号的信息。 (作为参考,“符号”基本上是全局对象、函数等的名称)
链接器获取所有这些对象文件并将它们组合成一个可执行文件(假设它可以,即:没有任何重复或未定义的文件)符号)。如果您不告诉他们使用命令行选项“仅编译”,许多编译器将为您执行此操作(请阅读:它们自己运行链接器)。 (
-c
是常见的“仅编译;不链接”选项。)An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)
A linker takes all these object files and combines them to form one executable (assuming that it can, i.e.: that there aren't any duplicate or undefined symbols). A lot of compilers will do this for you (read: they run the linker on their own) if you don't tell them to "just compile" using command-line options. (
-c
is a common "just compile; don't link" option.)目标文件是编译后的文件本身。两者没有区别。
通过链接目标文件形成可执行文件。
目标文件包含CPU可以理解的低级指令。这就是为什么它也被称为机器代码。
这种低级机器代码是指令的二进制表示,您也可以直接使用汇编语言编写,然后使用汇编器将汇编语言代码(以英语表示)处理为机器语言(以十六进制表示)。
以下是高级语言(例如 C)代码的此过程的典型高级流程
-->经过预处理器
-->给出优化的代码,仍然是 C
-->通过编译器
-->给出汇编代码
-->通过汇编器
-->给出存储在对象文件中的机器语言代码
-->通过链接器
-->获取可执行文件。
此流程可能有一些变化,例如大多数编译器可以直接生成机器语言代码,而无需通过汇编器。同样,他们可以为您进行预处理。尽管如此,为了更好地理解,分解这些成分还是很好的。
An Object file is the compiled file itself. There is no difference between the two.
An executable file is formed by linking the Object files.
Object file contains low level instructions which can be understood by the CPU. That is why it is also called machine code.
This low level machine code is the binary representation of the instructions which you can also write directly using assembly language and then process the assembly language code (represented in English) into machine language (represented in Hex) using an assembler.
Here's a typical high level flow for this process for code in High Level Language such as C
--> goes through pre-processor
--> to give optimized code, still in C
--> goes through compiler
--> to give assembly code
--> goes through an assembler
--> to give code in machine language which is stored in OBJECT FILES
--> goes through Linker
--> to get an executable file.
This flow can have some variations for example most compilers can directly generate the machine language code, without going through an assembler. Similarly, they can do the pre-processing for you. Still, it is nice to break up the constituents for a better understanding.
有 3 种目标文件。
1. 可重定位目标文件:
包含机器代码,其形式可以在链接时与其他可重定位目标文件组合,以形成可执行目标文件。
如果您有一个 ac 源文件,要使用 GCC 创建其目标文件,您应该运行:
gcc ac -c
完整的过程是:
ac
上运行cc1 )。
as
),as
) 将生成可重定位目标文件。该可重定位目标文件包含:
-g
),2. 共享对象文件:
特殊类型的可重定位对象文件,可以在加载时或运行时动态加载。
3.可执行目标文件:
包含可以直接加载到内存中的机器代码(由加载器,例如 execve)并随后执行。
在多个可重定位目标文件上运行链接器的结果是一个可执行目标文件。链接器通过将所有相同类型的输入节(例如
.data
)合并到相同类型的输出节,从左到右合并来自命令行的所有输入目标文件。它使用符号解析和重定位。额外阅读:静态与动态库
当链接到静态库时,输入对象中引用的函数将被复制到最终的可执行文件中。
使用动态库创建符号表,以实现与库的函数/全局变量的动态链接。因此,结果是部分可执行的目标文件,因为它取决于库。如果该库不存在,则该文件将无法再执行。
链接过程可以按如下方式完成:
命令:
gcc ac -o myexecutable
将调用第 1 点和第 3 点提到的所有命令 (cpp
-> < code>cc1 ->as
->ld
1)1:实际上是
collect2
,这是一个包装ld。There are 3 kind of object files.
1. Relocatable object files:
Contain machine code in a form that can be combined with other relocatable object files at link time, in order to form an executable object file.
If you have an
a.c
source file, to create its object file with GCC you should run:gcc a.c -c
The full process would be:
a.c
cc1
).as
)as
) will produce the relocatable object file.That relocatable object file contains:
-g
was used)2. Shared object files:
Special type of relocatable object file that can be loaded dynamically, either at load time, or at run time.
3. Executable object files:
Contain machine code that can be directly loaded into memory (by the loader, e.g execve) and subsequently executed.
The result of running the linker over multiple relocatable object files is an executable object file. The linker merges all the input object files from the command line, from left-to-right, by merging all the same-type input sections (e.g.
.data
) to the same-type output section. It uses symbol resolution and relocation.Bonus read: Static vs Dynamic Libraries
When linking against a static library the functions that are referenced in the input objects are copied to the final executable.
With dynamic libraries a symbol table is created instead that will enable a dynamic linking with the library's functions/globals. Thus, the result is a partially executable object file, as it depends on the library. If the library doesn't exist, the file can no longer execute.
The linking process can be done as follows:
The command:
gcc a.c -o myexecutable
will invoke all the commands mentioned at point 1 and at point 3 (cpp
->cc1
->as
->ld
1)1: actually is
collect2
, which is a wrapper overld
.目标文件就是编译一个(或多个)源文件时得到的文件。
它可以是完整的可执行文件或库,也可以是中间文件。
目标文件通常包含本机代码、链接器信息、调试符号等。
An object file is just what you get when you compile one (or several) source file(s).
It can be either a fully completed executable or library, or intermediate files.
The object files typically contain native code, linker information, debugging symbols and so forth.
目标文件是依赖于函数、符号和文本来运行程序的代码。就像旧的电传机一样,需要电传打字机才能将信号发送到其他电传机。
就像处理器需要二进制代码才能运行一样,目标文件就像二进制代码但没有链接。链接会创建附加文件,以便用户不必自己编译 C 语言。一旦目标文件与c语言或vb等编译器链接,用户可以直接打开exe文件。
Object files are codes that are dependent on functions, symbols, and text to run the program. Just like old telex machines, which required teletyping to send signals to other telex machine.
In the same way processor's require binary code to run, object files are like binary code but not linked. Linking creates additional files so that the user does not have to have compile the C language themselves. Users can directly open the exe file once the object file is linked with some compiler like c language , or vb etc.