compile_commands.json file, a JSON database of compile commands, using CMake isn’t something I run into as a web developer. But as somebody that’s recently gotten bitten by the game development bug, this was one of the first things I happened to have run into.
I’m not going to bore you too much with the details of my foray into game development. I’m trying to build a few small games, think Atari-era arcade games. The plan is to release them on Steam, and I’m focusing heavily on the native Steam Deck’s native screen resolution of 1280 by 800, as that will be my primary consumption device for these games.
Coincidentally, I’ve written games in the past, using web technologies as well as writing native games for Android and iOS. For a change of pace, I’m focusing on learning more about C++ this time around.
Syntax errors for days
Armed with my favorite text editor, Neovim, I set out to write my game. And within a few minutes, my syntax checking plugin, ALE, was not very happy at all:
The errors make sense, as it’s barking about things that aren’t explicitly in my code. In Node.js and TypeScript, developers must define exceptions for implied global variables, which I’m no stranger to.
But how the heck do I do this with a C++ program?
During some research, I found the missing piece to making
clangd and other C++ syntax checkers happy would be to provide it with a JSON database of compile commands. This typically lives in a file named
compile_commands.json, which can be generated when building your project.
While I had read that this file needs to be in the root of your project, I found that it was sufficient enough to leave the file inside of my build directory
./build. This is a personal choice of mine, but if I were to distribute my code, I’d probably move the file out of the build directory, so that it could be picked up by version control and shared. That way other engineers wouldn’t need to build the program before trying to edit the code.
Configuring CMake to export compile commands
So how does one create this
compile_commands.json file? First, you will need to update your
Alternatively you could pass this option along as arguments to the
cmake command, but that’s out of scope for how I’m building my program.
Finally, with the new option set, we can run our
cmake build commands:
And inside of our build directory, mine being
./build there is now a JSON database of compile commands in the
With the compile commands JSON database created by CMake, my code is now free of all of the noisy errors and I’m free to resolve the actual errors in my code, and continue out my merry way.