0

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) enter image description here

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:

  • 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.

Daniel Hu
  • 31
  • 1
  • 5
  • Is clangd finding your `compile_commands.json` file? Check the [clangd logs](https://clangd.llvm.org/troubleshooting#gathering-logs) if unsure (and if that's not the problem, please feel free to upload the logs for further diagnosis of the issue). – HighCommander4 May 15 '23 at 03:22
  • where is your CMake target definition and `target_include_directories` call? Or even just a `include_directories` call? Is this a header only library? Or what is it? – starball May 15 '23 at 04:17
  • @HighCommander4, thanks! The error message disappeared on its own. And it is not the `compile_commands.json` problem from [CMake-based projects](https://clangd.llvm.org/installation#project-setup). And the logs looks tedious so i'm not gonna put it here.(works while jumping now) – Daniel Hu May 15 '23 at 05:30
  • @user thanks! However, it seems that CMake is not the issue here. I'm writing a data structures library in c and some c++ algorithms(for practice). In case you are interested about my CMake files, I have just pushed the updated code to GitHub.[danielhu19/datastructures-algorithms](https://github.com/danielhu19/datastructures-algorithms.git) – Daniel Hu May 15 '23 at 05:51

1 Answers1

2

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 "../forehead/include.h"

Daniel
  • 678
  • 1
  • 5
  • 20
  • I would like to express my sincere gratitude for providing such a detailed and comprehensive explanation. I am happy to report that changing the file name from `include.h` to `forehead.h` has successfully resolved the issue, with no errors being detected by the clangd tool. Your answer has been invaluable and contributed significantly to my work. Thank you once again for your priceless contribution. – Daniel Hu May 19 '23 at 23:36