在某些情况下,选择静态链接而不是动态链接或反之,是否有令人信服的性能原因?我听过或读过以下内容,但我对这个主题了解不够,无法保证其真实性。

1)静态链接和动态链接在运行时性能上的差异通常可以忽略不计。

如果使用使用概要数据优化程序热路径的分析编译器,2)(1)是不正确的,因为使用静态链接,编译器可以优化你的代码和库代码。使用动态链接,只能优化您的代码。如果大部分时间都花在运行库代码上,这可能会有很大的不同。否则,(1)仍然适用。


当前回答

在类unix系统上,动态链接会使“root”很难使用安装在偏僻位置的共享库的应用程序。这是因为对于具有根权限的进程,动态连接器通常不会注意LD_LIBRARY_PATH或其等效内容。有时,静态链接可以解决问题。

另外,安装过程必须确定库的位置,但这可能会使计算机上的多个软件版本难以共存。

其他回答

Dynamic linking can reduce total resource consumption (if more than one process shares the same library (including the version in "the same", of course)). I believe this is the argument that drives its presence in most environments. Here "resources" include disk space, RAM, and cache space. Of course, if your dynamic linker is insufficiently flexible there is a risk of DLL hell. Dynamic linking means that bug fixes and upgrades to libraries propagate to improve your product without requiring you to ship anything. Plugins always call for dynamic linking. Static linking, means that you can know the code will run in very limited environments (early in the boot process, or in rescue mode). Static linking can make binaries easier to distribute to diverse user environments (at the cost of sending a larger and more resource-hungry program). Static linking may allow slightly faster startup times, but this depends to some degree on both the size and complexity of your program and on the details of the OS's loading strategy.


Some edits to include the very relevant suggestions in the comments and in other answers. I'd like to note that the way you break on this depends a lot on what environment you plan to run in. Minimal embedded systems may not have enough resources to support dynamic linking. Slightly larger small systems may well support dynamic linking because their memory is small enough to make the RAM savings from dynamic linking very attractive. Full-blown consumer PCs have, as Mark notes, enormous resources, and you can probably let the convenience issues drive your thinking on this matter.


要解决性能和效率问题:视情况而定。

通常情况下,动态库需要某种粘合层,这通常意味着双重分派或函数寻址中额外的间接层,并且可能会消耗一点速度(但是函数调用时间实际上是运行时间的很大一部分吗??)

但是,如果您正在运行多个进程,并且它们都经常调用同一个库,那么使用动态链接而不是静态链接时,您最终可以节省缓存行(从而赢得运行性能)。(除非现代操作系统足够智能,能够在静态链接的二进制文件中注意到相同的段。似乎很难,有人知道吗?)

另一个问题:加载时间。你需要支付装货费用。你什么时候支付这个费用取决于操作系统的工作方式以及你使用的链接。也许你宁愿推迟支付,直到你知道你需要它。

注意,静态链接与动态链接传统上不是一个优化问题,因为它们都涉及到对目标文件的单独编译。然而,这并不是必需的:原则上,编译器可以最初将“静态库”“编译”到一个摘要的AST表单中,并通过将这些AST添加到为主代码生成的AST中来“链接”它们,从而实现全局优化。我使用的系统都没有做到这一点,所以我不能评论它的工作效果如何。

回答性能问题的方法总是通过测试(并尽可能使用与部署环境相似的测试环境)。

另一个尚未讨论的问题是修复库中的错误。

使用静态链接,您不仅需要重新构建库,还必须重新链接和重新分发可执行文件。如果库只在一个可执行文件中使用,这可能不是问题。但是,需要重新链接和重新分发的可执行文件越多,痛苦就越大。

使用动态链接,只需重新构建和重新分发动态库,就完成了工作。

这真的很简单。当您在源代码中进行更改时,您希望等待10分钟或20秒来构建它?我只能忍受20秒。除此之外,我要么拿出剑来,要么开始考虑如何使用单独的编译和链接将其带回舒适区。

Another consideration is the number of object files (translation units) that you actually consume in a library vs the total number available. If a library is built from many object files, but you only use symbols from a few of them, this might be an argument for favoring static linking, since you only link the objects that you use when you static link (typically) and don't normally carry the unused symbols. If you go with a shared lib, that lib contains all translation units and could be much larger than what you want or need.

动态链接是满足某些许可要求(如LGPL)的唯一实用方法。