Skip to content

Latest commit

 

History

History
152 lines (122 loc) · 6.67 KB

lsp_clangd_tips.md

File metadata and controls

152 lines (122 loc) · 6.67 KB

Sublime Text C/C++ clangd LSP Tips

The following takes Ubuntu Bionic (18.04) as an example. For Windows, see here.

Installation Prerequisites

  • Install clangd from the apt repositories

    1. Add the GPG key: $ wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -

    2. Add the LLVM apt repository as listed on the above web page:

      You have to change the following command depending on your Ubuntu version. Please check the apt repositories' web page as described above. The minimal LLVM version required is 7. Here I use version 10 anyway.

      sudo su -c "cat > /etc/apt/sources.list.d/llvm-toolchain-bionic.list <<EOF
      deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main
      deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main
      EOF"
    3. Fetch information from the newly added repository: $ sudo apt update

    4. Install clang-tools: $ sudo apt install clang-tools-10 clangd-10

    5. Prefer using the installed clangd: $ sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-10 100

    6. Make sure clangd is available with the correct version: $ clangd --version

      clangd version 10.0.0-svn371101-1~exp1+0~20190905175528.1178~1.gbpc222ee (trunk)
      

Generate the Setup for Your Project

You would have to generate a compile_commands.json file.

  • If your project uses cmake, $ cmake path/to/source -DCMAKE_EXPORT_COMPILE_COMMANDS=ON should generate a compile_commands.json in the current directory.

  • If your project uses make, use compiledb to generate it. You may check the readme on compiledb's GitHub repository.

    1. Install compiledb: $ sudo pip install compiledb
    2. compiledb is just a wrapper of make. Use compiledb make as if you are using make. So you may probably do compiledb make all in your Makefile directory.
    3. compile_commands.json should be generated at the same directory in the above step.

The generated compile_commands.json usually has no information about header files, so you would see nonsense output from the LSP. But we could add header files information to compile_commands.json.

  1. Install compdb: $ sudo pip install compdb
  2. Assuming a build directory build/, containing a compile_commands.json, a new compilation database, containing the header files, can be generated with: $ compdb -p build/ list > compile_commands.json. You may check the readme on compdb's GitHub repository.

After we have a decent compile_commands.json, it's all set now. Copy the generated compile_commands.json to your project root. You may want to add compile_commands.json into you .gitignore as well.

Setup for Sublime Text LSP

  1. Install LSP via Package Control.

  2. Here's a clangd LSP settings example (Menu > Preferences > Packages Settings > LSP > Settings):

    {
      "clients": {
        "clangd": {
          "enabled": true,
          "command": [
            "clangd", // you may use an absolute path for this clangd executable
            "-function-arg-placeholders=0",
            "-header-insertion-decorators=1",
            "-index",
          ],
          "scopes": ["source.c", "source.c++", "source.objc", "source.objc++"],
          "syntaxes": [
            "Packages/C++/C.sublime-syntax",
            "Packages/C++/C++.sublime-syntax",
            "Packages/Objective-C/Objective-C.sublime-syntax",
            "Packages/Objective-C/Objective-C++.sublime-syntax",
          ],
          "languageId": "cpp",
        },
      },
    }
  3. If you have other related linters enabled, you may want to disable them since LSP is more powerful. To do that in your project, edit the project settings (Menu > Project > Edit Project):

    {
      "folders":
      [
        // ... not important, here are just your project folders
      ],
      "settings":
      {
        // for example, to disable some other C/C++ linters
        "SublimeLinter.linters.gcc.disable": true,
        "SublimeLinter.linters.clang.disable": true,
        "SublimeLinter.linters.clang++.disable": true,
      }
    }

References

clangd on Windows

I hardly write C/C++ codes on Windows but I did make some trying. The official LLVM/Clang support on Windows is for MSVC-only. So I use MSVC + LLVM/Clang combination to make clangd work.

  1. Donwload the Visual Studio Build Tools 2017 or 2019 vs_buildtools.exe.

  2. Execute vs_buildtools.exe and install VC++ build tools, Windows 10 SDK and CMake VC++ tools as shown in this screenshot.

  3. Download one of the following. Remember that the minimal version has to be 7 for clangd to work properly. I use stable LLVM 9.0.0 when I am writing this note.

  4. Install the downloaded offline LLVM installer. During the installation, there is an option to add LLVM into PATH env.

  5. Maybe you have to reboot your PC to make the new PATH env work.

  6. Make sure clangd is available from PATH. Open cmd and execute $ clangd --version should show something like clangd version 9.0.0 (tags/RELEASE_900/final).

  7. I use the same LSP settings with the one I used in Ubuntu above. Of course, you still have to deal with generating a compile_commands.json which seems to be a harder part on Windows.

How About the cquery LSP

cquery LSP does quite the same thing. The key is that you have to generate a compile_commands.json for your project. But, it looks like cquery has been abandoned due to its inactivity and the thriving of clangd which is developed by the official LLVM/Clang team.