什么是“对象"?在“目标文件"中为什么这样称呼它? [英] What is "object" in "object file" and why is it called this way?

查看:267
本文介绍了什么是“对象"?在“目标文件"中为什么这样称呼它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人问我一个问题:什么是'目标文件'?"

I was asked a question: "What is an 'object file'?".

查看 Wiki 后,我只知道它包含对象.

After looking at Wiki, I only know that it contains objects.

但是那些对象是什么,为什么有人这样称呼它们?

But what are those objects and why someone called them that way?

推荐答案

目标文件(或目标代码)是由编译器从源代码生成的机器代码文件.

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).

使用GCC编译C文件时:

When you compile a C file with GCC:

gcc -Wall -o test test.c

在这里,您正在编译 AND 链接.因此,您将获得一个可执行文件,其中包含该文件所包含符号(库,标头等)的所有内存地址引用.

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).

但是,当您这样做时:

gcc -Wall -o test.o -c test.c

您将生成目标文件.这也是机器代码,但需要链接才能生成可执行文件或库.

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.

例如,当您的一个项目中包含许多C文件时,您将每个文件编译为目标代码,然后将所有目标文件链接在一起以产生最终产品.

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.

例如:

gcc -Wall -o foo.o -c foo.c              // Object file for foo.c
gcc -Wall -o bar.o -c bar.c              // Object file for bar.c
gcc -Wall -o main.o -c main.c            // Object file for main.c
gcc -Wall -o software foo.o bar.o main.o // Executable (foo + bar + main)

术语对象"在这里代表未链接的机器代码序列(基本上). 对象文件包含对象.

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...

有关信息,GCC内部文档仅将对象代码定义为:

For information, the GCC Internals documentation only defines object code as:

作品的源代码"是指对作品进行修改的首选形式. 目标代码"是指作品的任何非源形式.

The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work.

历史原因含糊不清...

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...

这篇关于什么是“对象"?在“目标文件"中为什么这样称呼它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆