简体   繁体   English

动态链接库的工作原理

[英]How Dynamic-link library works

I was thinking: when I produce a DLL with Visual Studio (C++) it generates 我在想:当我使用Visual Studio(C ++)生成DLL时,它会生成

  • a .dll file 一个.dll文件
  • a .lib file 一个.lib文件

And I have a .h file 我有一个.h文件

So, why not developing directly a static .lib library? 那么,为什么不直接开发静态.lib库呢?
For example, why Office doesn't have .lib files? 例如,为什么Office没有.lib文件?

And, in the future, if I change the DLL, will I have to send to all the machines also the new .lib file and .h file? 并且,在将来,如果我更改DLL,我是否还必须向所有机器发送新的.lib文件和.h文件?

The .h and .lib is only for developers. .h和.lib仅适用于开发人员。 Whoever writes a program to use the DLL. 编写程序以使用DLL的人。

Those who just execute the application need only the .DLL. 那些只执行应用程序的人只需要.DLL。

So if you release a new version, you send DLL to users and the triplet to developers. 因此,如果您发布新版本,则将DLL发送给用户,将三元组发送给开发人员。 Unless you changed the public interface (the exports), the old clients will be happy to use the updated DLL without any work. 除非您更改了公共接口(导出),否则旧客户端将乐意使用更新的DLL而无需任何工作。

If you instead build a static .lib, every client must rebuild his binaries. 如果您改为构建静态.lib,则每个客户端都必须重建其二进制文件。

The .lib file and the header file is the static part of your dynamic library. .lib文件和头文件是动态库的静态部分。
You need the .lib and header file in order to compile and link a program so that it uses your library. 您需要.lib和头文件才能编译和链接程序,以便它使用您的库。

So why not use a static library? 那么为什么不使用静态库呢?
There are multiple reasons. 原因有很多。 One would be that if you use a static library every change of your library causes a recompile of your program. 一个是如果您使用静态库,库的每次更改都会导致重新编译您的程序。 Another that the size of your program will increase. 另一个是程序的大小会增加。 And some more. 还有一些。

So for your second question the lib file are useless for a user of your program. 因此,对于您的第二个问题,lib文件对于您的程序用户来说是无用的。 In case of office. 在办公室的情况下。 As long as you don't have the sources and a compiler the lib files will not help. 只要你没有源代码和编译器,lib文件就无济于事。

For your last question. 对于你的上一个问题。 The answer is also simple. 答案也很简单。 No you don't need to distribute the .lib files. 不,您不需要分发.lib文件。 You can replace the dll files with new versions as long as the interface stays the same. 只要界面保持不变,您就可以用新版本替换dll文件。

This is a SHORT version that tries to answer your specific questions. 这是一个SHORT版本,试图回答您的具体问题。 The subject of "how DLL's and shared libraries work" is quite a few pages long, and I'm just not going to write that all as an answer. “DLL和共享库如何工作”的主题是相当长的几页,我只是不打算将其全部作为答案。

To start with the parts the compiler generats, a .lib and a .dll file, are used as follows: The .lib file contains "stubs" that go into .exe file using the .dll, and contain "where to find the different functions" in the .dll file. 从编译器生成的部分开始,使用.lib和.dll文件,如下所示:.lib文件包含使用.dll进入.exe文件的“存根”,并包含“在哪里查找不同的文件”函数“在.dll文件中。 So this is used only when building the .exe file. 所以这仅在构建.exe文件时使用。 Similarly, the .h file is used when compiling the .dll and .exe file, and aren't used once you have the final binaries (.exe and .dll files) that make up the product. 类似地,.h文件在编译.dll和.exe文件时使用,并且在您拥有构成产品的最终二进制文件(.exe和.dll文件)后不会使用.h文件。

The purpose of using DLL's over static linking is most important when there are multiple executables that use the same .dll file. 当有多个可执行文件使用相同的.dll文件时,使用DLL而不是静态链接的目的是最重要的。 If there is only one "user" of a DLL then the the benefits are reduces, but there are still some benefits (such as compartmentalisation, ability to supply updates to only that part of the code, plugins, etc). 如果只有一个DLL的“用户”,那么好处会减少,但仍然有一些好处(例如分隔,只能为代码的那一部分提供更新的能力,插件等)。

Assuming your .dll is part of a program that contains only binaries, no source code, then you only need to distribute the new .dll [as long as the functions haven't changed in such a way that the .exe or other .dll's that use this .dll has to change, of course]. 假设你的.dll是一个只包含二进制文件,没有源代码的程序的一部分,那么你只需要分发新的.dll [只要函数没有以.exe或其他.dll的方式改变)。使用这个.dll当然必须改变]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM