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 Visual Studio Code, 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.
I have also had some partial success trying to include with quotes and relative paths, like #include "../forehead/include.h"
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 Visual Studio Code, 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.
I have also had some partial success trying to include with quotes and relative paths, like #include "../forehead/include.h"
There is one more workaround. It is quite ugly, but it helps. One can add paths into the .clangd file, in the CompileFlags section, Add subsection (link to the documentation). For example:
CompileFlags:
Add:
... # some already existed flags
- -I<path_1>
- -I<path_2>
- -I<path_3>
... # and so on
These paths can be added manually, or with a script that extracts them from the compile_commands.json. Write with quotes "-I<path_with_space>" in case of a path with spaces.
The drawback of this approach is that clangd adds these flags for all clang++ calls. Therefore we won’t know some build configuration issues for some source files until we start the build. But hints in VS Code will work fine.
cmake - clang in neovim giving pp_file_not_found error for c++/pytorch basic example - Stack Overflow
System include extraction successful, yet still `clang(pp_file_not_found)`
SDL pp_file_not_found
Some system include paths are missing, and result in a header not found error
Since the compilation succeeds with cmake-make, it is possible to ask CMake to generate the compilation database (typically a compile_commands.json file) with cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1.
Once the file is available, it can be read by the autocompletion compiler (clang in this case), which prevents the autocompleter from being lost in the filesystem, because all flags and headers are specified for all compilation units. The compilation database just needs to be put where the autocompletion plugin expects it to be.
You can add at the beginning of your CMakeList.txt
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Or as a cache variable in a CMakePresets.json file if you use one :
{
"version": 1,
"configurePresets": [
{
"name": "debug",
"displayName": "Debug",
"binaryDir": "${sourceDir}/build",
"generator": "Unix Makefiles",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"CMAKE_CXX_FLAGS_INIT": "-std=c++17 -g -Wall -Wextra -Wpedantic -Werror -Weffc++ -Wshadow -O0"
}
},
{
"name": "release",
"displayName": "Release",
"binaryDir": "${sourceDir}/build",
"generator": "Unix Makefiles",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_CXX_FLAGS_INIT": "-std=c++17 -march=haswell -flto -O3"
}
}
]
}
I downloaded the latest TDM-GCC from jmeubank.github.io/tdm-gcc with default setup. So the files are located in C:\TDM-GCC-64.
I am using the clangd extension from vscode and I am having trouble setting it up. Searching the net I found that I had to add --query-driver=C:\TDM-GCC-64\bin\* to the argument list in clangd.arguments as well as make a file in the project folder named .clangd and have the following in the file
CompileFlags:
Compiler: C:\TDM-GCC-64\bin\g++.exeNow I am getting an issue I have no idea how to find a fix on the net. My code is
#include <bits/stdc++.h>
int main() {
}I am getting the following error
In included file: 'math.h' file not foundclang(pp_file_not_found) cmath(45, 15): Error occurred here
The line in question is
#include_next <math.h>
I can comment this out and it just goes to the next error which is
In included file: 'stdlib.h' file not foundclang(pp_file_not_found) std_abs.h(38, 15): Error occurred here
which is
#include_next <stdlib.h>
I tried searching for include_next issues with clangd but they are all unresolved. So idk how to fix this.
Any help would be appreciated :)
I am writing a code about openMP in c++ and when i type
#include <omp.h> i get the omp.h file not found error but when i compile it everying work without problems why this happen? and how can i solve It !