C语言 什么是 C 中的目标文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7718299/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:52:28  来源:igfitidea点击:

What's an object file in C?

ccompilationlinkerobject-files

提问by Pithikos

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.

我正在阅读 C 语言中的库,但我还没有找到关于什么是目标文件的解释。任何其他编译文件和目标文件之间的真正区别是什么?
如果有人能用人类语言解释,我会很高兴。

回答by cHao

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, ie: 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. (-cis a common "just compile; don't link" option.)

链接器获取所有这些目标文件并将它们组合成一个可执行文件(假设它可以,即:没有任何重复或未定义的符号)。如果您不告诉它们使用命令行选项“仅编译”,许多编译器会为您执行此操作(阅读:它们自己运行链接器)。(-c是一个常见的“只编译;不链接”选项。)

回答by RHT

  1. An Object file is the compiled file itself. There is no difference between the two.

  2. An executable file is formed by linking the Object files.

  3. Object file contains low level instructions which can be understood by the CPU. That is why it is also called machine code.

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

  1. 对象文件是编译后的文件本身。两者没有区别。

  2. 通过链接目标文件形成可执行文件。

  3. 目标文件包含 CPU 可以理解的低级指令。这就是为什么它也被称为机器码。

  4. 这种低级机器代码是指令的二进制表示,您也可以直接使用汇编语言编写,然后使用汇编程序将汇编语言代码(以英文表示)处理为机器语言(以十六进制表示)。

Here's a typical high level flow for this process for code in High Level Language such as C

这是使用高级语言(例如 C)的代码的此过程的典型高级流程

--> goes through pre-processor

--> 通过预处理器

--> to give optimized code, still in C

--> 给出优化的代码,仍然是 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.

这个流程可以有一些变化,例如大多数编译器可以直接生成机器语言代码,而无需通过汇编程序。同样,他们可以为您进行预处理。尽管如此,为了更好地理解,分解组成部分还是很好的。

回答by Paschalis

There are 3 kind of object files.

有3种目标文件。

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.csource file, to create its object file with GCC you should run: gcc a.c -c

如果你有一个a.c源文件,要使用 GCC 创建它的目标文件,你应该运行: gcc a.c -c

The full process would be: preprocessor (cpp) would run over a.c. Its output (still source) will feed into the compiler (cc1). Its output (assembly) will feed into the assembler (as), which will produce the relocatable object file. That file contains object code and linking (and debugging if -gwas used) metadata, and is not directly executable.

完整的过程是: 预处理器 (cpp) 将运行 ac 它的输出(仍然是源代码)将输入到编译器 (cc1) 中。它的输出(汇编)将输入汇编器(as),汇编器将生成relocatable object file. 该文件包含目标代码和链接(如果-g使用了调试)元数据,并且不能直接执行。

Shared object files

共享目标文件

Special type of relocatable object file that can be loaded dynamically, either at load time, or at run time. Shared libraries are an example of these kinds of objects.

可以在加载时或运行时动态加载的特殊类型的可重定位目标文件。共享库就是这类对象的一个​​例子。

Executable object files

可执行的目标文件

Contain machine code that can be directly loaded into memory (by the loader, e.g execve) and subsequently executed.

包含可以直接加载到内存中(由加载器,例如execve)并随后执行的机器代码。

The result of running the linker over multiple relocatable object filesis 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 resolutionand relocation.

在多个上运行链接器的结果relocatable object files是一个executable object file. 链接器通过将所有相同类型的输入节(例如.data)合并到相同类型的输出节,从左到右合并来自命令行的所有输入目标文件。它使用symbol resolutionrelocation

Bonus read:

奖金阅读:

When linking against a static librarythe functions that are referenced in the input objects are copied to the final executable. With dynamic librariesa 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).

链接static library时,输入对象中引用的函数将复制到最终的可执行文件中。随着dynamic libraries创建一个符号表代替,使一个充满活力与图书馆的功能/全局链接。因此,结果是一个部分可执行的目标文件,因为它取决于库。如果库不存在,则文件不能再执行)。

The linking process can be done as follows: ld a.o -o myexecutable

链接过程可以如下完成: ld a.o -o myexecutable

The command: gcc a.c -o myexecutablewill invoke all the commands mentioned at point 1 and at point 3 (cpp -> cc1 -> as -> ld1)

命令:gcc a.c -o myexecutable将调用在第 1 点和第 3 点提到的所有命令(cpp -> cc1 -> as -> ld 1

1: actually is collect2, which is a wrapper over ld.

1:实际上是collect2,它是ld的包装器。

回答by Mat

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.

目标文件通常包含本机代码、链接器信息、调试符号等。

回答by Farhan

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.

以同样的方式,处理器需要二进制代码才能运行,目标文件就像二进制代码,但没有链接。链接会创建附加文件,因此用户不必自己编译 C 语言。一旦目标文件与某些编译器(如 c 语言或 vb 等)链接,用户就可以直接打开 exe 文件。