Search code examples
c++compilationclangd

clangd-14 cannot find c++ headers


How can I make clangd find c++ libs?

Here is a simple example, given this cpp main file

// main.cpp
#include <string>

int main() {

 return 0;   
}

I am generating the compile_commands.json db using bear:

$ bear -- g++ main.cpp

This gives the following compile_commands.json:

[
  {
    "arguments": [
      "/usr/bin/g++",
      "-c",
      "main.cpp"
    ],
    "directory": "/tmp/test",
    "file": "/tmp/test/main.cpp"
  }
]

If I want to check if clangd-14 would work on this small project, it can't find <string>

$ clangd --check=main.cpp 
I[00:26:23.073] Ubuntu clangd version 14.0.0-1ubuntu1
I[00:26:23.073] Features: linux+grpc
I[00:26:23.073] PID: 32847
I[00:26:23.073] Working directory: /tmp/test
I[00:26:23.073] argv[0]: clangd
I[00:26:23.073] argv[1]: --check=main.cpp
I[00:26:23.073] Entering check mode (no LSP server)
I[00:26:23.073] Testing on source file /tmp/test/main.cpp
I[00:26:23.073] Loading compilation database...
I[00:26:23.073] Loaded compilation database from /tmp/test/compile_commands.json
I[00:26:23.074] Compile command from CDB is: /usr/bin/g++ --driver-mode=g++ -c -resource-dir=/usr/lib/llvm-14/lib/clang/14.0.0 -- /tmp/test/main.cpp
I[00:26:23.074] Parsing command...
I[00:26:23.076] internal (cc1) args are: -cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -fcoverage-compilation-dir=/tmp/test -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++ -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/x86_64-linux-gnu -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir=/tmp/test -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -x c++ /tmp/test/main.cpp
I[00:26:23.076] Building preamble...
I[00:26:23.088] Indexing headers...
E[00:26:23.089] [pp_file_not_found] Line 1: 'string' file not found
I[00:26:23.089] Building AST...
I[00:26:23.096] Indexing AST...
I[00:26:23.096] Testing features at each token (may be slow in large files)
I[00:26:23.097] All checks completed, 1 errors

How can I make clangd to work with c++ headers?

$ clangd --version       
Ubuntu clangd version 14.0.0-1ubuntu1
Features: linux+grpc
Platform: x86_64-pc-linux-gnu

Solution

  • If you're on Ubuntu 22.04, this is a common issue.

    The easiest workaround is to install the package libstdc++-12-dev, as discussed in this answer.

    Additional discussion, including other possible workarounds, and discussion of what a proper fix might look like on the clangd side, can be found in this clangd issue (see in particular this comment).