Is "g++ -MMD" better than include scanning?

ACyclic picture ACyclic · Aug 24, 2012 · Viewed 9.1k times · Source

Whilst looking at build systems, a lot of them (SCons, bjam, cmake, Tundra, etc) have a built-in #include scanner. Yet gcc & icc offer a -MMD (or -MD) option which outputs the names of the header files that the C++ file depends upon.

The -MMD dependency option seems to be reliable. If you add a #include to a C file, its timestamp would change so the build system would recompile it. If you add a #include to a header file, its timestamp would change and it would recompile all affected C files.

Include scanning systems break, but -MMD would seem to me to be robust. Which is best, and why?

Answer

Jonathan Wakely picture Jonathan Wakely · Aug 24, 2012

-MMD is best, for the reasons you give and more.

Getting the compiler to output dependencies as part of the normal compilation process ensures that the exact same set of compiler options such as -I paths and macros are in effect for compilation and when finding dependencies. That's less redundant and less error-prone than ensuring the same options are used for two separate tools.