0

I am trying to use the clangd vscode extension with cmake, but it isn't detecting my pch file, even though everything compiles just fine. I think this is because clangd isn't detecting the cmake_pch_arm64.hxx file generated by cmake. Because if I manually go and change the file extension from .hxx to .h and update the compile_commands.json file accordingly, everything works as expected. Additionally I get a warning from clangd in the cmake_pch_arm64.hxx file: #pragma system_header ignored in main file clang(-Wpragma-system-header-outside-header).

I am wondering if there is any way to configure cmake so that it stops using the .hxx extension for that file? If this is not supported by make, is there any other way to automatically change the file extension and compile_commands.json file every time I build the project?

Taxen99
  • 49
  • 5

1 Answers1

1

Clangd does not currently support PCH files, as discussed in https://github.com/clangd/clangd/issues/248.

Internally, clangd uses an optimization very similar to PCH files where it precompiles the includes at the top of the file (which it calls the "preamble") into a representation that it reuses as you edit the file; so as a clangd user, you benefit from many of the performance advantages of a PCH file without having to explicitly use one.

However, clangd will not try to read an actual PCH file produced by your build. Every compiler has its own format for these files (so e.g. clang-based tools cannot read PCH files produced by gcc), and even in the case of a PCH file produced by clang, the format changes between versions. (In the case of an exact version match between clang as your build compiler and clangd, clangd could hypothetically read the PCH file, but this is not currently implemented.)


That said, you can still use clangd in a project whose build uses PCH files: just include the (non-precompiled) header in the normal way in your source files.

Note that, if needed, you can make #include directives (or any code) "seen" only by clangd and not your build as follows:

  1. Choose a macro name of your choosing, for example __CLANGD__.
  2. Create a clangd config file that defines this macro for clangd's purposes:
    CompileFlags:
      Add: [-D__CLANGD__]
    
  3. Make the #include or other code conditional on the macro:
    #ifdef __CLANGD__
    #include "my_header.h"
    #endif
    
HighCommander4
  • 50,428
  • 24
  • 122
  • 194