惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
月光博客
月光博客
IT之家
IT之家
B
Blog RSS Feed
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
博客园_首页
B
Blog
V
V2EX
腾讯CDC
Vercel News
Vercel News
量子位
Microsoft Security Blog
Microsoft Security Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Install LLVM/Clangd from Source on CentOS 7
2021-07-03 · via jdhao's digital space

Clangd is a language server for C++/C etc. I try to use clangd 12 for C++ code auto-completion on CentOS 7.

My failed attempt to use clang binary release#

Clangd can be easily installed via its binary release. However, when I run clangd --version, I see the following error:

clangd_12.0.0/bin/clangd: /lib64/libc.so.6: version `GLIBC_2.18’ not found (required by clangd_12.0.0/bin/clangd)

The reason is that libc.so on CentOS 7 is too old for clangd to run. We need to use a newer version of glibc (at least glibc-2.18).

I tried to clone glibc repo and build version 2.20 from scratch:

git clone https://mirrors.tuna.tsinghua.edu.cn/git/glibc.git
cd glibc
git checkout glibc-2.20

cd ..
# Note that glibc requires out-source building, i.e., we are forbidden to build glibc inside its source directory.
mkdir glibc-build
../glibc/configure --prefix=$HOME/local
make -j 16
make install

After that, I added $HOME/local/lib to LD_LIBRARY_PATH. Then things went badly wrong: every executable I use segfaults immediately! For example, when I run ls, I got an error from the shell:

zsh: segmentation fault (core dumped) ls -F –color=auto

According to this post, this is due to version mismatch: the glibc version required to run the executable is different from what is provided right now.

I tried the glibc-6.18 release tarball downloaded from here. The same errors happens when I add $HOME/local/lib to LD_LIBRARY_PATH and run clang.

Building clangd from source: success#

I also tried to build llvm project from scratch to use clangd. Here is how to compile llvm from source:

git clone --depth=1 https://github.com/llvm/llvm-project.git

cd llvm-project
mkdir build && cd build
cmake -G "Unix Makefiles" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DCMAKE_INSTALL_PREFIX=~/tools/llvm -DCMAKE_BUILD_TYPE=Release ../llvm
make -j 16
make install

Some explanation about the options used:

  • -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra": it specifies which targets we want to build. To use clangd and clang-tidy, clang-tools-extra is a must.
  • -DCMAKE_INSTALL_PREFIX=~/tools/llvm: it specifies where we want to install llvm, in this case, we install it under ~/tools/llvm.
  • -DCMAKE_BUILD_TYPE=Release: it specifies the build type. Release type will be smaller than Debug.

For more details about Cmake options on building llvm, check here.

Since llvm is a huge project, it may take quite a while to build. FYI, It takes about 37 minutes to build on my server with 16 parallel processes.

After installing llvm, do not forget to add llvm binary to your PATH variable. This time, clangd runs without any error.

How to use clangd#

According to clangd documentation, you can either generate a compile_commands.json file for your project or use compile_flags.txt.

For simple project, using compile_flags.txt is sufficient. Your compile flags are written one per line. A sample compile_flags.txt looks like this:

-Wall
-std=c++11
-L/usr/local/lib
-I/usr/local/include
-lfmt

References#