jlx
jlx

Reputation: 634

VSCode cannot detect C++ system files when clangd is enabled

For example, if I include <string>, it will show an error saying 'string' file not found clang(pp_file_not_found): screenshot of the error message

I am building the project into build folder. And use a .clangd file to specify the CompilationDatabase to where compile_commands.json is. This method is successful in my another PC(macOS). For VScode settings about clangd, I specified the clangd.path.

For this PC(Linux Ubuntu), I tried to add "-std=c++17" to 'clangd.fallbackflags'. But it still does not work.

When I use CMake to compile the whole project, it works well. Seems it is a VSCode issue or clangd issue.

Update: I found that if I use #include <9/string> instead of #include <string>, vscode can find the file, is that something about include path? However, I have never made changes to it. So I tried to update the include path by this method Visual Studio Code cannot open source file "iostream". Still does not work.

Upvotes: 1

Views: 3903

Answers (2)

jlx
jlx

Reputation: 634

I solved this problem by adding missing libs:

sudo apt update
sudo apt install clang clangd libclang-dev libstdc++-9-dev

Check the installation by:

echo | clang++ -E -x c++ - -v

Reinstall some libs if there is missing path or other issues:

sudo apt install --reinstall libstdc++-11-dev libstdc++-9-dev

Upvotes: 0

HighCommander4
HighCommander4

Reputation: 52847

You might be running into a known issue affecting Ubuntu 22.04: https://github.com/clangd/clangd/issues/1394.

You can read about the available workarounds in this comment, but this simplest one is to install g++-12.


If that's not the issue, my next suggestion is to read these parts of the clangd docs:

and try --query-driver in particular.


If you're still not having any luck, please share clangd logs for further diagnosis.

Upvotes: 2

Related Questions