Clangd cant find system headers with MinGW target?

Hi there folks,

Sorry if this has been answered before, but here goes…

I’m on Windows and using Clang as my compiler with CMake, and my compiler toolchain contains set(CMAKE_CXX_COMPILER_TARGET i686-pc-windows-gnu), which passes --target=i686-pc-windows-gnu argument to Clang, causing it to properly find all the system headers for MinGW.

I asked CMake to generate the compile_commands.json file for me, and I can see that the database contains the --target=... flag; however, when I actually try and use Clangd I get errors that it can’t find any of the standard library header files (It specifically complains about #include <cinttypes>.

Am I doing something wrong here, or is this some kind of known limitation of Clangd?

Thanks!

I’m not on Windows myself to try it, but the usual way to diagnose issues like this is:

  • Look at clangd’s logs. The way to do this depends on the client, e.g. in VSCode, it’s available as a drop-down option in the Output window.
  • Specifically, when you open a file, there should be a line of the form ASTWorker building file <path> version <version> with command [<workingdir>] <command>. See if <command> is what you expect.
  • Try running <command> manually from the command-line (in <workingdir>), and see if you get the same errors you see in the editor. (If so, the issue is with the command and fixing the command should fix clangd.)
  • If not, try additionally replacing the compiler driver in <command> with the clang binary from the same package as clangd. If you now get the errors, you may need to use the --query-driver flag as described here to get clangd to use your actual compiler’s built-in include paths.

If none of these resolves the problem, please feel free to post the complete clangd log and I’m happy to try and help further.

1 Like

Thanks for the reply! It was very helpful :grinning_face_with_smiling_eyes:

Your idea of running the compiler from the working directory worked – I saw the 'stdio.h' file not found message from Clang on my command prompt, even though the exact same command works perfectly fine during the actual compilation.

I figured out the root issue – When I compile with my build system, it sets the PATH environment variable to point to MinGW, and Clang uses that to locate the system headers. When Clangd was running in my IDE, it didn’t have the environment set and was unable to locate the headers.

Hopefully if anyone else encounters this, they can reference this post. Thanks again for the help!

1 Like

Hey everyone I have almost the same problem, clangd can not find any cpp headers like iostream etc. I am using MinGW64 on windows, I’ve tried making compile_flags.txt with content as --target=x86_64-pc-windows-gnu, but no luck.
I am not using any Build system such as CMAKE or Make, MinGW64 is in the system path and I just want clangd to show diagnostics for a single cpp source file.

The following is the output of testing a single file from terminal:

clangd --check=test.cpp
I[03:16:14.953] clangd version 12.0.0 (GitHub - kirillbobyrev/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Note: the repository does not accept github pull requests at this moment. Please submit your patches at http://reviews.llvm.org. 8a844a3a321ea56da21af4d48433ad493b1bcfae)
I[03:16:14.955] PID: 7516
I[03:16:14.957] Working directory: C:\Users\SilentSolitude\Desktop\Projects
I[03:16:14.957] argv[0]: C:\Users\SilentSolitude\Desktop\CP\clangd_12.0.0\bin\clangd.exe
I[03:16:14.958] argv[1]: --check=test.cpp
I[03:16:14.969] Entering check mode (no LSP server)
I[03:16:14.970] Testing on source file C:\Users\SilentSolitude\Desktop\Projects\test.cpp
I[03:16:14.971] Loading compilation database…
I[03:16:14.990] Failed to find compilation database for C:\Users\SilentSolitude\Desktop\Projects\test.cpp
I[03:16:14.991] Generic fallback command is: C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\clang C:\Users\SilentSolitude\Desktop\Projects\test.cpp -fsyntax-only -resource-dir=C:\Users\SilentSolitude\Desktop\CP\clangd_12.0.0\lib\clang\12.0.0
I[03:16:14.992] Parsing command…
I[03:16:15.013] internal (cc1) args are: -cc1 -triple x86_64-pc-windows-msvc19.11.0 -fsyntax-only -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -resource-dir C:\Users\SilentSolitude\Desktop\CP\clangd_12.0.0\lib\clang\12.0.0 -internal-isystem C:\Users\SilentSolitude\Desktop\CP\clangd_12.0.0\lib\clang\12.0.0\include -internal-isystem C:/Program Files/Microsoft Visual Studio 10.0/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 9.0/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include -internal-isystem C:/Program Files/Microsoft Visual Studio 8/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include -fdeprecated-macro -fdebug-compilation-dir C:\Users\SilentSolitude\Desktop\Projects -ferror-limit 19 -fmessage-length=120 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.11 -std=c++14 -fdelayed-template-parsing -fcxx-exceptions -fexceptions -faddrsig -x c++ C:\Users\SilentSolitude\Desktop\Projects\test.cpp
I[03:16:15.016] Building preamble…
I[03:16:15.033] Indexing headers…
E[03:16:15.034] [pp_file_not_found] Line 8: ‘bits/stdc++.h’ file not found
I[03:16:15.035] Building AST…
E[03:16:15.086] [expected_namespace_name] Line 13: expected namespace name
E[03:16:15.087] [undeclared_var_use] Line 15: use of undeclared identifier ‘pair’
E[03:16:15.089] [expected_after] Line 15: expected ‘;’ after alias declaration
E[03:16:15.089] [unknown_typename] Line 34: unknown type name ‘clock_t’
E[03:16:15.090] [undeclared_var_use] Line 34: use of undeclared identifier ‘clock’
E[03:16:15.091] [undeclared_var_use] Line 35: use of undeclared identifier ‘std’
E[03:16:15.091] [undeclared_var_use] Line 35: use of undeclared identifier ‘std’
E[03:16:15.092] [undeclared_var_use] Line 36: use of undeclared identifier ‘std’
E[03:16:15.093] [undeclared_var_use] Line 36: use of undeclared identifier ‘std’
E[03:16:15.093] [unknown_typename] Line 38: in included file: unknown type name ‘string’
E[03:16:15.094] [fatal_too_many_errors] Line 1: too many errors emitted, stopping now
I[03:16:15.095] Indexing AST…
I[03:16:15.096] Testing features at each token (may be slow in large files)
I[03:16:15.134] Found definition heuristically in index for ll
I[03:16:15.135] Found definition heuristically in index for ll
I[03:16:15.136] Found definition heuristically in index for ll
I[03:16:15.137] Found definition heuristically in index for ll
I[03:16:15.141] Found definition heuristically in index for ll
I[03:16:15.142] Found definition heuristically in index for ll
I[03:16:15.148] Found definition heuristically in index for ll
I[03:16:15.149] Found definition heuristically in index for ll
I[03:16:15.150] Found definition heuristically in index for ll
I[03:16:15.151] Found definition heuristically in index for ll
I[03:16:15.153] Found definition heuristically in index for ll
I[03:16:15.154] Found definition heuristically in index for ll
I[03:16:15.157] Found definition heuristically in index for ll
I[03:16:15.158] Found definition heuristically in index for ll
I[03:16:15.158] Found definition heuristically using nearby identifier custom_hash
I[03:16:15.160] Found definition heuristically using nearby identifier custom_hash
I[03:16:15.176] Found definition heuristically in index for ll
I[03:16:15.177] Found definition heuristically in index for ll
I[03:16:15.181] Found definition heuristically in index for ll
I[03:16:15.182] Found definition heuristically in index for ll
I[03:16:15.182] Found definition heuristically in index for ll
I[03:16:15.183] Found definition heuristically in index for ll
I[03:16:15.280] All checks completed, 12 errors

I would really appreciate any help, thanks.