Search code examples
c++vscode-extensionsclangdvscodium

Clangd can't find standard library headers and gives weird errors for everything


Clangd vscode's extension keeps giving me weird squigglys for #includes and standard types.

My setup: Win11. Fresh intall. I've installed both GCC/G++ and CLANG with MYSYS (following this guide), I've edited PATH to include the relevant directories, I have VSCode's fork VSCodium, and I've installed Clangd LS and Clangd extension for VSCodium.

My problem: Clangd keeps giving me weird incorrect squiggly errors for the most common things, such as trying to pass a std::string or a c-string (const * char[], const char[]) to std::cout. e.g.

class someClass
{
...
public:
    void someMethod()
    {
        if (this->someFuncThatReturnsBool())
        {   
            std::cout << "A simple \\n-terminated string literal \n";
                  //^^^^^^
                  // "Invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'const char *')" 
        }
        return;
    };
};

It also keeps giving me some include errors regarding both include paths for headers that are definetly were they're suposed to be. e.g. error trying to #include <iostream>, in some cases it states that i should replace <> with "", in others it just says it cant find the relevant header files.

I know this are false errors, as my program compiles both with g++ and clang

(unsuccesful) Attempts to fix this:

  • I've uninstalled and re-installed all relevant programs.
  • I've set configurations for clangd (both at global and project scope), adding "compiler flags" indicating the include directory path, the standard to use, &c. in a .clangd config file
  • I've tryed using the --query-driver flag as shown in Clangd's FAQ

I've searched on google, looked into the FAQ, search for other qa in this site, and can't find anything. Thank you in advanced

EDIT: ANSWER FOUND @HolyBlackCat suggested that the problem could be in Clangd LS being installed by the VSC extension. Indeed, I deleted the binary, installed Clangd LS (as part of the clang-extra-tools package) with MYSYS

$ pacman -S mingw-w64-ucrt-x86_64-clang-extra-tools

and then specified the path to clangd.exe inside the VSC extension configuration GUI and now everything works as intended.


Solution

  • If your compiler comes from MSYS2, the easiest option is to install Clangd (clangd.exe, the language server) from MSYS2 as well, as opposed to using the official Clang installer or letting VSC extension download it.

    Then specify the path to it in VSC settings: "clangd.path": "path/to/clangd",.

    MSYS2's Clangd uses the same default flags as the MSYS2's compilers.

    If MSYS2's Clangd misbehaves (earlier versions used to do so), you can use the official build as long as you specify the correct compiler flags for it, in "clangd.fallbackFlags" or elsewhere (which would probably include at least --target=?? --sysroot=??, where the former is the target triplet, and the latter is probably path/to/mingw64 (replace mingw64 according to your MSYS2 environment)).