C++ 应用程序 - 我应该为库使用静态链接还是动态链接?

我将开始一个新的 C++ 项目,该项目将依赖于一系列库,包括部分 Boost 库、log4cxx 或 google 日志库 - 并且随着项目的发展,其他库(我不能尚未预期).

I am going to start a new C++ project that will rely on a series of libraries, including part of the Boost libraries, the log4cxx or the google logging library - and as the project evolves other ones as well (which I can not yet anticipate).

它将必须在 32 位和 64 位系统上运行,很可能在一个非常多样化的 Linux 环境中,我不希望所有必需的库都可??用,也没有 su 权限.

It will have to run on both 32 and 64 bit systems, most probably in a quite diverse Linux environment where I do not expect to have all the required libraries available nor su privileges.

我的问题是,我应该通过动态或静态链接到所有这些库来构建我的应用程序吗?

My question is, should I build my application by dynamically or statically linking to all these libraries?

注意事项:

(1) 我知道在开发过程中静态链接可能会很痛苦(更长的编译时间、32 位和 64 位的交叉编译、向下依赖链以包含所有库等),但它更容易在测试期间 - 只需移动文件并运行.

(1) I am aware the static linking might be a pain during development (longer compile times, cross-compiling for both 32 and 64 bit, going down dependency chains to include all libraries, etc), but it's a lot easier during testing - just move the file and run.

(2) 另一方面,在开发阶段动态链接接缝更容易 - 编译时间短,(不知道如何处理从我的 32 位开发环境动态链接到 64 位库),没有依赖的喧嚣链.另一方面,新版本的部署可能很丑陋 - 特别是在需要新库时(请参阅上述条件,即在目标机器上没有 su 权限,也没有这些库可用).

(2) On the other hand, dynamic linking seams easier during development phase - short compile times, (don't really know how to handle dynamic linking to 64 bit libraries from my 32 bit dev environment), no hustle with dependency chains. Deployment of new versions on the other hand can be ugly - especially when new libraries are required (see condition above of not having su rights on the targeted machines, nor these libraries available).

(3) 我已阅读有关此主题的相关问题,但无法确定哪种方法最适合我的情况.

(3) I've read the related questions regarding this topic but couldn't really figure out which approach would best fit my scenario.

结论:

  1. 感谢大家的投入!
  2. 我可能会使用静态链接,因为:
    • 更容易部署
    • 性能期间可预测的性能和更一致的结果.测试(看这篇论文:http://www.inf.usi.ch/faculty/hauswirth/publications/CU-CS-1042-08.pdf)
    • 正如所指出的,静态和动态编译的大小和持续时间似乎差别不大
    • 更轻松、更快速的测试周期
    • 我可以保留所有开发人员.在我的开发中循环.机器

推荐答案

静态链接的名声不好.这些天我们有巨大的硬盘驱动器,以及非常胖的管道.许多支持动态链接的旧论点现在都不那么重要了.

Static linking has a bad rap. We have huge hard drives these days, and extraordinarily fat pipes. Many of the old arguments in favor of dynamic linking are way less important now.

此外,在 Linux 上更喜欢静态链接有一个很好的理由:过多的平台配置使得几乎不可能保证您的可执行文件即使在没有静态链接的情况下也能在其中的一小部分工作.

Plus, there is one really good reason to prefer static linking on Linux: The plethora of platform configurations out there make it almost impossible to guarantee your executable will work across even a small fraction of them without static linking.

我怀疑这不会是一个流行的观点.美好的.但是我有 11 年在 Linux 上部署应用程序的经验,并且在 LSB 之类的东西真正起飞并真正扩展其覆盖范围之前,Linux 将继续难以部署应用程序.在此之前,如果您必须在各种平台上运行,请静态链接您的应用程序.

I suspect this will not be a popular opinion. Fine. But I have 11 years experience deploying applications on Linux, and until something like LSB really takes off and really extends it's reach, Linux will continue to be much more difficult to deploy applications on. Until then, statically link your application, if you have to run across a wide range of platforms.

相关文章