How to export `compile_commands.json`?

When I use Zig to build a C++ project, I want better code hints in my editor. For CMake-based projects, I can generate a compile_commands.json file by adding the following code to the build configuration:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

I would like to know if Zig provides a similar mechanism.

3 Likes

I have very recently started looking at C++, so I’m still deeply unfamiliar with the ecosystem, but I use the Zig build system for my experimentation repo. I have a config.zig file, as a single source of truth for everything, and then my build.zig pulls that in and creates or updates .clang-tidy and compile_flags.txt for me. I don’t know the compile_commands.json format, so maybe that requires a lot more machinery or something, but perhaps looking at my example helps.

I vaguely remembered there being a project that does that exactly, and I found it:

FYI, A proposal is currently under development:
https://codeberg.org/ziglang/zig/pulls/35462

1 Like