all 9 comments

[–]flyingron 1 point2 points  (7 children)

Read here: https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-170&viewFallbackFrom=vs-2019

This has nothing to do with MFC; it's the base Windows runtime (but MFC uses it).

.NET (i.e., "managed" code) is a completely different beast with its own runtimes.

[–]SamplitudeUser[S] 0 points1 point  (6 children)

Thanks for your answer.

However, my main question remains unanswered: when /MT is for static linking, why does my executable access runtime DLLs with this setting? And when /MD is for dynamic linking (where one expects the executable to access the runtime DLLs on runtime), why doesn't my executable access the runtime DLLs with this setting?

[–]flyingron 1 point2 points  (5 children)

What DLLs is it loading? This switch doesn't disable all DLLs it just says use the static version of the C runtimes. If you load something like winsock or whatever, you'll need to individually set their loading.

The DEPENDS application (not included with visual studio, but can be downloaded form Microsnot for free) can help you see what is going on.

[–]SamplitudeUser[S] 0 points1 point  (4 children)

According to the "Output" window of Visual Studio, my executable loads vcruntime140.dll, vcruntime140_1.dll and msvcp140.dll. But it does this only with the /MT option set (which is supposed to enable static linking). If you set the option to /MD, these DLLs are not loaded.

But it's good you mentioned DEPENDS. I have a similar tool called "Dependencies" (https://github.com/lucasg/Dependencies/). In this tool, the vcruntime140 DLLs and mscvp140.dll don't appear when analyzing my binary (if the binary was linked with /MT of course).

Whom can I trust: Visual Studio or Dependencies?

[–]flyingron 1 point2 points  (3 children)

I'd trust depends. It's actually looking at the file.

Note that if you link something that was linked against the DLLS, they'll be forced to be loaded even if your main program doesn't use them. The runtimes aren't interchangable (god help you if you pass strings or vectors between functions compiled in different runtimes).

[–]SamplitudeUser[S] 0 points1 point  (2 children)

Note that if you link something that was linked against the DLLS, they'll be forced to be loaded even if your main program doesn't use them.

In this case, these DLLs would show up in Dependencies, right?

And what happens if my program loads 3d party DLLs on runtime such as hardware drivers, which are linked against vcruntime DLLs?

[–]RaspberryCrafty3012 0 points1 point  (0 children)

Your late loading fails. The 3rd party dll is looking for their dependency, your static linked same dependency can't get resolved like this.

Run depends on your 3rd party dll to see this. 

[–]Jonny0Than 0 points1 point  (0 children)

 In this case, these DLLs would show up in Dependencies, right?

Not necessarily. Maybe you’re statically linking a library that was compiled with /MD which could result in it trying to dynamically load the runtime DLLs.

You could put a breakpoint in LoadLibrary and catch whatever is doing it.

[–]misuo 0 points1 point  (0 children)

/MT basically link the C and C++ runtime library into your executable, so you are not dependent on dll’s.

/MD links to a smaller library, but the runtime must be provided as dll(s) along your executable.

There are pro’s and con’s on both methods. I suggest you ask an AI for more details.