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