windows 静态/动态运行时链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/456323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static/Dynamic Runtime Linking
提问by Codingday
What are the best practices for choosing the linking method in VC++? Can anything/everything be statically linked?
在 VC++ 中选择链接方法的最佳实践是什么?任何东西/一切都可以静态链接吗?
On a dynamically linked project, is the relative/absolute location of the linked library important?
在动态链接的项目中,链接库的相对/绝对位置重要吗?
What are the pros and cons ?
优缺点都有什么 ?
added: I was mainly referring to lib files. Do they behave same as dll linking?
补充:我主要是指lib文件。它们的行为是否与 dll 链接相同?
采纳答案by Spence
Dynamic links allow you to upgrade individual DLLs without recompiling your applications. That is why windows can be upgraded without your application being recompiled, because the dynamic linker is able to determine the entry points in the dll, provided that the method name exists.
动态链接允许您升级单个 DLL,而无需重新编译您的应用程序。这就是为什么可以在不重新编译应用程序的情况下升级 Windows,因为只要方法名称存在,动态链接器就能够确定 dll 中的入口点。
Statically linking your application has a benefit in that calls to the linked code are not indirected, so they run faster. This may have an impact on extremely performance dependent code.
静态链接您的应用程序的好处在于对链接代码的调用不是间接的,因此它们运行得更快。这可能会对极其依赖性能的代码产生影响。
Using DLLs can also help you reduce your memory footprint, as effectively you only load the libraries as you need them and you can unload them when your done (think application plugins, only load an image browsing library when you have an image open etc.)
使用 DLL 还可以帮助您减少内存占用,有效地您只在需要时加载库,并且可以在完成后卸载它们(想想应用程序插件,只有在打开图像时才加载图像浏览库等)
EDIT: Robert Gamble has added a comment which I missed: DLLs are loaded into memory shared by all processes in the operating systems. This means if two programs (or two instances of your program) use the same DLL, they will use the same DLL loaded into memory which will further reduce your overall memory usage.
编辑:Robert Gamble 添加了我错过的评论:DLL 被加载到操作系统中所有进程共享的内存中。这意味着如果两个程序(或您的程序的两个实例)使用同一个 DLL,它们将使用加载到内存中的同一个 DLL,这将进一步减少您的整体内存使用量。
回答by SAMills
DLLs canmake for smaller runtime workingset, if the application were written in such a way as to manage the context switching between DLLs (For example, for larger applications, you could divide the applications functionality into logical boundaries to be implemented within self-contained DLLs and allow the loader to load at runtime).
如果应用程序的编写方式可以管理 DLL 之间的上下文切换(例如,对于较大的应用程序,您可以将应用程序功能划分为要在自包含 DLL 中实现的逻辑边界),DLL可以使运行时工作集更小并允许加载器在运行时加载)。
While it's true that DLLs are primarily installed/copied into the same folder as the .exe, the requirement is to adhere to the loaders loading rules (which includes system folder (bad idea), PATH, current directory [see LoadLibrary API Help documentation for a full description of precedence]).
虽然 DLL 确实主要安装/复制到与 .exe 相同的文件夹中,但要求是遵守加载程序加载规则(包括系统文件夹(坏主意)、PATH、当前目录 [请参阅 LoadLibrary API 帮助文档以了解优先级的完整描述])。
You "added" a comment regarding LIB files. In BOTH Dynamic and Static, you link using LIB files. But in the case of dynamic loading you deliver the .exe along with all dependent DLLs (the LIB files contain the exported entry points for the corresponding DLL).
您“添加”了关于 LIB 文件的评论。在动态和静态中,您都使用 LIB 文件进行链接。但是在动态加载的情况下,您将 .exe 与所有依赖的 DLL 一起交付(LIB 文件包含相应 DLL 的导出入口点)。
I prefer DLLs as my applications tend to be larger and segmented and this allows me to deliver ONLY those updated components (DLLs). We even separate business logic from presentation in their own DLLs [permits localization of the resource-only dll independent of the logic.
我更喜欢 DLL,因为我的应用程序往往更大和分段,这允许我只提供那些更新的组件 (DLL)。我们甚至在他们自己的 DLL 中将业务逻辑与表示分开 [允许独立于逻辑的纯资源 DLL 的本地化。
Programming using DLLs DOES cause you to force yourself to adhere to the contract of the exported class/method or function.
使用 DLL 编程确实会导致您强迫自己遵守导出的类/方法或函数的约定。
回答by Martin York
The obvious advantage to dll is that you can upgrade individual components not just the whole application (in theory) and share common components (by encapsulating them in dlls). Unfortunately in practice there is a certain amount of binding between dll(s) (even when defined well). That leads you to need to upgrade dll in matching sets and isolating dlls that do no play well together.
dll 的明显优势是您可以升级单个组件而不仅仅是整个应用程序(理论上)并共享公共组件(通过将它们封装在 dll 中)。不幸的是,实际上在 dll(s) 之间存在一定数量的绑定(即使定义得很好)。这导致您需要升级匹配集中的 dll 并隔离不能很好地协同工作的 dll。
If not done carefully upgrading DLL can lead to the problems known as DLL Hell.
如果不小心升级 DLL 会导致被称为DLL Hell的问题。
In real life an application tend to put all the dlls they use in the same directory as the executable. This allows upgrading but does not promote sharing. Upgrading then consists of upgrading sets of DLL's in the application directory in sync with he DLL in the windows central repository.
在现实生活中,应用程序倾向于将它们使用的所有 dll 放在与可执行文件相同的目录中。这允许升级但不促进共享。然后升级包括升级应用程序目录中的 DLL 集,与 Windows 中央存储库中的 DLL 同步。