简体   繁体   English

静态/动态运行时链接

[英]Static/Dynamic Runtime Linking

What are the best practices for choosing the linking method in VC++? 在VC ++中选择链接方法的最佳实践是什么? Can anything/everything be statically linked? 可以静态链接任何东西吗?

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. 补充 :我主要是指lib文件。 Do they behave same as dll linking? 它们的行为与dll链接相同吗?

Dynamic links allow you to upgrade individual DLLs without recompiling your applications. 动态链接使您可以升级单个DLL,而无需重新编译应用程序。 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. 这就是为什么可以在不重新编译您的应用程序的情况下升级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. 编辑:Robert Gamble添加了一条我错过的评论:DLL被加载到操作系统中所有进程共享的内存中。 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. 这意味着,如果两个程序(或程序的两个实例)使用相同的DLL,则它们将使用加载到内存中的相同DLL,这将进一步减少您的整体内存使用量。

DLLs can make 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. 您“添加”了有关LIB文件的评论。 In BOTH Dynamic and Static, you link using LIB files. 在“动态”和“静态”中,都使用LIB文件进行链接。 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). 但是,在动态加载的情况下,您将交付.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). 我更喜欢DLL,因为我的应用程序倾向于更大和更分段,这使我只能交付那些更新的组件(DLL)。 We even separate business logic from presentation in their own DLLs [permits localization of the resource-only dll independent of the logic. 我们甚至在自己的DLL中将业务逻辑与表示分离开来[允许独立于逻辑的纯资源dll本地化。

Programming using DLLs DOES cause you to force yourself to adhere to the contract of the exported class/method or function. 使用DLL进行编程确实会使您强迫自己遵守导出的类/方法或函数的约定。

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). dll的明显优点是,您不仅可以升级整个组件(理论上),而且可以共享通用组件(通过将它们封装在dll中)。 Unfortunately in practice there is a certain amount of binding between dll(s) (even when defined well). 不幸的是,在实践中,dll之间存在一定数量的绑定(即使定义正确)。 That leads you to need to upgrade dll in matching sets and isolating dlls that do no play well together. 这导致您需要升级匹配集中的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. 在现实生活中,应用程序倾向于将它们使用的所有dll与可执行文件放在同一目录中。 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. 然后,升级包括与Windows中央存储库中的DLL同步升级应用程序目录中的DLL集。

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

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