静态或动态链接 CRT、MFC、ATL 等

2022-01-11 00:00:00 linker c++ mfc

早在 90 年代,当我第一次开始使用 MFC 时,我曾经动态链接我的应用程序并发布相关的 MFC DLL.这给我带来了一些问题(DLL 地狱!),我改用静态链接 - 不仅适用于 MFC,还适用于 CRT 和 ATL.除了更大的 EXE 文件之外,静态链接从来没有给我带来任何问题――那么其他人遇到过任何缺点吗?是否有充分的理由再次访问动态链接?我的应用现在主要是 STL/Boost FWIW.

Back in the 90s when I first started out with MFC I used to dynamically link my apps and shipped the relevant MFC DLLs. This caused me a few issues (DLL hell!) and I switched to statically linking instead - not just for MFC, but for the CRT and ATL. Other than larger EXE files, statically linking has never caused me any problems at all - so are there any downsides that other people have come across? Is there a good reason for revisiting dynamic linking again? My apps are mainly STL/Boost nowadays FWIW.

推荐答案

有一些缺点:

  • 更大的 exe 大小(尤其是如果您发送多个 exe)
  • 使用依赖或假定动态链接的其他 DLL 的问题(例如:您无法作为静态库获取的第 3 方 DLL)
  • 具有独立静态链接(无跨模块分配/释放)的 DLL 之间的不同 c 运行时
  • 无法自动为共享组件提供服务(无法让第 3 方模块供应商更新其代码以解决问题,而无需重新编译和更新您的应用程序)

我们为我们的 Windows 应用程序进行静态链接,主要是因为它允许 xcopy 部署,这只是通过安装或依赖 SxS DLL 的方式是不可能的,因为该过程和机制没有很好的文档记录或易于远程处理.如果您在安装目录中使用本地 DLL,它会起作用,但它没有得到很好的支持.无法在不通过远程系统上的 MSI 的情况下轻松进行远程安装是我们不使用动态链接的主要原因,但是(正如您所指出的)静态链接还有许多其他好处.各有利弊;希望这有助于列举它们.

We do static linking for our Windows apps, primarily because it allows xcopy deployment, which is just not possible with installing or relying on SxS DLL's in a way which works, since the process and mechanism is not well documented or easily remotable. If you use local DLL's in the install directory it will kinda work, but it's not well supported. The inability to easily do remote installation without going through a MSI on the remote system is the primary reason why we don't use dynamic linking, but (as you pointed out) there are many other benefits to static linking. There are pros and cons to each; hopefully this helps enumerate them.

相关文章