“静态链接"和“动态链接"是什么意思? [英] What do 'statically linked' and 'dynamically linked' mean?

查看:29
本文介绍了“静态链接"和“动态链接"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常听到静态链接"和动态链接"这两个术语,通常指的是用 CC++C#.它们是什么,它们到底在说什么,它们在链接什么?

I often hear the terms 'statically linked' and 'dynamically linked', often in reference to code written in C, C++ or C#. What are they, what exactly are they talking about, and what are they linking?

推荐答案

从源代码(你写的)到可执行代码(你运行的)有两个阶段(在大多数情况下,不考虑解释代码).

There are (in most cases, discounting interpreted code) two stages in getting from source code (what you write) to executable code (what you run).

首先是将源代码转换为目标模块的编译.

The first is compilation which turns source code into object modules.

第二个,链接,是将对象模块组合在一起形成可执行文件.

The second, linking, is what combines object modules together to form an executable.

区别在于,除其他外,允许第​​三方库包含在您的可执行文件中,而您不会看到它们的源代码(例如用于数据库访问、网络通信和图形用户界面的库),或者用于在不同的语言(例如 C 和汇编代码),然后将它们链接在一起.

The distinction is made for, among other things, allowing third party libraries to be included in your executable without you seeing their source code (such as libraries for database access, network communications and graphical user interfaces), or for compiling code in different languages (C and assembly code for example) and then linking them all together.

当您静态将文件链接到可执行文件时,该文件的内容会在链接时包含.换句话说,文件的内容被物理插入到您将要运行的可执行文件中.

When you statically link a file into an executable, the contents of that file are included at link time. In other words, the contents of the file are physically inserted into the executable that you will run.

当您动态链接时,指向被链接文件的指针(例如文件的文件名)包含在可执行文件中,并且该文件的内容不包含在链接中时间.只有当您稍后运行可执行文件时,这些动态链接的文件才会被购买,并且它们只会被购买到可执行文件的内存副本中,而不是磁盘上的那个.

When you link dynamically, a pointer to the file being linked in (the file name of the file, for example) is included in the executable and the contents of said file are not included at link time. It's only when you later run the executable that these dynamically linked files are bought in and they're only bought into the in-memory copy of the executable, not the one on disk.

它基本上是一种延迟链接的方法.还有一个更多延迟方法(在某些系统上称为后期绑定),它不会引入动态链接文件,直到您实际尝试调用其中的函数.

It's basically a method of deferred linking. There's an even more deferred method (called late binding on some systems) that won't bring in the dynamically linked file until you actually try to call a function within it.

静态链接文件在链接时被锁定"到可执行文件,因此它们永远不会改变.可执行文件引用的动态链接文件只需替换磁盘上的文件即可更改.

Statically-linked files are 'locked' to the executable at link time so they never change. A dynamically linked file referenced by an executable can change just by replacing the file on the disk.

这允许更新功能而无需重新链接代码;每次运行时加载器都会重新链接.

This allows updates to functionality without having to re-link the code; the loader re-links every time you run it.

这有好有坏 - 一方面,它允许更轻松的更新和错误修复,另一方面,如果更新不兼容,它可能导致程序停止工作 - 这有时是可怕的DLL地狱"的原因有人提到,如果将动态链接库替换为不兼容的库,应用程序可能会被破坏(顺便说一下,这样做的开发人员应该会受到追捕和严厉惩罚).

This is both good and bad - on one hand, it allows easier updates and bug fixes, on the other it can lead to programs ceasing to work if the updates are incompatible - this is sometimes responsible for the dreaded "DLL hell" that some people mention in that applications can be broken if you replace a dynamically linked library with one that's not compatible (developers who do this should expect to be hunted down and punished severely, by the way).

作为一个示例,让我们看看用户编译他们的 main.c 文件以进行静态和动态链接的情况.

As an example, let's look at the case of a user compiling their main.c file for static and dynamic linking.

Phase     Static                    Dynamic
--------  ----------------------    ------------------------
          +---------+               +---------+
          | main.c  |               | main.c  |
          +---------+               +---------+
Compile........|.........................|...................
          +---------+ +---------+   +---------+ +--------+
          | main.o  | | crtlib  |   | main.o  | | crtimp |
          +---------+ +---------+   +---------+ +--------+
Link...........|..........|..............|...........|.......
               |          |              +-----------+
               |          |              |
          +---------+     |         +---------+ +--------+
          |  main   |-----+         |  main   | | crtdll |
          +---------+               +---------+ +--------+
Load/Run.......|.........................|..........|........
          +---------+               +---------+     |
          | main in |               | main in |-----+
          | memory  |               | memory  |
          +---------+               +---------+

您可以在静态情况下看到主程序和 C 运行时库在链接时链接在一起(由开发人员).由于用户通常无法重新链接可执行文件,因此他们被库的行为所困扰.

You can see in the static case that the main program and C runtime library are linked together at link time (by the developers). Since the user typically cannot re-link the executable, they're stuck with the behaviour of the library.

在动态情况下,主程序与 C 运行时导入库链接(它声明了动态库中的内容,但实际上并没有定义它).即使缺少实际代码,这也允许链接器进行链接.

In the dynamic case, the main program is linked with the C runtime import library (something which declares what's in the dynamic library but doesn't actually define it). This allows the linker to link even though the actual code is missing.

然后,在运行时,操作系统加载程序将主程序与 C 运行时 DLL(动态链接库或共享库或其他命名法)进行后期链接.

Then, at runtime, the operating system loader does a late linking of the main program with the C runtime DLL (dynamic link library or shared library or other nomenclature).

C 运行时的所有者可以随时插入新的 DLL 以提供更新或错误修复.如前所述,这既有优点也有缺点.

The owner of the C runtime can drop in a new DLL at any time to provide updates or bug fixes. As stated earlier, this has both advantages and disadvantages.

这篇关于“静态链接"和“动态链接"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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