Clangd - how to set default flags to be used when there is no `compile_commands.json`?

Viewed 3440

This is probably not possible, but I still want to ask. I'm using Clangd as an autocompletion engine for VSCode. It works great, but there is one problem.

The official Windows binaries of Clang rely on MSVC standard library headers. If MSVC is not installed, Clang and Clangd complain about missing headers.

There's a flag that makes Clang use MinGW's libstdc++ (--target=x86_64-w64-windows-gnu), which I have to include in compile_commands.json.

This solution works, but it would be nice to have sensible autocomplete even without compile_commands.json.

Is there a way I can tell Clangd to assume --target=x86_64-w64-windows-gnu if there is no compile_commands.json?

2 Answers

Is there a way I can tell Clangd to assume --target=x86_64-w64-windows-gnu if there is no compile_commands.json?

From clangd getting started:

compile_flags.txt

If all files in a project use the same build flags, you can put those flags one-per-line in compile_flags.txt in your source root.

Clangd will assume the compile command is clang $FLAGS some_file.cc.

Creating this file by hand is a reasonable place to start if your project is quite simple.

Just create compile_flags.txt file with --target=x86_64-w64-windows-gnu.

At some point after the question was asked, the VSCode Clangd extension started exposing the default flags to the config: "clangd.fallbackFlags": ["--target=x86_64-w64-windows-gnu"].

Related