c++gccg++g++-4.7

gcc build object and dependency files together


Using gcc 4.7.2 on Ubuntu, I am compiling some source file with this command:

g++ -c -o obj/foo.o foo.cpp -O0 -Wall [.. lots of other args .. ]

That works fine and gives me a reasonable object file, obj/foo.o. However, if I want to also generate the dependency file at the same time:

g++ -c -o obj/foo.o foo.cpp -O -Wall [ .. ] -M -MD -MG -MP -MF obj/foo.dep

Then while I get a totally reasonable looking obj/foo.dep, I get an empty obj/foo.o. There is also no output from compilation at all. Looking at the documentation, it seems that this should work:

-MD
    If -MD is used in conjunction with -E, any -o switch is understood to specify the
    dependency output file (see -MF), but if used without -E, each -o is understood to
    specify a target object file.

    Since -E is not implied, -MD can be used to generate a dependency output file as
    a side-effect of the compilation process.

I am not using -E, so shouldn't this give me both an object file and a dependency output file? How can I determine where the problem is?


Solution

  • Remove the -M and -MG options. From the gcc manpage:

    Passing -M to the driver implies -E, (...)

    ..and -MG requires -M, so you cannot keep it. This shouldn't be a problem because -MG is only important if generated header files are still missing, in which case you wouldn't be able to compile the code yet anyway.