VScode clangd failed to find my header file not found `clang(pp_file_not_found)`

huangapple go评论75阅读模式
英文:

VScode clangd failed to find my header file not found `clang(pp_file_not_found)`

问题

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 giving 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:

  • OS: macOS ventura 13.0
  • clangd: 16.0.3
  • CMake: 3.26.3

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.

英文:

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&#39; file not found clang(pp_file_not_found)
VScode clangd failed to find my header 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 &lt;forehead/include.h&gt;

But the exactly same line in graph.h, clangd keeps give me the error:forehead/include.h&#39; 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:

  • OS: macOS ventura 13.0
  • clangd: 16.0.3
  • CMake: 3.26.3

root/CMakeLists.txt:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

vscode/setting.json:

  &quot;clangd.path&quot;: &quot;/opt/homebrew/opt/llvm/bin/clangd&quot;,
  &quot;clangd.arguments&quot;: [&quot;--header-insertion=never&quot;],
  &quot;clangd.serverCompletionRanking&quot;: 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.

答案1

得分: 2

Clangd通过部分编译您的文件并解析AST来工作。对于源文件(.cpp/.c等),它直接编译该文件,这应该没问题。

对于头文件,它无法直接编译,所以目前的方法似乎是选择一个应该与您的头文件匹配的源文件。问题是,这是基于启发式方法的,经常失败,找到您的依赖项或其他地方的无意义文件(请参见 https://github.com/clangd/clangd/issues/519https://github.com/clangd/clangd/issues/123)。

如果这是您的问题,您可以通过在VSCode上打开“clangd”语言服务器输出,并查找类似以下行的内容来诊断它:

ASTWorker正在构建文件 e:\your\folder\forehead\include.h 版本1,命令是从E:\somewhere\else\Include.cpp 推断出的

这非常令人讨厌,我认为没有干净的解决方法,除非也许将您的文件和某个匹配的 .cpp 文件重命名为更具特色的名称,例如 forehead_include.h 并在其旁边有一个相关的 forehead.cpp?否则,像 include.h 这样的名称可能与其他地方的随机文件配对。

编辑:我还尝试使用引号和相对路径包含,例如 #include &quot;../forehead/include.h&quot;,取得了部分成功。

英文:

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 https://github.com/clangd/clangd/issues/519 or https://github.com/clangd/clangd/issues/123).

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 &quot;../forehead/include.h&quot;

huangapple
  • 本文由 发表于 2023年5月15日 10:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定