链接器是做什么的?

What do linkers do?(链接器是做什么的?)

本文介绍了链接器是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道.我知道编译器会将您编写的代码转换为二进制文件,但链接器是做什么的?他们对我来说一直是个谜.

I've always wondered. I know that compilers convert the code you write into binaries but what do linkers do? They've always been a mystery to me.

我大致了解什么是链接".这是对库和框架的引用被添加到二进制文件中的时候.我不明白除此之外的任何事情.对我来说,它有效".我也了解动态链接的基础知识,但没有太深入.

I roughly understand what 'linking' is. It is when references to libraries and frameworks are added to the binary. I don't understand anything beyond that. For me it "just works". I also understand the basics of dynamic linking but nothing too deep.

有人能解释一下这些术语吗?

Could someone explain the terms?

推荐答案

要了解链接器,首先要了解将源文件(例如 C 或 C++ 文件)转换为可执行文件(可执行文件是可以在您的机器或运行相同机器架构的其他机器上执行的文件).

To understand linkers, it helps to first understand what happens "under the hood" when you convert a source file (such as a C or C++ file) into an executable file (an executable file is a file that can be executed on your machine or someone else's machine running the same machine architecture).

在后台,当程序被编译时,编译器会将源文件转换为目标字节码.此字节码(有时称为目标码)是只有您的计算机体系结构才能理解的助记指令.传统上,这些文件具有 .OBJ 扩展名.

Under the hood, when a program is compiled, the compiler converts the source file into object byte code. This byte code (sometimes called object code) is mnemonic instructions that only your computer architecture understands. Traditionally, these files have an .OBJ extension.

创建目标文件后,链接器开始发挥作用.通常,一个真正的程序做任何有用的事情都需要引用其他文件.例如,在 C 语言中,将您的姓名打印到屏幕上的简单程序包括:

After the object file is created, the linker comes into play. More often than not, a real program that does anything useful will need to reference other files. In C, for example, a simple program to print your name to the screen would consist of:

printf("Hello Kristina!
");

当编译器将你的程序编译成 obj 文件时,它只是简单地引用 printf 函数.链接器解析此引用.大多数编程语言都有一个标准的例程库来涵盖该语言所期望的基本内容.链接器将您的 OBJ 文件与此标准库链接.链接器还可以将您的 OBJ 文件与其他 OBJ 文件链接.您可以创建具有可由另一个 OBJ 文件调用的功能的其他 OBJ 文件.链接器的工作方式几乎类似于文字处理器的复制和粘贴.它复制"出程序引用的所有必要函数并创建一个可执行文件.有时,复制出来的其他库依赖于其他 OBJ 或库文件.有时,链接器必须非常递归才能完成其工作.

When the compiler compiled your program into an obj file, it simply puts a reference to the printf function. The linker resolves this reference. Most programming languages have a standard library of routines to cover the basic stuff expected from that language. The linker links your OBJ file with this standard library. The linker can also link your OBJ file with other OBJ files. You can create other OBJ files that have functions that can be called by another OBJ file. The linker works almost like a word processor's copy and paste. It "copies" out all the necessary functions that your program references and creates a single executable. Sometimes other libraries that are copied out are dependent on yet other OBJ or library files. Sometimes a linker has to get pretty recursive to do its job.

请注意,并非所有操作系统都创建一个可执行文件.例如,Windows 使用 DLL 将所有这些功能放在一个文件中.这会减小可执行文件的大小,但会使可执行文件依赖于这些特定的 DLL.DOS 曾经使用称为覆盖(.OVL 文件)的东西.这有很多目的,但一个是将常用的功能放在一个文件中(如果您想知道,它的另一个目的是能够将大型程序放入内存中.DOS 在内存方面存在限制,并且覆盖可以从内存中卸载",其他覆盖可以加载"在该内存之上,因此名称为覆盖").Linux 有共享库,这与 DLL 的想法基本相同(我认识的 Linux 核心人员会告诉我有很多很大的不同).

Note that not all operating systems create a single executable. Windows, for example, uses DLLs that keep all these functions together in a single file. This reduces the size of your executable, but makes your executable dependent on these specific DLLs. DOS used to use things called Overlays (.OVL files). This had many purposes, but one was to keep commonly used functions together in 1 file (another purpose it served, in case you're wondering, was to be able to fit large programs into memory. DOS has a limitation in memory and overlays could be "unloaded" from memory and other overlays could be "loaded" on top of that memory, hence the name, "overlays"). Linux has shared libraries, which is basically the same idea as DLLs (hard core Linux guys I know would tell me there are MANY BIG differences).

希望这能帮助你理解!

这篇关于链接器是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:链接器是做什么的?

基础教程推荐