I've created a very simple C++/CMake project:
CMakeLists.txt (note the -MD flag):
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
add_executable(moop main.cc)
target_compile_options(moop PRIVATE -MD)
main.cc:
#include "moop.hh"
int main( int, char** ) { return 0; }
moop.hh:
#pragma once
From the project root, I run the following:
mkdir build && cd build
cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
cat compile_commands.json
cat compile_commands.json outputs (note the -MD flag):
{
"directory": "/home/zbardoo/moop/build",
"command": "/usr/bin/clang++ -MD -o CMakeFiles/moop.dir/main.cc.o -c /home/zbardoo/moop/main.cc",
"file": "/home/zbardoo/moop/main.cc"
}
If I then run ninja, the executable moop is successfully built. However, moop.cc.d is nowhere to be found. But if I then copy and paste the command value from compile_commands.json and run it:
/usr/bin/clang++ -MD -o CMakeFiles/moop.dir/main.cc.o -c /home/zbardoo/moop/main.cc
The file /home/zbardoo/moop/build/CMakeFiles/moop.dir/main.cc.d appears:
zbardoo@localhost:~/moop/build$ cat CMakeFiles/moop.dir/main.cc.d
CMakeFiles/moop.dir/main.cc.o: /home/zbardoo/moop/main.cc \
/home/zbardoo/moop/moop.hh
Why doesn't ninja obey the -MD flag in the compile_commands.json file?