现在,我们在 Debian/Ubuntu 中直接使用 apt install clang-format 来安装 clang-format。 然而,Debian/Ubuntu 提供的 clang-format 已经很老了。 例如,来自 Ubuntu 18.04 的最新版本的 clang-format 是 v7,但最新的稳定版本已经达到了 v13。 您可以按照以下步骤安装最新版本的 clang-format:
Step 1: 获取key
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
Step 2: 根据操作系统在/etc/apt/sources.list的末尾追加以下内容:
Buster (Debian 10):
deb http://apt.llvm.org/buster/ llvm-toolchain-buster main
deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster main
# 11
deb http://apt.llvm.org/buster/ llvm-toolchain-buster-11 main
deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster-11 main
# 12
deb http://apt.llvm.org/buster/ llvm-toolchain-buster-12 main
deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster-12 main
Bullseye (Debian 11):
deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye main
deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye main
# 11
deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-11 main
deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-11 main
# 12
deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-12 main
deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-12 main
Xenial (Ubuntu 16.04):
deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial main
deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial main
# 11
deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-11 main
deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-11 main
# 12
deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-12 main
deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-12 main
Bionic (Ubuntu 18.04):
deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic main
deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic main
# 11
deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-11 main
deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-11 main
# 12
deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main
deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main
You can check https://apt.llvm.org/ if your operating system is not on this list.
Step 3: 对当前已安装软件包的更新
sudo apt update
Step 4: 检查所有可用版本
$ apt search clang-format
Sorting... Done
Full Text Search... Done
clang-format/unknown 1:13.0-53~20210202214852.35 amd64 [upgradable from: 1:3.8-33ubuntu3.1]
Tool to format C/C++/Obj-C code
clang-format-11/unknown 1:11.1.0~++20210314110124+1fdec59bffc1-1~exp1~20210314220751.162 amd64
Tool to format C/C++/Obj-C code
clang-format-12/unknown 1:12.0.0~++20210319082646+4990141a4366-1~exp1~20210319193401.61 amd64
Tool to format C/C++/Obj-C code
clang-format-13/unknown 1:13~++20210327080829+e5f2898bc751-1~exp1~20210327192522.3607 amd64
Tool to format C/C++/Obj-C code
clang-format-3.5/xenial 1:3.5.2-3ubuntu1 amd64
Tool to format C/C++/Obj-C code
clang-format-3.6/xenial 1:3.6.2-3ubuntu2 amd64
Tool to format C/C++/Obj-C code
clang-format-3.7/xenial 1:3.7.1-2ubuntu2 amd64
Tool to format C/C++/Obj-C code
clang-format-3.8/xenial-updates,now 1:3.8-2ubuntu4 amd64 [installed,automatic]
Tool to format C/C++/Obj-C code
clang-format-3.9/xenial-updates,xenial-security 1:3.9.1-4ubuntu3~16.04.2 amd64
Tool to format C/C++/Obj-C code
clang-format-4.0/xenial-updates,xenial-security 1:4.0-1ubuntu1~16.04.2 amd64
Tool to format C/C++/Obj-C code
clang-format-5.0/xenial-updates 1:5.0-3~16.04.1 amd64
Tool to format C/C++/Obj-C code
clang-format-6.0/xenial-updates 1:6.0-1ubuntu2~16.04.1 amd64
Tool to format C/C++/Obj-C code
clang-format-8/xenial-updates,xenial-security 1:8-3~ubuntu16.04.1 amd64
Tool to format C/C++/Obj-C code
Step 5: 使用 apt install 安装你想要的 clang-format。 然后您可以使用 --version 检查已安装的 clang-format。
$ sudo apt install -y clang-format-13
$ clang-format-13 --version
Ubuntu clang-format version 13.0.0-++20210327080829+e5f2898bc751-1~exp1~20210327192522.3607
$ sudo apt install -y clang-format
$ clang-format --version
Ubuntu clang-format version 13.0.0-++20210327080829+e5f2898bc751-1~exp1~20210327192522.3607
Step 6: 安装clang-13
sudo apt install -y clang-13
Step 7: 查看版本
$ clang-13 -v
Ubuntu clang version 13.0.0-++20210327080829+e5f2898bc751-1~exp1~20210327192522.3607
Step 8: 设置clang默认版本为:clang13
本机安装clang3.8 和 clang13,查看clang 版本,默认是3.8:
clang++ -v
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
接下来将clang13设置为默认
$ sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-13 100
n$ sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-13 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-13 100
参考文章:
- [Ubuntu下安装高版本clang-format 11, 12, 13
](https://www.codeleading.com/a...) --> 原文