7

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?

Wevah
  • 28,182
  • 7
  • 83
  • 72
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • I found a possible solution. When starting Clangd, the VSCode extension sends some initialization parameters to its stdin. There's an otherwise unexposed parameter ([`initializationOptions.fallbackFlags`](https://clangd.llvm.org/extensions.html)), that lets you specify the custom flags. Now I just need some clever way to modify it on the fly. – HolyBlackCat Apr 10 '20 at 22:07
  • I am on a similar boat. Have you tried running a script that sets an environment variable, and passing that environment variable in as the flags? – Francisco Aguilera May 19 '21 at 21:14
  • @FranciscoAguilera I don't fully understand the idea, but now the VSC extension now lets you specify the fallback flags directly in the config, so this shouldn't be an issue anymore. – HolyBlackCat May 19 '21 at 21:46
  • when you say the config do you mean the `.clangd` file? Or VS Code `settings.json` as `"clangd.fallbackFlags": [ "%CLANGD_FALLBACK_FLAGS%" ],`? For my purposes I need to parse flags out of a `compile_commands.json` entry and use those as fallback flags. – Francisco Aguilera May 19 '21 at 22:10
  • Yes, setting.json. *"I need to parse flags out of a compile_commands.json"* Oh, I see. Might not work then. I'd try to amend compile_commands.json – HolyBlackCat May 20 '21 at 05:49
  • @FranciscoAguilera I think you can have override project-specific config by creating a separate `settings.json` in the `.vscode` folder. You could put your flags there. – HolyBlackCat May 20 '21 at 06:24

2 Answers2

4

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"].

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 1
    For reference: The exact same option works when using `clangd` via [CoC](https://github.com/neoclide/coc.nvim) (coc-clangd) in NeoVim (presumably also regular Vim) – Raven Jan 11 '23 at 11:36
2

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111