8

I am working on a project that uses a GCC library (SFML), which is not available for clang, as far as I know. I am using COC with vim for code completions, but for C++ it needs clangd. Is there a way to use GCC as my compiler, but still use the clangd language server?

I have also heard that there may be a way to make clang recognize GCC libraries/headers, but I've never been able to make it work right. If somebody could point me in the right direction there that would be helpfull too. But I'm used to GCC (I've been using it since I started programming C++), so being able to use clangd and GCC would be preferable.

Thaddaeus Markle
  • 434
  • 1
  • 4
  • 12
  • ClangD is a language server, it assists the IDE. Off course, having a compilation with clang next to your GCC would be a good idea, however, if you build your indexes with clangd, you don't need to specifically compile with clang yourselve – JVApen Jun 28 '20 at 15:19
  • How about using `ycm_core` -- YouCompleteMe? – frozenOne Jun 29 '20 at 09:37
  • @forzenOne, yeah, no. I tried once, and it's just not for me. COC (Conquerer of Completion) has been working _way_ better so far. – Thaddaeus Markle Jun 29 '20 at 16:09

3 Answers3

5

Yes it is. I do it with ccls (which is clang based as well).

Given my installation of clang is not the standard one (I compile it, tune it to use libc++ by default, and I install it somewhere in my personal space) I have to inject paths to header files known by clang but unknown by other clang based tools.

I obtain them with

clang++ -E -xc++ - -Wp,-v < /dev/null

Regarding the other options related to the current project, I make sure to have a compile_commands.json compilation database (generated by CMake, or bear if I have no other choice), and ccls can work from there. I expect clangd to be quite similar in these aspects.

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • I have used ccls before, and it worked great! The only problem is that to use it with COC and vim, you have to add it as a custom langauge server, so it doesn't integrate as smoothly. For clangd I could use the coc-clangd extension, so it would work better. – Thaddaeus Markle Jun 29 '20 at 16:07
  • So would the same compile_commands.json work for ccls and clangd? What about the .ccls file? Could it be renamed to compile_flags.txt and work as a drop-in replacement? – Thaddaeus Markle Jun 29 '20 at 16:11
  • IMO, would the same compile_commands.json should work for ccls and clangd. I've no idea for your two other questions as [I inject these directories automatically from my .vimrc](https://github.com/LucHermitte/lh-misc/blob/master/vimrc_core.vim#L1135). – Luc Hermitte Jun 29 '20 at 18:40
  • The .ccls file is equivilent to the command section of compile_commands.json, minus the /usr/bin/clang++, as is the compile_flags.txt. If compile_commands.json would work for the same way, I would assume that compile_flags.txt will too. I will have to test this on my own. – Thaddaeus Markle Jun 29 '20 at 20:49
0

On ubuntu, if you want to use gcc toolchain, you can get the location of your installed compiler by

# Replace gcc with the compiler you want to use
gcc -v -c -xc++ /dev/null

You will see output like this:

  • But you have to only add the lines after starting from this #include <...> search starts here:
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/11
 /usr/include/x86_64-linux-gnu/c++/11
 /usr/include/c++/11/backward
 /usr/lib/gcc/x86_64-linux-gnu/11/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

Now, your ~/.config/clangd/config.yaml will look like this:

CompileFlags:
  Add: [-isystem,/usr/include/,-I/usr/include/c++/11,-I/usr/include/x86_64-linux-gnu/c++/11, -I/usr/include/x86_64-linux-gnu/c++/11,-I/usr/include/c++/11/backward,-I/usr/lib/gcc/x86_64-linux-gnu/11/include,-I/usr/local/include,-I/usr/include/x86_64-linux-gnu,-I/usr/include]

Reference:

-1

Ops, answered the wrong question. But for those who use ccls:

  1. create a .ccls file in your project directory and append --gcc-toolchain=/usr to it.
  2. use this tool to generate a compile_commands.json file

see https://github.com/MaskRay/ccls/wiki/FAQ#compiling-with-gcc

Firer
  • 1
  • 1