Description:
I'm using clangd as my language server for coding c/c++ with CMake in vscode.
However, I'm experiencing some annoying bugs that keep interrupting me.
forehead/include.h' file not found clang(pp_file_not_found)
Bugs Reproduction:
I got a directory called include
which looks like this:
.
├── CMakeLists.txt
├── forehead
│ └── include.h
├── graph
│ └── graph.h
├── list
│ ├── linklist.h
...
In linklist.h
, clangd works fine with no errors or warnings, and I can jump to file include.h
with gd
:
#include <forehead/include.h>
But the exactly same line in graph.h
, clangd keeps give me the error:forehead/include.h' file not found clang(pp_file_not_found)
, preventing me from jumping to that file, every keyword or function from include.h
are treated as errors by clangd.
Environment:
root/CMakeLists.txt
:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
vscode/setting.json
:
"clangd.path": "/opt/homebrew/opt/llvm/bin/clangd",
"clangd.arguments": ["--header-insertion=never"],
"clangd.serverCompletionRanking": true,
Expectation:
It is worth mentioning that I can navigate within Vim and successfully build the .o
and executable files using CMake.
I'm tired of dealing with this annoying bug that keeps popping up every time I write a new header file.
(BTW: The bug may disappear for no apparent reason, perhaps the next time I open VSCode -- However, deliberately restarting the clangd or reloading the VSCode window does not seem to be effective.
Clangd works by partially compiling your file and parsing the AST. For source files (.cpp/.c/etc.) it just compiles that file directly, which should be fine.
For header files, it can't compile them directly, so it seems the current approach is to pick a source file that should match your header file. The problem is that this is based on heuristics and frequently fails, finding nonsense files inside your dependencies or elsewhere (see this issue or this one.
If this is your issue, you can diagnose it by opening the "clangd" language server output on VSCode, and looking for a line like this:
ASTWorker building file e:\your\folder\forehead\include.h version 1 with command inferred from E:\somewhere\else\Include.cpp
This is very annoying and I don't think there's a clean way around it.. except maybe renaming your file and some matching .cpp file to something more distinctive, like forehead_include.h
and having a relevant forehead.cpp
next to it? Otherwise a name like include.h
may be paired with random files somewhere else.
Edit: I have also had some partial success trying to include with quotes and relative paths, like #include "../forehead/include.h"