c++includellvminclude-pathclangd

Add missing includes without path


I am using clangd with VSCode. Clangd offers the possibility to add missing includes. When doing so, it (sometimes? couldn´t figure out when) automatically adds the relative or absolute path to the include directive.

The latter causes problems when compiling on different OS's (in my case Linux and Termux (on my mobile)) as the absolute (system) paths are obviously different.

For example, instead of

#include "Header.h"

it will add

#include "/home/user/path/to/the/Header.h"

Of course I can manually delete the path, but I wonder:

Does someone know if there is a possibility to disable this behavior and make it omit the path? I tried to google it, but unfortunately I couldn't find anything...

Thanks in advance

Edit: Minimal example

Structure:

./
- .clangd
- compile_commands.json
- bin/
- build/
- include/
--- Header.h
--- Subheader.h
- src/
--- main.cpp`

Contents:

// .clangd
Diagnostics:
  UnusedIncludes: Strict
  MissingIncludes: Strict
// Subheader.h
#ifndef IG20240815185417
#define IG20240815185417

struct Subheader {
    int i{2};
};

#endif
// Header.h
#ifndef IG20240520170135
#define IG20240520170135
    
#include "Subheader.h"
    
struct Header {
  Subheader sh{};
};

#endif
// main.cpp
#include "Header.h"

int main() {
  Header h{};
  Subheader sh{};
  return 0;
}

Compile command (with compile_commands.json creation):

clang++ -o build/main.o -c src/main.cpp -MJ build/main.o.json -I./include && clang++ -o bin/main.exe build/main.o && sed -e '1s/^/[\n/' -e '$s/,$/\n]/' build/*.o.json > compile_commands.json

Note that compile_commands.json will be created via the compile command. If it does not work on your machine, here is the basic compile command and the final content of the .json file. You will have to adjust the directory to match your system!

Compile command (without compile_commands.json creation):

clang++ -o build/main.o -c src/main.cpp -I./include && clang++ -o bin/main.exe build/main.o
// compile_commands.json
[
{ "directory": "/ABSOLUTE/PATH/TO/WORKING/DIRECTORY", "file": "src/main.cpp", "output": "build/main.o", "arguments": ["/usr/bin/clang++", "-xc++", "src/main.cpp", "-o", "build/main.o", "--driver-mode=g++", "-c", "-I", "./include", "--target=x86_64-pc-linux-gnu"]}
]

Observation:

When viewing main.cpp (in VSCode) it shows a blue squiggle line below Subheader, hinting that the respective header is missing / not directly included (but known via Header.h). When choosing the QuickFix option, it adds the include-directive like this: #include "/path/to/working/directory/include/Subheader.h"


Solution

  • As there seems to be no solution so far, I reported a bug here.

    I followed HolyBlackCats suggestion:

    I removed UnusedIncludes: Strict from .clangd, after which it won't show any hint or quickfix option at all. Therefore unfortunately this does note solve the issue.

    Note: When hovering the Subheader type keyword, the hover info panel states: provided by: "/path/to/working/directory/include/Subheader.h". Hovering over Header instead states: provided by: "Header.h".

    Edit: I just found out that removing the MissingIncludes: Strict line, but keeping the UnusedIncludes: Strict produces the expected results