Question

There are basically two options for tracking dependencies which are -M and -MM. The difference is that -MM omits system headers and headers included by them.

My question: Why would anyone want to use -M? It inflates the generated .d files drastically, since a system header usually includes a large pack of other system headers. In addition, system headers cannot be built by make, so having them as a depencies yields no benefit. The only little benefit I could see is that - if a required system header is missing - make reports the missing header instead of gcc reporting it. But what is the benefit of this?

To sum things up, I see no reason why -M would be useful at all. Am I missing something? Which scenarios are there that require one to use -M over -MM.

Was it helpful?

Solution

Most header files can't be "built" by make. They're listed as prerequisites so that if they change, then the source code that relies on them is rebuilt. For example, if you install security fix packages on your system and they modify one of the system headers you use, you may want to be sure all your code is rebuilt. These days the backward-compatibility of most base libraries is such that this is not really needed most of the time, I agree.

Also, if you're cross-compiling then your "system" header files are provided to you from the cross-target; these headers might be for an embedded system or similar, and may change (in non-backward-compatible ways) more often than a standard system.

OTHER TIPS

Why would anyone want to use -M?

If the system headers change you want a rebuild to operate accordingly. That is, if your code uses a header, and that header changes, your code should rebuild even if your code didn't change.

Listing headers as dependencies is rarely about 'building' those headers. System headers are no different.

There could be a number of reasons.

Rebuild only necessary parts after system libraries update. Rare thing, but someone may need it.

Get full dependencies list, for some reasons - maybe just drawing dependency graph?

Generate ctags for all potentially used header files.

...

Personally I use it for ctags, so it is not all hypothetical examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top