iTranslated by AI
How to Build the Latest Version of clangd
I'm leaving these notes here so I don't forget them.
Environment
- Ubuntu 22.04
References
Although this renders this article redundant, everything you need is documented here:
Downloading the latest clangd
Download or git clone the entire LLVM project. As of this writing, v19 appears to be the latest version.
I downloaded it to Downloads. Note that it's over 200MB, so ensure you have sufficient disk space.
Once downloaded, move into the directory.
~/Downloads
$ cd ./llvm-project-main
Preparation for building
This guide assumes you already have cmake installed.
On Ubuntu, you can install it using sudo apt install cmake.
1. Create a build directory
Create a directory for the build and move into it.
You can name the build directory anything you like, but here we will call it build.
~/Downloads/llvm-project-main
$ mkdir ./build
$ cd ./build
2. CMake configuration
Run the following command inside build to start the CMake configuration.
$ cmake ../llvm/ \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" \
The options are explained below.
-
-DCMAKE_BUILD_TYPE
Specifies the build type. There are four types; I selected Release here.
| Build Type | Optimizations | Debug Info | Assertions | Best suited for |
|---|---|---|---|---|
| Release | For Speed | No | No | Users of LLVM and Clang |
| Debug | None | Yes | Yes | Developers of LLVM |
| RelWithDebInfo | For Speed | Yes | No | Users that also need Debug |
| MinSizeRel | For Size | No | No | When disk space matters |
Source
https://llvm.org/docs/CMake.html#frequently-used-cmake-variables
-
-DLLVM_ENABLE_PROJECTS
LLVM contains various projects, so specify which ones you want to build.
In this case, we useclangandclangd. Separate them with a;.
3. Building
Run the following command inside the build directory.
~/Downloads/llvm-project-main/build
$ cmake --build . --target install
It took about 3 to 4 hours for me, perhaps because my PC isn't very powerful. Let's wait patiently.
4. Installation
Once the build is finished, you should find the clang and clangd executables in build/bin. Once you confirm these files exist, run the following command:
~/Downloads/llvm-project-main/build
$ cmake -DCMAKE_INSTALL_PREFIX=/opt/llvm -P cmake_install.cmake
Specify the installation destination in -DCMAKE_INSTALL_PREFIX. I have a habit of placing individually source-built items in /opt, so I chose /opt/llvm.
5. Adding to PATH
It is convenient to add this to your .bash_profile.
export PATH="/opt/llvm/bin:$PATH"
6. Verifying operation with vim-lsp
Since I am using vim-lsp-settings, I add the following to my .vimrc:
let g:lsp_settings = {
\ 'clangd': {
\ 'cmd': ['/opt/llvm/clangd-15', '--enable-config'],
\ }
\}
Launch Vim and check if it works properly.
Discussion