kunzaZa183's blog

By kunzaZa183, history, 3 months ago, In English

Hi everyone,

I'm a big fan of Visual Studio Code. However, I'm not a big fan of the C/C++ intellisense provided by Microsoft. Although I am aware it works on other machines; on my machine, it is a slow and buggy extension that seems to hallucinate more often than not, which led me to switch to clangd as my primary C/C++ autocomplete extension. I've been using clangd for quite a while and I like it. One annoying thing I want to fix though, is the lack of inclusion of headers that are provided by GCC.

Headers like bits/stdc++.h, ext/pb_ds aren't supported by clang, but I would like to know how I could be able to use these headers with clangd on vscode. I remember that during IOI2024, the clangd extension provided by the host allowed me to use these headers, so I know that it is possible.

  • Vote: I like it
  • +8
  • Vote: I do not like it

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by kunzaZa183 (previous revision, new revision, compare).

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I did someting similar for my non vscode setup. My understanding is that clangd uses a clang/clang++ binary to parse and understand code. After you find that binary you can try "/your/clang/location/clang++ -v foo.cpp" to figure out the exact include paths of clang++. Once you find those paths you should manually add the headers you want to use.

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

You can configure clangd to additionally use GCC’s header search paths as follows:

  1. Make clangd use the --query-driver=<globs> flag, where <globs> is a comma-separated list of globs at least one of which matches the g++ you want to use (see https://clangd.llvm.org/guides/system-headers#query-driver). You can probably set this option in VS Code’s settings for the clangd extension. I use ST4, on macOS, so I use "initializationOptions": { "clangd.query-driver": "/opt/homebrew/bin/gcc-*,/opt/homebrew/bin/g++-*" } in my LSP-clangd.sublime-setings file to tell clangd to use the search paths of my Homebrew installation of GCC.

  2. Then in your .clangd file, set Compiler (under CompileFlags) to the path of a G++ that matches the glob you used with --query-driver (see https://clangd.llvm.org/config#compiler). E.g., I use g++-14.

I have found this method to be the only way to make clangd recognize the <bits/…> headers on macOS. Manually adding the location of the parent directory of bits/stdc++.h with -isystem<path> did not work for me.