Should I link to the Visual Studio C runtime statically or dynamically?

前端 未结 4 1715
离开以前
离开以前 2020-12-01 01:21

I have read arguments on both sides about whether one should link to the C runtime library statically or dynamically in Visual Studio projects, and I\'m still not entirely s

相关标签:
4条回答
  • 2020-12-01 01:25

    The only time you'll get multiple copies of the runtime is when you statically link a library into a DLL - each DLL will get a copy, and so will the exe. If they're all static libraries and not DLLs, they'll all link together and all of your libraries will share the same runtime.

    That's the linker's job.

    0 讨论(0)
  • 2020-12-01 01:26

    ... Do it statically... attempts to fix DLL Hell haven't worked out that well... just add an extra 200k to your installation with the static linkage.

    0 讨论(0)
  • 2020-12-01 01:39

    Linking statically will bloat all your EXEs and DLLs, and can cause crashes (eg. if code in one DLL calls free() with a pointer allocated by malloc() in a different DLL).

    You can get the best of both worlds by linking dynamically and deploying the runtime DLLs as private assemblies. This simply means putting a copy of a specially-named directory containing the runtime DLLs and their manifests next to your executable.

    See the section "Deploying Visual C++ library DLLs as private assemblies" at http://msdn.microsoft.com/en-us/library/ms235291(VS.80).aspx for details, but basically your application looks like this:

    c:\Program Files\My App\MyApp.exe
    c:\Program Files\My App\MyLibrary.dll
    c:\Program Files\My App\Microsoft.VC80.CRT\Microsoft.VC80.CRT.manifest
    c:\Program Files\My App\Microsoft.VC80.CRT\msvcr80.dll
    

    As to your last question, yes, the target machine needs the correct versions of the runtime DLLs to work, but by deploying them as private assemblies, you guarantee that.

    Another benefit is that non-admin users can install your app (not into Program Files, but elsewhere) - they don't need permission to write files into the WinSxS area.

    0 讨论(0)
  • 2020-12-01 01:43

    Static libraries don´t need to be statically linked to other static libraries. You only need to link all static libraries in your main project. That way the compiler will not complain about multiple symbols.

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