链接器做什么? [英] What do linkers do?

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

问题描述

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

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!\n");

当编译器将你的程序编译成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用来使用称为Overlays(.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).

希望这有助于你理解!

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

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