Static or dynamic linking the CRT, MFC, ATL, etc

前端 未结 7 744
一整个雨季
一整个雨季 2020-12-10 11:19

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 switch

相关标签:
7条回答
  • 2020-12-10 11:42

    As long as you keep your usage limited to certain libraries and do not use any dll's then you should be good.

    Unfortunately, there are some libraries that you cannot link statically. The best example I have is OpenMP. If you take advantage of Visual Studio's OpenMP support, you will have to make sure the runtime is installed (in this case vcomp.dll).

    If you do use dll's then you can't pass some items back and forth without some serious gymnastics. std::strings come to mind. If your exe and dll are dynamically linked then the allocation takes place in in the CRT. Otherwise your program may try to allocate the string on one side and deallocate it on the other. Bad things ensue...

    That said, I still statically link my exe's and dll's. It does reduce a lot of the variablilty in the install and I consider that well worth the few limitations.

    0 讨论(0)
  • 2020-12-10 11:42

    No, nothing new on that front. Keep it that way.

    0 讨论(0)
  • 2020-12-10 11:43

    There are some downsides:

    • Bigger exe size (esp if you ship multiple exe's)
    • Problems using other DLL's which rely on or assume dynamic linking (eg: 3rd party DLL's which you cannot get as static libraries)
    • Different c-runtimes between DLL's with independent static linkage (no cross-module allocate/deallocate)
    • No automatic servicing of shared components (no ability to have 3rd party module supplier update their code to fix issues without recompiling and updating your application)

    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.

    0 讨论(0)
  • 2020-12-10 11:44

    One good feature of using dll's are that if multiple processess loads the same dll its code can be shared between them. This can save memory and shorten loading times for an application loading a dll that's already used by another program.

    0 讨论(0)
  • 2020-12-10 11:45

    Most of the answers I hear about this involve sharing your dll's with other programs, or having those dll's be updated without the need to patch your software.

    Frankly I consider those to be downsides, not upsides. When a third party dll is updated, it can change enough to break your software. And these days, hard drive space isn't as precious as it once was, an extra 500k in your executable? Who cares?

    • Being 100% sure of the version of dll that your software is using is a good thing.
    • Being 100% sure that the client is not going to have a dependency headache is a good thing.

    The upsides far outweigh the downsides in my opinion

    0 讨论(0)
  • 2020-12-10 11:53

    There are some software licenses such as LGPL that require you to either use a DLL or distribute your application as object files that the user can link together. If you are using such a library, you'll probably want to use it as a DLL.

    0 讨论(0)
提交回复
热议问题