动态链接和动态加载有什么区别 [英] What is the difference between dynamic linking and dynamic loading

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

问题描述

据我了解 动态加载是指在加载或运行时将库(或与此相关的任何其他二进制文件)加载到内存中.因此,在下面的程序中,当调用动态加载程序的dlopen()出现时,如果尚未加载库,它将把lib加载到内存中.

动态链接是指在加载或运行时完成的链接.并解析外部引用. 因此,在dlsym()函数下面的程序中,函数将要求余弦函数,并且将在图片中显示动态链接,并会解析符号.

int main(int argc, char **argv) {
    void *handle;
    double (*cosine)(double);
    char *error;

    handle = dlopen ("/lib/libm.so.6", RTLD_LAZY);
    if (!handle) {
        fputs (dlerror(), stderr);
        exit(1);
    }

    cosine = dlsym(handle, "cos");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }

解决方案

这两个术语的相似之处在于,它们指的是将要使用的确切库的确定推迟到程序运行之前,但已经表示出不同的方面. /p>

动态加载发生在显式加载库(例如使用dlopen())时,而动态链接发生在动态链接的可执行文件被加载并由OS隐式处理时.目的不同.

在第一种情况下,动态加载库用于解析来自不同库的符号,这些库是可选的或具有互斥的符号,并且在程序运行之前无法确定要使用哪个库.

例如,程序可以根据配置文件的内容确定与特定数据库进行交互并且仅在读取配置文件后才需要加载数据库特定的库.它需要等到运行时解析配置文件后,再调用dlopen().

或者,动态链接的可执行文件(与大多数可执行文件一样)将在链接时确定其所需库的列表,并且这些库将在程序开始在运行时开始执行之前自动解析.此选项与加载静态链接的可执行文件相反,主要是为了节省内核内存和可执行文件的大小,因为对于使用该库的所有可执行文件,内核只需要将该库加载一次.您可以在动态链接的可执行文件上运行程序ldd来确定所有必需的库.

As i understand Dynamic loading means loading the library (or any other binary for that matter) into the memory during load or run-time. so in program below when dlopen() called dynamic loader will come in to picture and it will load the lib in to the memory if library is not loaded already.

Dynamic linking refers to the linking that is done during load or run-time. and it resolves external references. So in program below dlsym() function will ask for cosine function and dynamic linking will come in picture and symbols will be resolved.

int main(int argc, char **argv) {
    void *handle;
    double (*cosine)(double);
    char *error;

    handle = dlopen ("/lib/libm.so.6", RTLD_LAZY);
    if (!handle) {
        fputs (dlerror(), stderr);
        exit(1);
    }

    cosine = dlsym(handle, "cos");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }

解决方案

Both of these terms are similar in that they refer to postponing the determination of the exact library to use until the program runs but have come to signify different aspects.

Dynamic loading occurs when a library is loaded explicitly (e.g. using dlopen()) while dynamic linking occurs when an executable that is dynamically linked gets loaded and is handled implicitly by the OS. The purposes are different.

In the first case, dynamically loading a library is used to resolve symbols from different libraries that are optional or have symbols that are mutually exclusive and which library to use can not be determined until the program is running.

For example, a program can determine based on the contents of a configuration file that it will need to interact with a particular database and need to load the database specific library only after it has read he configuration file. It would need to wait until the configuration file was parsed at run time and then call dlopen().

Alternately, a dynamically linked executable (as most executables are) will have its list of required libraries determined at link time, and those libraries will be automatically resolved before the program begins to execute at run time. This option is opposed to loading a statically linked executable and is primarily meant to conserve kernel memory and executabe size because the library only need be loaded once by the kernel for all the executables that use that library. You can run the program ldd on a dynamically linked executable to determine all of the necessary libraries.

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

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