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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With