乐趣区

关于linux:ubuntu-1604-install-clangformat和clang-11-12-13

当初,咱们在 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…) –> 原文
退出移动版