24

In a C++ Bazel project, I want to use tools like Clang Tools or RTags, etc.

For that I need to generate a compile_commands.json file.

The solution is trivial for CMake with the CMAKE_EXPORT_COMPILE_COMMANDS.

For simple Makefiles you can still use the Bear tool.

However AFAIK there is no built-in solution for C++ Bazel projects.

Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70

2 Answers2

19

Extra information:

  • Bear is currently not working with Bazel: this issue
  • a good documentation concerning compile_command.json can be found here.

Suggested solutions:

After Googling about that I found gist:Basics of generating a compile_commands.json file with Bazel.

It works fine and I have written an automated solution with bash scripts <- my solution

Alternative:

Also on GitHub, you can find:

Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
4

To add to the alternatives listed by other answers...

Hedron's Compile Commands Extractor for Bazel (GitHub) was just released and does what you want, I think. From the README, its goal is to "provide Bazel users cross-platform autocomplete for (Objective-)C(++) to make development more efficient and fun[..., and] export Bazel build actions into the compile_commands.json format that enables great tooling decoupled from Bazel."

Disclaimer: I work for the company that makes hedronvision/bazel-compile-commands-extractor... but I use it every day, and in my personal opinion it is very good!

  • 1
    Thanks would be great if there was an example project somewhere, having difficultly getting it to populate anything other than `[]` in compile_commands.json – Dominic Jan 25 '22 at 12:53
  • As far as I know, there isn't an example project, but the README has a (new?) [section](https://github.com/hedronvision/bazel-compile-commands-extractor#usage) on setting up the tool with an existing project. If that still doesn't work, I'd file an issue in the repo because the author/maintainer is great about assisting new users. – Sam -Hedron Vision- May 24 '22 at 00:35
  • Yep thanks, the author was super friendly and helpful and I got it working – Dominic May 24 '22 at 01:02