现在,我们在Debian/Ubuntu中直接使用aptinstallclang-format安装clang-format。然而,Debian/Ubuntu提供的clang-format已经很老了。例如,Ubuntu18.04的clang-format最新版本是v7,但最新的稳定版本已经达到v13。您可以按照以下步骤安装最新版本的clang-format:第1步:获取keywget-O-https://apt.llvm.org/llvm-snapshot.gpg.key|sudoapt-keyadd-第2步:按照操作系统在/etc/apt/sources.list末尾追加如下内容:Buster(Debian10):debhttp://apt.llvm.org/buster/llvm-toolchain-bustermaindeb-srchttp://apt.llvm.org/buster/llvm-toolchain-bustermain#11debhttp://apt.llvm.org/buster/llvm-toolchain-buster-11maindeb-srchttp://apt.llvm.org/buster/llvm-toolchain-buster-11main#12debhttp://apt.llvm.org/buster/llvm-toolchain-buster-12maindeb-srchttp://apt.llvm.org/buster/llvm-toolchain-buster-12mainBullseye(Debian11):debhttp://apt.llvm.org/bullseye/llvm-toolchain-bullseyemaindeb-srchttp://apt.llvm.org/bullseye/llvm-toolchain-bullseyemain#11debhttp://apt.llvm.org/bullseye/llvm-toolchain-bullseye-11maindeb-srchttp://apt.llvm.org/bullseye/llvm-toolchain-bullseye-11main#12debhttp://apt.llvm.org/bullseye/llvm-toolchain-bullseye-12maindeb-srchttp://apt.llvm.org/bullseye/llvm-toolchain-bullseye-12mainXenial(Ubuntu16.04):debhttp://apt.llvm.org/xenial/llvm-toolchain-xenialmaindeb-srchttp://apt.llvm.org/xenial/llvm-toolchain-xenialmain#11debhttp://apt.llvm.org/xenial/llvm-toolchain-xenial-11maindeb-srchttp://apt.llvm.org/xenial/llvm-toolchain-xenial-11main#12debhttp://apt.llvm.org/xenial/llvm-toolchain-xenial-12maindeb-srchttp://apt.llvm.org/xenial/llvm-toolchain-xenial-12mainBionic(Ubuntu18.04):debhttp://apt.llvm.org/bionic/llvm-toolchain-bionicmaindeb-srchttp://apt.llvm.org/bionic/llvm-toolchain-bionicmain#11debhttp://apt.llvm.org/bionic/llvm-toolchain-bionic-11maindeb-srchttp://apt.llvm.org/bionic/llvm-toolchain-bionic-11main#12debhttp://apt.llvm.org/bionic/llvm-toolchain-bionic-12maindeb-srchttp://apt.llvm.org/bionic/llvm-toolchain-bionic-12main如果您的操作系统不在这个列表中,您可以查看https://apt.llvm.org/。第3步:对当前已安装软件包的更新sudoaptupdateStep4:检查所可用版本$aptsearchclang-formatSorting...DoneFullTextSearch...Doneclang-format/unknown1:13.0-53~20210202214852.35amd64[upgradablefrom:1:3.8-33ubuntu3.1]格式化C/C++/的工具Obj-Ccodeclang-format-11/unknown1:11.1.0~++20210314110124+1fdec59bffc1-1~exp1~20210314220751.162amd64格式化C/C++/Obj-Ccodeclang-format-12/unknown1:12.0.0的工具~++20210319082646+4990141a4366-1~exp1~20210319193401.61amd64格式化C/C++/Obj-C代码的工具clang-format-13/unknown1:13~++20210327080829+e5f2898bc751-1~exp1?53~2719207amd64格式化C/C++/Obj-Ccodeclang-format-3.5/xenial1:3.5.2-3ubuntu1amd64格式化C/C++/Obj-Ccodeclang-format-3.6/xenial1:3.6.2-3ubuntu2的工具amd64格式化C/C++/Obj-Ccodeclang-format-3.7/xenial1:3.7.1-2ubuntu2amd64格式化C/C++/Obj-Ccodeclang-format-3.8/xenial-updates的工具,now1:3.8-2ubuntu4amd64[installed,automatic]格式化C/C++/Obj-Ccodeclang-format-3.9/xenial-updates,xenial-sec的工具urity1:3.9.1-4ubuntu3~16.04.2amd64格式化C/C++/Obj-C的工具codeclang-format-4.0/xenial-updates,xenial-security1:4.0-1ubuntu1~16.04.2amd64格式化C的工具/C++/Obj-Ccodeclang-format-5.0/xenial-updates1:5.0-3~16.04.1amd64格式化C/C++/Obj-Ccodeclang-format-6.0/xenial-updates1:6.0-1ubuntu2~的工具16.04.1amd64C/C++/Obj-C代码格式化工具clang-format-8/xenial-updates,xenial-security1:8-3~ubuntu16.04.1amd64C/C++/Obj-C代码格式化工具步骤5:使用aptinstall安装你想要的clang-format然后你可以使用--version检查安装的clang-format。$sudoaptinstall-yclang-format-13$clang-format-13--versionUbuntuclang-formatversion13.0.0-++20210327080829+e5f2898bc751-1~exp1~20210327192522.♂7$sudoaptinstall-yclang-format$clang-format--versionUbuntuclang-formatversion13.0.0-++20210327080829+e5f2898bc751-1~exp1~20210327192522.♂7Step 6:安装clang-13sudoaptinstall-yclang-13Step7:查看版本$clang-13-vUbuntuclangversion13.0.0-++20210327080829+e5f2898bc751-1~exp1~20210327192522.3607Step8:设置clang的默认版本:clang13nativelyinstallclang3.8and clang13,查看clang版本,默认是3.8:clang++-vclangversion3.8.0-2ubuntu4(tags/RELEASE_380/final)接下来,设置clang13为默认$sudoupdate-alternatives--install/usr/bin/clang++clang++/usr/bin/clang++-13100n$sudoupdate-alternatives--install/usr/bin/clangclang/usr/bin/clang-13--slave/usr/bin/clang++clang++/usr/bin/clang++-13100参考文章:【Ubuntu下安装高版本clang-format11、12、13】(https://www.codeleading.com/a...)-->原文
