在某些情况下,选择静态链接而不是动态链接或反之,是否有令人信服的性能原因?我听过或读过以下内容,但我对这个主题了解不够,无法保证其真实性。
1)静态链接和动态链接在运行时性能上的差异通常可以忽略不计。
如果使用使用概要数据优化程序热路径的分析编译器,2)(1)是不正确的,因为使用静态链接,编译器可以优化你的代码和库代码。使用动态链接,只能优化您的代码。如果大部分时间都花在运行库代码上,这可能会有很大的不同。否则,(1)仍然适用。
在某些情况下,选择静态链接而不是动态链接或反之,是否有令人信服的性能原因?我听过或读过以下内容,但我对这个主题了解不够,无法保证其真实性。
1)静态链接和动态链接在运行时性能上的差异通常可以忽略不计。
如果使用使用概要数据优化程序热路径的分析编译器,2)(1)是不正确的,因为使用静态链接,编译器可以优化你的代码和库代码。使用动态链接,只能优化您的代码。如果大部分时间都花在运行库代码上,这可能会有很大的不同。否则,(1)仍然适用。
当前回答
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.
其他回答
我同意dnmckee提到的观点,另外:
静态链接的应用程序可能更容易部署,因为很少或没有额外的文件依赖项(.dll / .so),当它们丢失或安装在错误的位置时,可能会导致问题。
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)的唯一实用方法。
静态链接只给你一个单一的exe,为了做出改变,你需要重新编译你的整个程序。而在动态链接中,您只需要对dll进行更改,当您运行exe时,这些更改将在运行时被拾取。通过动态链接(例如:windows)更容易提供更新和错误修复。
在类unix系统上,动态链接会使“root”很难使用安装在偏僻位置的共享库的应用程序。这是因为对于具有根权限的进程,动态连接器通常不会注意LD_LIBRARY_PATH或其等效内容。有时,静态链接可以解决问题。
另外,安装过程必须确定库的位置,但这可能会使计算机上的多个软件版本难以共存。