Search code examples
clangsublimetext3windows-subsystem-for-linuxsublime-text-pluginlanguage-server-protocol

Windows Sublime and LSP-Clangd plugin: Cannot find C++ headers


I recently decided I wanted to try SublimeText 3, and I find it super neat, the plugin support is expansive! I recently found this plugin: https://github.com/tomv564/LSP and I'm using it with Clangd. The issue though is that the errors I'm getting I know compile as I've tried. The steps I took were straightforward:

  1. Install Sublime: https://download.sublimetext.com/Sublime%20Text%20Build%203176%20x64%20Setup.exe
  2. Install CLang.exe pre-built binary for Windows: https://releases.llvm.org/7.0.1/LLVM-7.0.1-win64.exe
  3. Restart Computer (so new modification to Windows Environment PATH can take place).
  4. Install LSP from Package Control
  5. type LSP: Enable Language Server Globally into command pallette
  6. type clangd into the subsequent window and hit enter to enable.
  7. Restart Sublime and open a C++ file.

The C++ file itself is nothing special, I can post it here to ensure zero ambiguity:

#include <bits/stdc++.h>

using namespace std;

int main() {
    int w, s, c, k;
    cin >> w >> s >> c >> k;
    bool valid = false;
    if (k > s)
        valid = true;
    else if (k == s)
    {
        if (w + c < k)
            valid = true;
        else if (w + c == k)
            valid = true;
        else if (w == k && c == k)
            valid = true;
        else if (w + c <= 2 * k)
            valid = true;
    }
    else if (w + c < k)
        valid = true;
    else if (w + c == k && s <= 2 * k)
        valid = true;
    cout << (valid ? "YES" : "NO") << endl;
    return 0;
}

This file builds when I actually run it through with clang++ otherside.cpp through WSL, but in Sublime.exe, The two errors I'm getting are:

  • 'bits/stdc++.h' file not found
  • using directive refers to implicitly-defined namespace 'std'
    • use of undeclared identifier 'cin'
    • use of undeclared identifier 'cout'
    • use of undeclared identifier 'endl'

The three nested bullets I believe would be solved with the solution to the 'std' error so I don't really think they're errors?

'bits/stdc++.h' is an include I use when participating in programming competitions, so I know that it exists despite being bad in the workplace, and I know that using namespace std is not "good practice" but at the very least I know it's not an error that breaks compilation. This feels like it's a Clangd issue, but I know that IDE with LSP I have used in the past, such as Visual Studio and CLion do not report these two errors. Has anyone else solved this issue before?

UPDATE: I actually took the time to build the small cpp file with Clang.exe by running clang++ otherside.cpp. I got the expected error specifying: "fatal error: 'bits/stdc++.h' file not found" However, compiling it with via clang++ otherside.cpp on WSL compiles just fine. So it seems like there's a discrepancy between Windows and Linux.

I figured out that I need C/C++ headers on Windows. According to: https://clang.llvm.org/get_started.html , the recommended is MSys utilities or GNUWin32. I was hoping I could use C/C++ headers through WSL though, is that a possibility?

Feb.22.19 UPDATE: I attempted to install MinGW C and C++ compilers to get the headers for Clang, but it's looking like if I want Clang to find those headers, I'll have to build Clang from source and modify something called initHeaderSearch.cpp. Again, I'm hoping for more of a solution where I could use WSL's C/C++ headers with Clang.exe.

Also, as just a proof of concept to my friend, I also installed Sublime on WSL, and installed the LSP-Clangd plugin to show that none of the Clang errors I get on Windows were appearing on WSL Sublime. While sure enough the errors weren't appearing on WSL Sublime, it raised an interesting point. Maybe there's a way to start Clangd in WSL and have Sublime.exe hook into that instead? I know within the last few years WSL has made leaps and bounds and one of the things that came out of it was AF_UNIX sockets. I'm not the most familiar with unix sockets though, is it possible to use that to start or interact with Clangd?


Solution

  • I've solved my initial question. To be clear: I figured out why Clangd cannot find c++ headers. I did not solve how to use C/C++ headers provided by WSL nor did I solve how to interact with Clangd through WSL using unix sockets.

    My issue was a little deeper than simply missing stdc++.h. I was missing all C/C++ headers in general. There are various methods to getting these, but the method I chose was to install MSVC via "Visual Studio Build Tools". I downloaded "Visual Studio Installer" here: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15

    After downloading, I installed the program and followed these two steps:

    1. Open "Visual Studio Installer"
    2. Install "Universal Windows Platform build tools"
      • One of the optionals had checked to be installed with was "Windows 10 SDK (10.0.17763.0)"

    After that, the LSP plugin just worked for me. The only issue then was "bits/stdc++.h" which makes sense that MSVC doesn't have it since it's non-standard. I simply Googled "GCC stdc++.h" for that one and pulled the first one I found, specifically this one: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/precompiled/stdc%2B%2B.h and put it in a folder called "bits" that lived in the same place as the rest of my C/C++ headers (for me that was here: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\include).

    A little bit of a PS in closing: I really installed this entire tool chain because I wanted to see if I could have something that was what I considered "Linux independent." I wanted a light weight alternative to Visual Studio that I could recommend to fellow peers who weren't as Linux-oriented (though I have to admit finding a solution that used WSL's C/C++ headers was me giving up on finding something "Linux independent"). There are multiple ways of getting C/C++ headers, the most prominent was to install Visual Studio or MinGW. Installing Visual Studio defeats the purposes of having an alternative and MinGW actually requires building Clang from source to include the C/C++ header locations. MSVC was the perfect option here as it's a simple matter of installing it. This solution really only adds a couple step to my steps to reproduce in the question and it completes the package and allows for a relatively easy to setup development environment for C/C++. Those steps are:

    1. Install Sublime: https://download.sublimetext.com/Sublime%20Text%20Build%203176%20x64%20Setup.exe
    2. Install CLang.exe pre-built binary for Windows: https://releases.llvm.org/7.0.1/LLVM-7.0.1-win64.exe
    3. Install "Visual Studio Build Tools": https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15
    4. Open "Visual Studio Build Tools" and install "Universal Windows Platform build tools"
    5. Restart Computer (so new modification to Windows Environment PATH can take place).
    6. Install LSP from Package Control
    7. type LSP: Enable Language Server Globally into command pallette
    8. type clangd into the subsequent window and hit enter to enable.
    9. Restart Sublime and open a C++ file.