c++cmakeqt-creatorsyntax-highlightingclangd

clangd false positive '<quadmath.h>' not found


I have a C++ project, using CMake, GCC and Arch Linux. I am using GCC and the project is compiling correctly (both in IDE and command line), but I have these errors in Qt Creator:

heat_transfer_explicit_scheme.cpp:1:10: In included file: 'quadmath.h' file not found
float128.hpp:54:10: error occurred here
...
heat_transfer_explicit_scheme.cpp:1:10: In included file: no member named 'value' in 'boost::multiprecision::number_category<backends::float128_backend>'
number.hpp:131:399: error occurred here

But this header is present in /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include, and clicking on #include <quadmath.h> will open it in the IDE.

As I understand, /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include is not the default sysroot, and clangd does not know about this location, so I tried to add CMAKE_SYSROOT /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include like on this forum thread: Qt Forum — Qt Creator - clang code model doesn't find types, even though code compiles. But it doesn't make <quadmath.h> location accessible to clangd.

Moreover, after setting CMAKE_SYSROOT in CMakeLists.txt pointing to their location, other project dependencies (like boost) can't be found by find_package.

Also, I tried to add /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include to PATH of current terminal session and open Qt Creator from this env, but it seems to have no effect.

Qt Creator version 14.0.1 based on Qt 6.7.2 (GCC 14.2.1 20240805, x86_64).

Clangd not indexing these files with clang++ compiler because he have his own realizations and not needed gcc intrinsic headers codechecker. But I am using g++ in project and want clangd to handle the indexing.


Solution

  • /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include is gcc 14's "built-in include directory". Clangd deliberately ignores this, even if you point it at the gcc 14 installation using --query-driver, because many files in this directory contain gcc-only intrinsics that the clang frontend cannot parse.

    (If you're curious, the reasons for clangd ignoring this directory are discussed in more detail at https://github.com/clangd/clangd/issues/1695)

    You can override this behaviour by adding this directory to the include path in the compile commands used by clangd. A simple way to do this is to create a .clangd file in the project root containing:

    CompileFlags:
      Add: -I/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include