c++g++neovimclangd

clangd doesn't recognize standard headers


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


Solution

  • 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