动态加载和动态链接的区别? [英] difference between dynamic loading and dynamic linking?

查看:26
本文介绍了动态加载和动态链接的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用之前不会加载例程.所有例程都以可重新定位的加载格式保存在磁盘上.主程序加载到内存&被执行.这称为动态链接.

Routine is not loaded until it is called. All routines are kept on disk in a re-locatable load format. The main program is loaded into memory & is executed. This is called Dynamic Linking.

为什么这称为动态链接?不应该是动态加载吗,因为在动态加载中调用例程之前不会加载例程,而在动态链接中,链接推迟到执行时间.

Why this is called Dynamic Linking? Shouldn't it be Dynamic Loading because Routine is not loaded until it is called in dynamic loading where as in dynamic linking, Linking postponed until execution time.

推荐答案

这个答案假设你知道基本的 Linux 命令.

This answer assumes that you know basic Linux command.

在 Linux 中,有两种类型的库:静态库或共享库.

In Linux, there are two types of libraries: static or shared.

为了调用静态库中的函数,您需要将库静态链接到可执行文件,从而生成静态二进制文件.

In order to call functions in a static library you need to statically link the library into your executable, resulting in a static binary.

在共享库中调用函数时,您有两种选择.

While to call functions in a shared library, you have two options.

第一个选项是动态链接,这是常用的 - 在编译可执行文件时,您必须指定程序使用的共享库,否则它甚至无法编译.当您的程序启动时,系统会负责打开这些库,这些库可以使用 ldd 命令列出.

First option is dynamic linking, which is commonly used - when compiling your executable you must specify the shared library your program uses, otherwise it won't even compile. When your program starts it's the system's job to open these libraries, which can be listed using the ldd command.

另一个选项是动态加载 - 当您的程序运行时,打开该库是程序的工作.此类程序通常与 libdl 链接,它提供了打开共享库的能力.

The other option is dynamic loading - when your program runs, it's the program's job to open that library. Such programs are usually linked with libdl, which provides the ability to open a shared library.

摘自维基百科:

动态加载是计算机程序在运行时可以使用的一种机制时间,将库(或其他二进制文件)加载到内存中,检索库中包含的函数和变量的地址,执行那些函数或访问那些变量,并从中卸载库记忆.它是计算机程序可以通过的三种机制之一使用其他软件;另外两个是静态链接和动态链接.与静态链接和动态链接不同,动态加载允许计算机程序在没有这些的情况下启动库,发现可用的库,并潜在地获得附加功能.

Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory. It is one of the 3 mechanisms by which a computer program can use some other software; the other two are static linking and dynamic linking. Unlike static linking and dynamic linking, dynamic loading allows a computer program to start up in the absence of these libraries, to discover available libraries, and to potentially gain additional functionality.

如果您仍然感到困惑,请先阅读这篇很棒的文章:Linux 动态库剖析 并构建 dynamic加载示例来感受一下,然后回到这个答案.

If you are still in confusion, first read this awesome article: Anatomy of Linux dynamic libraries and build the dynamic loading example to get a feel of it, then come back to this answer.

这是我的 ldd ./dl 输出:

linux-vdso.so.1 =>  (0x00007fffe6b94000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f400f1e0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f400ee10000)
/lib64/ld-linux-x86-64.so.2 (0x00007f400f400000)

如您所见,dl 是一个动态的可执行文件,它依赖于 libdl,它由 Linux 的 ld.so 动态链接运行 dl 时的动态链接器.列表中的其他 3 个库也是如此.

As you can see, dl is a dynamic executable that depends on libdl, which is dynamically linked by ld.so, the Linux dynamic linker when you run dl. Same is true for the other 3 libraries in the list.

libm 未显示在此列表中,因为它用作动态加载的库.在要求 ld 加载它之前不会加载它.

libm doesn't show in this list, because it is used as a dynamically loaded library. It isn't loaded until ld is asked to load it.

这篇关于动态加载和动态链接的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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