gcc 同时生成依赖项和对象

gcc make dependency and object simultaneously

我正在使用 gcc 4.8.2,我正在尝试同时构建目标文件和依赖文件。

这个有效:

$ g++ -std=c++11 -MP -MD -c foo.cxx -o foo.o
$ [ -s foo.d ] && [ -s foo.o ] && echo yay
yay

但是,我不想生成 foo.d,而是想生成 foo.D,所以我尝试了:

$ rm foo.d foo.o
$ g++ -std=c++11 -MP -M -MF foo.D -c foo.cxx -o foo.o
$ [ -s foo.D ] && [ -s foo.o ] && echo yay
$

成功生成 foo.D,但生成空 foo.o。为什么? -MDdocumentation 开头为:

-MD is equivalent to -M -MF file, except that -E is not implied.

我不知道为什么 -M -MF foo.D 对您不起作用,我对文档的阅读与您的相同。我认为它应该工作。 strace 的汇编可能会告诉您一些有趣的事情。

但作为一种解决方案,您只需将 -MF foo.D 参数添加到原始命令行即可,这应该可以满足您的需求。

正如 -MF 的文档所说:

-MF file When used with -M or -MM, specifies a file to write the dependencies to. If no -MF switch is given the preprocessor sends the rules to the same place it would have sent preprocessed output

When used with the driver options -MD or -MMD, -MF overrides the default dependency output file.