Should I compile with /MD or /MT?


In Visual Studio, there's the compile flags /MD and /MT which let you choose which kind of C runtime library you want.

I understand the difference in implementation, but I'm still not sure which one to use. What are the pros/cons?

One advantage to /MD that I've heard, is that this allows someone to update the runtime, (like maybe patch a security problem) and my app will benefit from this update. Although to me, this almost seems like a non-feature: I don't want people changing my runtime without allowing me to test against the new version!

Some things I am curious about:

  • How would this affect build times? (presumably /MT is a little slower?)
  • What are the other implications?
  • Which one do most people use?
4/16/2009 6:22:54 PM

Accepted Answer

By dynamically linking with /MD,

  • you are exposed to system updates (for good or ill),
  • your executable can be smaller (since it doesn't have the library embedded in it), and
  • I believe that at very least the code segment of a DLL is shared amongst all processes that are actively using it (reducing the total amount of RAM consumed).

I've also found that in practice, when working with statically-linked 3rd-party binary-only libraries that have been built with different runtime options, /MT in the main application tends to cause conflicts much more often than /MD (because you'll run into trouble if the C runtime is statically-linked multiple times, especially if they are different versions).

4/16/2009 6:49:54 PM

If you are using DLLs then you should go for the dynamically linked CRT (/MD).

If you use the dynamic CRT for your .exe and all .dlls then they will all share a single implementation of the CRT - which means they will all share a single CRT heap and memory allocated in one .exe/.dll can be freed in another.

If you use the static CRT for your .exe and all .dlls then they'll all get a seperate copy of the CRT - which means they'll all use their own CRT heap so memory must be freed in the same module in which it was allocated. You'll also suffer from code bloat (multiple copies of the CRT) and excess runtime overhead (each heap allocates memory from the OS to keep track of its state, and the overhead can be noticeable).

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow