DEV Community

BRATAN CORPORATION
BRATAN CORPORATION

Posted on

What I check when switching CMake presets in VS Code

A CMake preset can change the compiler, language standard, include paths or definitions. If the editor is still reading a compilation database from the previous build directory, it may underline valid code or miss an error that the build catches. Here is a quick way to check where the two configurations diverge.

Find the database for the build you actually ran

For a CMake project using Ninja, enable CMAKE_EXPORT_COMPILE_COMMANDS and look for compile_commands.json in the build directory. CLAP enables this export for clap cmake .. In a local test of CLAP 0.43.0 on Ubuntu 26.04 under WSL, I confirmed that the generated sample contained both build/build.ninja and build/compile_commands.json.

Open the entry for the source file showing the problem. Check its working directory and look for one flag you expect the preset to change. A project-specific -D definition is a good starting point. If the flag is missing from this database, inspect the CMake configuration and regenerate before changing editor settings.

Point the editor at that same file

In VS Code with Microsoft’s C/C++ extension, check the Compile Commands setting for the workspace folder that owns the file. When two presets use different build directories, make sure this setting points at the new database after switching. If both roots contain a file with the same name, use the full path to check which database entry you found.

To test the connection, use a small source file with an #ifdef for a macro that changes between presets. Build with each preset, switch the database, and check whether the editor marks the same branch active as you expect from the compiler flags. This is a proposed diagnostic exercise. The WSL check above verified database generation for the sample.

CLAP compilation database notes · Microsoft C/C++ configuration reference

This article was prepared with AI assistance.

Top comments (0)