compile_commands.json
loggerThis is a simple
MSBuild logger
that emits a
Clang-style compile_commands.json
file
by observing the MSVC compiler invocations when a C++ project is built. It is particularly useful
with Visual Studio Code's C/C++ extension, which
can be configured
to use compile_commands.json
to determine the compiler options (include paths,
defines, etc.) for accurate IntelliSense.
Building the project is straightforward:
dotnet build
Then, invoke MSBuild with the -logger
option.
For example:
msbuild -logger:/path/to/CompileCommandsJson.dll MyProject
By default, compile_commands.json
is written in the current directory. You can
control the output path using a parameter, e.g.:
msbuild -logger:/path/to/CompileCommandsJson.dll;my_new_compile_commands.json MyProject
There are two significant design limitations:
The logger will only emit entries for compiler invocations that it observes during a build; in particular, for an incremental build, there will be no output for any targets that are considered up to date.
The logger truncates the JSON file at startup, and writes to it incrementally throughout the build.
Thus, for an accurate result you should use this logger only on a completely clean build, and to avoid confusing tools (such as VSCode) that may observe the file as it is written, you should probably write the output to a temporary file and rename it only after the build succeeds. Typical usage is roughly:
rm -r out
msbuild -logger:CompileCommandsLogger.dll;cctmp.json
mv cctmp.json compile_commands.json
Andrew Baumann