Undocumented differences between -M and -MD

J.T. Conklin jtc@acorntoolworks.com
Fri Nov 26 11:53:00 GMT 2010


I happened to look at a gcc generated dependency file at work earlier
today.  The make target (object file) was listed twice, which made me
curious whether the correct set of command line options were being
used.  After reading the info pages and interactively playing with
different sets of options, there seem to be differences in behavior
between -M and -MD that aren't explained in the docs.

In my case, the dependencies are generated as side effects of the 
build using options like:

    gcc -MD -MF obj/foo.d -MT obj/foo.o -o obj/foo.o -c src/foo.c

This yielded a obj/foo.d like:

    obj/foo.o obj/foo.o: src/foo.c src/foo.h

Note the duplicate obj/foo.o.

If the -MT option is not used, obj/foo.d is:

    obj/foo.o: src/foo.c src/foo.h

Which is what I expected.  However, the documentation for -MT states:

    "Change the target of the rule emitted by dependency generation.
    By default CPP takes the name of the main input file, deletes any
    directory components and any file suffix such as '.c', and appends
    the platform's usual object suffix.  The result is the target.

    An '-MT' option will set the target to be exactly the string you
    specify."

Neither output was consistant with this description.  In the first, I
would have expected only the one make target set on the command line.
In the second, I would have expected the target to be "foo.o" (since
"any directory components" are said to be deleted).

After further investigation, I discovered the documented behavior 
does seem to match when -M is used instead of -MD.

    gcc -M -MF obj/foo.d -MT obj/foo.o src/foo.c

Resulted in a obj/foo.d like:

    obj/foo.o: src/foo.c src/foo.h

as expected, and removing the -MT yeilded:

    foo.o: src/foo.c src/foo.h

which is also consistant with the documentation.


I think the current behavior of -MD where the object directory prefix
is used in the dependency file make target is useful.

The text for -M already describes the object file name that will be
used for the target name.  The text for -MT is similar.  Perhaps that
should be removed, and new text added for -MD that describes how the
object file name is created when that option is used.

That leaves not being able to override the target with -MT (or -MQ)
with -MD.  This seems more like a bug than a documentation problem to
me.  Just to be sure it hadn't already been reported/fixed, I built
gcc-4.5.1, re-ran the first example, and found that -MT now replaces
the make target as expected.

So after all this, I guess the original options are just fine with a
new enough gcc; but the docs could use some touch up.

    --jtc

-- 
J.T. Conklin



More information about the Gcc-help mailing list