Why CMake pass '-MD -MT' flags to gcc c++ and clang++ while compiling on Linux?

To my knowledge, these seem to be MSVC’s runtime flags. I could not find them in GCC or clang’s manuals.

These flags are used to generate dependencies.
See GCC Preprocessor options.

2 Likes

Regarding the purpose of the generated dependency files, they are used so that generators (e.g. make, ninja) recompile files only when their included headers are modified.

There is a nice answer in stack overflow: gcc - purpose of creating DEPENDENCIES_OUTPUT file while compiling c program

Not quite right. They are there to additionally recompile the file when the included headers change. It may be recompiled for a variety of other reasons (flag change, source itself changes, etc.).