skip to Main Content

This question has been asked and answered a bunch of times, but none of the answers seem to work for me.

I’ve been trying to get clangd set up in nvim lsp. I used bear to generate compile_commands.json, but clangd still gives me errors telling me it can’t find standard library headers.
Here’s a minimal example:

main.cpp:

#include <iostream>
using namespace std;

int main(){
  cout << "hello clangd";
  return 0;
}

I then run: bear -- g++ main.cpp, which compiles and creates a compile_commands.json with this content:

[
  {
    "arguments": [
      "/usr/bin/g++",
      "-c",
      "main.cpp"
    ],
    "directory": "/home/xxx/tmp/hello_clangd",
    "file": "/home/xxx/tmp/hello_clangd/main.cpp"
  }
]

I also tried compiling using a cmake flag to generate compile_commands.json but I’m getting the same issue. I can get the file but the language server still won’t work properly.

I have been able to use clang with vim-pio so it seems it’s not broken. what am I missing.

EDIT: I’m on ubuntu btw

2

Answers


  1. Chosen as BEST ANSWER

    I have found a solution myself using clang++ using the instructions here. The command that works for me is:

    bear -- clang++ -I/usr/include/c++/11 -I/usr/include/x86_64-linux-gnu/c++/11 -L /usr/lib/gcc/x86_64-linux-gnu/11 main.cpp
    

    It's still a bit confusing to me so I'm open to better solutions and explanations.


  2. I had a similar issue on Pop!_OS 22.04 LTS using lunarvim 1.2
    and Clang++/Clangd seems to look for the newest available libraries, so instead of parsing the "11" directory (which contained "libstdc++"), it parses the "12" directory (which did not contain "libstdc++") for the libraries.

    ls /usr/lib/gcc/x86_64-linux-gnu/
    
    11  12
    

    I searched for the version I needed.

    apt search libstdc++
    

    Problem was solved after installing the "libstdc++" for gcc version 12 from the apt repository.

    apt install libstdc++-12-dev
    

    I used this post to solve the issue

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search