什么是“对象”?在“目标文件”中为什么这样称呼?
I was asked a question: "What is an 'object file'?".
After looking at Wiki, I only know that it contains objects.
But what are those objects and why someone called them that way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
目标文件(或目标代码)是编译器从源代码生成的机器代码文件。
与可执行文件的区别在于,目标文件没有链接,因此尚未定义对函数、符号等的引用(它们的内存地址基本上留空)。
当您使用 GCC 编译 C 文件时:
这里您正在编译 AND 链接。因此,您将获得一个可执行文件,其中包含其所包含符号(库、标头等)的所有内存地址引用。
但是当你这样做时:
你将生成目标文件。它也是机器代码,但需要链接才能生成可执行文件或库。
例如,当您的项目包含许多 C 文件时,您将把每个文件编译成目标代码,然后将所有目标文件链接在一起以生成最终产品。
例如:
术语“对象”在这里代表未链接的机器代码序列(基本上)。
对象文件包含对象。
你问:为什么这样称呼。我实在无法回答。 “蓝”为什么叫“蓝”? ; )
这只是自...好吧,数十年以来使用的术语...
仅供参考,GCC 内部文档仅将目标代码定义为:
关于历史原因相当模糊......
我只是希望您现在更好地理解什么是目标文件。我认为这比知道为什么这么称呼更重要,因为单词只是,嗯,单词......
Object files (or object code) are machine code files generated by a compiler from source code.
The difference with an executable is that the object file isn't linked, so references to functions, symbols, etc aren't defined yet (their memory addresses is basically left blank).
When you compile a C file with GCC:
Here you are compiling AND linking. So you'll got an executable, containing all the memory addresses references for the symbols it contains (libraries, headers, etc).
But when you do this:
You'll produce and object file. It's also machine code, but it will need to be linked in order to produce an executable, or a library.
When you have a project with many C files (for instance), you'll compile each one into object code, and then you will link all object files together in order to produce the final product.
For instance:
The term object stands here for sequences of unlinked machine code (basically).
An object file contains objects.
You asked: why is this call that way. I can't really answer. Why is "blue" named "blue"? ; )
It's just the term used since... well, decades...
For information, the GCC Internals documentation only defines object code as:
Pretty vague about the historical reason...
I simply hope you now understand better what is an object file. I think it's more important than knowing why it's called like that, as words are just, well, words...
我相信这个名称与区分以下内容有关:
对象文件包含:
来源:此处
I believe the name has something to do with making a distinction between:
Object files contain:
Source: here
目标文件是源(文本)文件的二进制表示。它是各个部分的集合,将数据类型分隔在以下位置:
根据您的编译器/环境,这些可能会有所不同。
例如在 *nix 系统上:
objdump -d a.out <--- 提供我们编译的 a.cpp
这里的各种调用命令然后被各种库调用以调用实际的函数。
An object file is binary representation of source(text) file. It's a collection of various sections segragating type of data in:
Depending on your compiler/environment these may differ.
E.g. on *nix systems:
objdump -d a.out <--- provide we compiled a.cpp
The various call commands here are then liked to the various libraries to call the actual functions.
根据您链接的页面,每个序列或对象通常包含主机完成某些任务的指令,可能还伴有相关数据和元数据(例如重定位信息、堆栈展开信息、注释、程序符号、调试或分析信息) )。
基本上,目标文件中的每个对象都是一个函数,以及链接器将其包含到完整程序中的相关信息。
According to the page you linked, Each sequence, or object, typically contains instructions for the host machine to accomplish some task, possibly accompanied by related data and metadata (e.g. relocation information, stack unwinding information, comments, program symbols, debugging or profiling information).
Basically, each object in the object file is a function, and the relevant info for the linker to include it into the full program.