C语言 编译器和链接器之间有什么区别?

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

What are the differences between a compiler and a linker?

ccompiler-constructionlinkerterminology

提问by smruti

What is the difference between a compiler and a linker in C?

C中的编译器和链接器有什么区别?

回答by Stefano Borini

The compiler converts code written in a human-readable programming language into a machine code representation which is understood by your processor. This step creates objectfiles.

编译器将以人类可读的编程语言编写的代码转换为处理器可以理解的机器代码表示。此步骤创建目标文件。

Once this step is done by the compiler, another step is needed to create a working executable that can be invoked and run, that is, associate the function calls (for example) that your compiled code needs to invoke in order to work. For example, your code could call sprintf, which is a routine in the C standard library. Your code has nothing that does the actual service provided by sprintf, it just reports that it must be called, but the actual code resides somewhere in the common C library. To perform this (and many others) linkages, the linkermust be invoked. After linking, you obtain the actual executable that can run.

一旦编译器完成了这一步,就需要另一个步骤来创建一个可以调用和运行的工作可执行文件,即关联已编译代码需要调用才能工作的函数调用(例如)。例如,您的代码可以调用sprintf,它是 C 标准库中的一个例程。您的代码没有提供由 提供的实际服务sprintf,它只是报告必须调用它,但实际代码驻留在公共 C 库中的某处。要执行此(以及许多其他)链接,必须调用链接器。链接后,您将获得可以运行的实际可执行文件。

回答by Oded

A compiler generates object code files (machine language) from source code.

编译器从源代码生成目标代码文件(机器语言)。

A linkercombines these object code files into an executable.

一个连接器结合了这些目标代码文件转换成可执行。

Many IDEs invoke them in succession, so you never actually see the linker at work. Some languages/compilers do not have a distinct linker and linking is done by the compiler as part of its work.

许多 IDE 会连续调用它们,因此您实际上从未看到链接器在工作。某些语言/编译器没有独特的链接器,链接由编译器作为其工作的一部分完成。