如何在Ubuntu上安装clang-format?

60

我想在vim中使用clang-tools,特别是clang-format来自动格式化代码,但是我无法通过apt-get搜索找到这个工具。

有没有人遇到过这个问题,你有什么建议吗?


当您运行 sudo apt-cache search clang-format 命令时,您会看到什么? - Mike Kinghan
实际上,apt-get 找不到它,然后我将 Ubuntu-saucy 添加到我的仓库列表中,它就找到了 clang-format。 - Validus Oculus
1
我刚刚在我的回答中添加了指令,以从clang+llvm源代码存储库发布页面直接下载clang-format的最新二进制文件。 - Gabriel Staples
9个回答

60

使用 Ubuntu 16.04,只需执行以下操作:

sudo apt install clang-format

安装完这种方式后,我会得到 clang-format: command not found 的错误。于是我继续移除了那个 clang-format 包。 - Michael Fulton
@MichaelFulton 这在 ubuntu 16.04 上可以运行,不确定后续版本是否也可以。 - Eric
7
在Ubuntu 18.04上,该软件包可通过调用clang-format-6.0或您所拥有的任何版本来使用。 - Michael Fulton
我在Ubuntu 14.04上使用了 sudo apt install clang-format-3.6 - Gabriel Staples
在Ubuntu 18.04上,我使用了以下命令: sudo apt install clang-format-5.0 安装完成后,您可以尝试运行 clang-format-5.0 -h 来检查是否一切正常。 - Zhi Yuan

24
现在,你可以直接在Debian/Ubuntu中使用apt install clang-format来安装clang-format。但是,Debian/Ubuntu提供的clang-format版本相当旧。例如,Ubuntu 18.04中最新的clang-format版本为v7,而最新的稳定版本已经达到v12。您可以按照以下步骤安装最新版本的clang-format: 第1步:运行wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add - 第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
  • 靶心(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
    
  • 仿生(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
    
  • 聚焦(Ubuntu 20.04)

  • deb http://apt.llvm.org/focal/ llvm-toolchain-focal main
    deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main
    # 14
    deb http://apt.llvm.org/focal/ llvm-toolchain-focal-14 main
    deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-14 main
    # 15
    deb http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main
    deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main
    
  • 果酱(Ubuntu 22.04)

  • deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy main
    deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy main
    # 14
    deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-14 main
    deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-14 main
    # 15
    deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main
    deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main
    

    如果您的操作系统不在此列表中,可以访问https://apt.llvm.org/查看。

    步骤3:运行sudo apt update

    步骤4:运行apt search clang-format并检查所有可用版本:

    $ sudo apt search clang-format
    Sorting... Done
    Full Text Search... Done
    arcanist-clang-format-linter/stable 0.git20161021-2 all
      clang-format linter for Arcanist
    
    clang-format/unknown 1:13.0-53~20210202214848.38 amd64
      Tool to format C/C++/Obj-C code
    
    clang-format-11/unknown 1:11.1.0~++20210203115409+1fdec59bffc1-1~exp1~20210203230038.161 amd64
      Tool to format C/C++/Obj-C code
    
    clang-format-12/unknown 1:12.0.0~++20210312110334+ca14f0282fce-1~exp1~20210312221110.59 amd64
      Tool to format C/C++/Obj-C code
    
    clang-format-13/unknown 1:13~++20210315063844+b868a3edad9d-1~exp1~20210315174553.2286 amd64
      Tool to format C/C++/Obj-C code
    
    clang-format-6.0/stable 1:6.0.1-10 amd64
      Tool to format C/C++/Obj-C code
    
    clang-format-7/stable 1:7.0.1-8+deb10u2 amd64
      Tool to format C/C++/Obj-C code
    

    步骤 5:使用 apt install 安装所需的 clang-format 版本。之后可以使用 --version 命令检查已安装的 clang-format。同一环境中可安装多个版本的 clang-format

    $ sudo apt install -y clang-format-12
    $ clang-format-12 --version
    Ubuntu clang-format version 12.0.0-++20210312110334+ca14f0282fce-1~exp1~20210312221110.59
    
    $ sudo apt install -y clang-format
    $ clang-format --version
    Ubuntu clang-format version 13.0.0-++20210315063844+b868a3edad9d-1~exp1~20210315174553.2286
    

    希望这些说明的源代码能够更清晰地显示(https://apt.llvm.org),除此之外,对我来说效果非常好。 - Clodéric
    注意:同时也可以在Ubuntu 18-04上安装clang-format-13和clang-format-14。请参见我的答案 - Sonic78
    嗨@ocavue,我需要Clang-format-7,但是当我按照你的建议并执行“apt search clang-format”...每个版本都有,除了7. 我该如何获取clang-format-7? - Jack

    20

    在ubuntu-precise 12.04中无法使用clang-format,但在ubuntu saucy中可以使用。您可以在http://packages.ubuntu.com/saucy/clang-format-3.4上找到它。

    为了通过apt-cache查找此软件包,我们必须将以下列表添加到我们的存储库列表中。实际上,下面的列表是针对新加坡服务器生成的,但是如果要查找自己所在的国家,可以使用http://repogen.simplylinux.ch/generate.php

    生成列表后,您必须将它们添加到您的存储库中,您可以通过查看https://help.ubuntu.com/community/Repositories/CommandLine了解如何执行此操作。

    软件包列表如下:

    deb http://sg.archive.ubuntu.com/ubuntu/ saucy main restricted universe multiverse 
    
    deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy main restricted universe multiverse 
    
    deb http://sg.archive.ubuntu.com/ubuntu/ saucy-security main restricted universe multiverse 
    
    deb http://sg.archive.ubuntu.com/ubuntu/ saucy-updates main restricted universe multiverse 
    
    deb http://sg.archive.ubuntu.com/ubuntu/ saucy-proposed main restricted universe multiverse 
    
    deb http://sg.archive.ubuntu.com/ubuntu/ saucy-backports main restricted universe    multiverse 
    
    deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-security main restricted universe multiverse 
    
    deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-updates main restricted universe multiverse 
    
    deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-proposed main restricted universe multiverse 
    
    deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-backports main restricted universe multiverse 
    

    首先,您应该使用以下命令搜索clang-format:

    sudo apt-cache search clang-format

    然后,您可以安装所需的版本,例如:

    sudo apt-get install clang-format-3.3


    2
    sudo 在使用 apt-cache search 命令时不是必需的。 - Justin M. Keyes
    3
    在Ubuntu 14.04 LTS中,键入“sudo apt-get install clang-3.4”后,“sudo apt-get install clang-format-3.4”有效。 - Yonatan Simson
    我在Ubuntu 14.04上使用了sudo apt install clang-format-3.6 - Gabriel Staples
    我该如何获取最新版本?sudo apt install clang-format安装的是clang-format 6版本,但我能在网上找到的文档页面是针对版本9和11的。为什么Ubuntu版本落后了5个主要版本? - Aaron Franke

    11

    这个答案已经在被投票否决后进行了重写,截至2022年4月,它是对这个问题最新的回答。它也是唯一一个解释如何直接从制造商LLVM获取最新版本clang-format的答案。

    安装最新版本的clang-formatgit-clang-format(可通过git clang-format运行):

    摘要

    1. 在Ubuntu中安装clang-format,您可以使用sudo apt install clang-format或类似的命令(见下文),但它经常安装一个非常旧的版本,缺少很多功能。所以:
    2. [首选] 要在Ubuntu中安装最新版本clang-format,您必须直接从GitHub上的官方发布页面获取。我将在下面详细解释如何操作。

    [旧方法] 安装旧版本的clang-format

    执行以下操作:

    sudo apt update
    

    然后按照顺序逐个尝试,直到找到可行方案:

    sudo apt install clang-format
    sudo apt install clang-format-14.0
    sudo apt install clang-format-13.0
    sudo apt install clang-format-12.0
    sudo apt install clang-format-11.0
    sudo apt install clang-format-10.0
    sudo apt install clang-format-9.0
    sudo apt install clang-format-8.0
    sudo apt install clang-format-7.0
    sudo apt install clang-format-6.0
    sudo apt install clang-format-5.0
    sudo apt install clang-format-4.0
    sudo apt install clang-format-3.6
    sudo apt install clang-format-3.4
    sudo apt install clang-format-3.0
    

    例如,在Ubuntu 14.04上,第一个有效的命令是sudo apt install clang-format-3.6。从Ubuntu 16.04开始,我相信sudo apt install clang-format可以使用,但无论您使用哪个版本的Ubuntu,包括Ubuntu 20.04在内,sudo apt install clang-format都会安装一个相当过时的版本(例如,在Ubuntu 18.04上是版本6.0.0)。因此,要获取最新版本的clang-format,请继续阅读。
    [首选方法] 如何在Ubuntu上安装最新版本的clang-format 超快速指南:

    要直接从LLVM(制作clang C和C ++编译器以及这些工具的母公司)获取clang-formatgit-clang-format,请按照下面我的说明进行操作。但是,这需要下载整个压缩的clang编译器工具集发布版,大约为600 MB,并将其解压缩到一个文件夹中,完全解压缩后大小为约5 GB,这样您才能复制出几兆字节的这些可执行文件。这需要一些时间。

    如果你非常匆忙,并且想要信任我的个人仓库中的可执行文件,我在这里的bin目录下保留了最新版本(14.0.0或更高版本)的clang-formatgit-clang-formathttps://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/tree/main/bin。然而,通常不建议信任他人的可执行文件,因此我提供了以下确切的方法和说明,这是我用来从LLVM(clang-format制造商)的原始发布中提取这些可执行文件的方法,如果您愿意,可以自行按照以下步骤操作。

    如果您想快速下载并安装clang-formatgit-clang-format,请执行以下操作:

    wget https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/raw/main/bin/clang-format
    wget https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/raw/main/bin/git-clang-format
    chmod +x clang-format git-clang-format
    mkdir -p ~/bin
    mv clang-format ~/bin
    mv git-clang-format ~/bin
    
    # log out of Ubuntu and log back in
    
    # ensure it worked and you now have a later version
    clang-format --version  
    
    # Check the help menus
    clang-format -h
    git clang-format -h
    # OR (same thing as the line just above):
    git-clang-format -h
    

    从LLVM官方获取最新版本的完整指南:

    这需要下载整个LLVM clang C和C++编译器工具集,该文件大小约为600 MB,并将其解压缩到一个文件夹中,解压后大小约为5 GB,这样您就可以从其中复制出几MB的可执行文件。

    这些说明最初发布在我的eRCaGuy_CodeFormatter存储库的README中,链接在此处:https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter#installation-instructions

    获取最新的clang-formatgit-clang-format的完整步骤来自最新的官方LLVM发布:

    1. 前往LLVM官方clang发布页面:https://github.com/llvm/llvm-project/releases
    2. 找到适用于您操作系统的最新二进制版本。例如:对于64位Linux,当前版本为(截至2022年3月):clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz。下载链接为:https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz
    3. 使用上面找到的链接执行以下命令:
      url="https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz"
      # 下载文件,请耐心等待:文件大小约为600 MB
      wget "$url"
      # 解压文件,请耐心等待:解压后文件大小约为5 GB!在我的高速电脑上,配备了SSD,大约需要1分钟
      time tar xf clang+llvm*.tar.xz
      # 进入bin目录
      cd clang+llvm*/bin
      # 如果~/bin目录不存在,则创建一个
      mkdir -p ~/bin
      # 将`clang-format`复制到~/bin目录中
      cp clang-format ~/bin
      # 将`git-clang-format`复制到~/bin目录中
      cp git-clang-format ~/bin
      
      # 手动编辑您的~/.profile文件,以确保它包含以下内容,以便将~/bin作为可执行PATH变量的一部分。这是您默认的Ubuntu ~/.profile文件(您在/etc/skel/.profile中有一个备份副本):
      #
      #       # set PATH so it includes user's private bin if it exists
      #       if [ -d "$HOME/bin" ] ; then
      #           PATH="$HOME/bin:$PATH"
      #       fi
      
      # 现在,如果这是您第一次创建和使用~/bin目录,请退出Ubuntu并重新登录。
      
      # 检查您的clang-format版本,以确保它显示您刚刚安装的版本
      clang-format --version
      # 确保它在您的~/bin目录中找到;我的输出是:
      # `/home/gabriel/bin/clang-format`
      which clang-format
      # 也要检查`git-clang-format`
      which git-clang-format
      
      # 检查帮助菜单
      clang-format -h
      git clang-format -h
      # 或者(与上面一行相同):
      git-clang-format -h
      
      # 如果需要,手动删除已提取的文件夹以及下载的*.tar.xz文件
      
    请注意,git 有一个非常棒的功能,它会自动将您系统中任何以 git- 开头的可执行文件视为 git 命令。每当您运行 git some_commandgit 就会自动搜索您系统的 PATH 变量,查找名为 git-some_command 的可执行文件。如果存在,则运行它。因此,只要您将一个名为 git-clang-format 的可执行文件放在路径中,git 就允许您将其作为 git clang-format 运行(在 git 后面没有破折号)。当然,您也可以直接将该可执行文件作为 git-clang-format 运行,因为这是它的文件名。
    请参阅下文,了解推荐的 git clang-format 使用和工作流程。

    其他设置、使用信息和资源:

    如果您刚刚按照上面的步骤操作,现在您已经拥有了最新版本的clang-formatgit-clang-format。后者允许您将git clang-format作为git命令运行,在您提交到git之前自动格式化您的文件。 git-clang-format是由LLVM编写的Python脚本,LLVM是clangC和C ++编译器以及clang-format的制造商。它位于官方的LLVM GitHub存储库中,链接如下: https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/git-clang-format。将其放入您的PATH中;例如:在名为~/bin/git-clang-format的文件中,并将此文件标记为可执行(chmod +x ~/bin/git-clang-format)。 建议使用以下git工作流来调用和使用此git-clang-format自动格式设置程序:
    # 查看您的git更改内容
    git difftool  
    # 或:如果您尚未配置difftool(如meld),则使用`git diff`
    git diff
    # 添加(暂存)要提交的C或C ++文件
    git add my_changed_file.c 
    
    # 运行`git-clang-format`来检查和自动格式化**仅更改的行**。(这将运行
    # `~/bin/git-clang-format` Python脚本)。
    git clang-format
    # 查看`clang-format`刚刚对您更改的行所做的更改
    git difftool
    # 或
    git diff
    
    # 再次添加(暂存)此文件,因为它已被
    # `git-clang-format`更改
    git add my_changed_file.c 
    
    # 提交更改后的文件
    git commit                
    

    其他有用的信息:
    1. git-clang-format python脚本设置说明:https://dx13.co.uk/articles/2015/4/3/Setting-up-git-clang-format.html
    2. git clang-format使用和工作流说明:https://electronjs.org/docs/development/clang-format
    2020年4月更新:我刚刚在GitHub上的一个项目中完全运行了clang-format。我使用./run_clang_format.sh来运行它;下面是如何操作:
    1. 我在这里撰写了一些说明:https://github.com/AmboVent-1690-108/AmboVent#setup
    2. 这是我添加所有内容的PR:https://github.com/AmboVent-1690-108/AmboVent/pull/39。您可以查看一下,看我是如何做到的。
    3. 我从我的dotfiles项目中借鉴了笔记和文件,链接如下:https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/clang-format

    相关

    1. 另请参阅我基于clang-format的项目,链接在此处:eRCaGuy_CodeFormatter

    5

    pip3 install clang-format 可以在Ubuntu 18.04上零配置安装最新的版本9.0。


    我得到了 "ModuleNotFoundError: No module named 'skbuild'" 的错误提示?因此,您需要执行以下操作: "pip3 install scikit-build",并且不要在同一步骤中进行。 - Macke
    这是一个很棒的答案,特别是结合 PyPi 包还提供了 pre-commit 钩子并允许选择特定的 clang-format 版本:https://pypi.org/project/clang-format/ - heiner
    Linux上软件分发的现状...其中pip是安装clang-format的最佳方式,我还发现它是安装最新的CMake的最佳方式(如果需要的话)。 - Timmmm

    2

    您可以做:

    python3 -m pip install --user clang-format
    

    这将安装来自https://pypi.org/project/clang-format/的预编译二进制文件。如果您没有sudo访问权限,或者需要clang-format用于预提交钩子,这相当实用。

    2

    只需使用以下命令:

    sudo apt-get install clang-format
    

    在Ubuntu16.04中运行正常(安装clang-format-3.8设置(1:3.8-2ubuntu4))

    但是当我尝试时

    sudo apt-cache search clang-format
    # it return 
    clang-format - Tool to format C/C++/Obj-C code
    clang-format-3.5 - Tool to format C/C++/Obj-C code
    clang-format-3.6 - Tool to format C/C++/Obj-C code
    clang-format-3.7 - Tool to format C/C++/Obj-C code
    clang-format-3.8 - Tool to format C/C++/Obj-C code
    clang-format-3.9 - Tool to format C/C++/Obj-C code
    clang-format-4.0 - Tool to format C/C++/Obj-C code
    clang-format-5.0 - Tool to format C/C++/Obj-C code
    clang-format-6.0 - Tool to format C/C++/Obj-C code
    clang-format-8 - Tool to format C/C++/Obj-C code
    

    然后尝试

    sudo apt-get install clang-format-8
    

    在当前环境中不存在clang-format命令


    1

    这是一篇更新,因为我在 Ubuntu 21.04 上遇到了上述建议的问题。

    sudo apt install clang-format 安装的是 v.12 版本,而我需要使用旧版本。

    sudo apt install clang-format-9.0 无法工作。(“未找到软件包”)

    以下命令可以正常工作: sudo apt install clang-format-9


    0

    我知道这个问题是关于另一个Linux发行版的。但是以防万一,对于openSUSE,要安装clang-format,您需要运行以下命令:

    zypper in clang-tools
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接