如何在ubuntu中安装clang-format

Val*_*lus 20 vim code-formatting clang clang-format

我试图使用clang-tools特别是clang-format在vim中进行自动代码格式化,但我找不到这个工具使用apt-get搜索.

以前是否有人遇到过此问题,您有什么建议吗谢谢

Eri*_*ang 18

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

sudo apt install clang-format

  • 在Ubuntu 18.04上,通过调用`clang-format-6.0`或任何版本来使用该包. (5认同)

use*_*375 14

clang-format在ubuntu-precise 12.04中不可用,但可以在ubuntu中找到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 
Run Code Online (Sandbox Code Playgroud)

然后你应该先用下面的命令搜索clang-format

sudo apt-cache搜索clang-format

然后,您可以安装要安装的版本,例如;

sudo apt-get install clang-format-3.3

  • ``sudo apt-get install clang-format-3.4''在Ubuntu 14.04 LTS中工作。这是在输入后:`sudo apt-get install clang-3.4` (3认同)
  • apt-cache搜索不需要sudo。 (2认同)

Gab*_*les 11

该答案自投反对票以来已被重写,是截至 2022 年 4 月该问题的最新答案。这也是解释如何clang-format直接从 LLVM 获取最新版本的唯一答案,人们谁做到了。

要安装最新版本的clang-formatgit-clang-format(可作为 运行git clang-format):

概括

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

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

做:

sudo apt update
Run Code Online (Sandbox Code Playgroud)

然后按以下顺序尝试,一次一个,直到其中一个有效:

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
Run Code Online (Sandbox Code Playgroud)

例如,在 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

获取最新版本的超级快速说明

要直接获取clang-formatLLVM git-clang-format(制作clangC 和 C++ 编译器以及这些工具的父组织),请按照下面我的说明进行操作。但是,这需要下载大约 600 MB 的整个压缩clang编译器工具集版本,并将其解压到完全解压后大约 5 GB 的文件夹中,这样您就可以复制出几兆字节的这些可执行文件。这需要一些时间。

因此,如果您非常着急,并且想要信任我个人存储库中的可执行文件,我会在我的存储库目录中保留clang-format两者的最新版本(14.0.0 或更高版本) :https:// github.com/ElectricRCAaircraftGuy/eRCaGuy_CodeFormatter/tree/main/bin。然而,通常建议信任其他人的可执行文件,因此我在下面提供了我用来从 LLVM(的制造商)的原始版本中提取这些可执行文件的确切方法和说明,如果您愿意,您可以自行遵循反而。git-clang-formatbinclang-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
Run Code Online (Sandbox Code Playgroud)

直接从 LLVM 制造商那里获取最新版本的完整说明:

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

这些说明最初发布在我的 eRCaGuy_CodeFormatter 存储库的自述文件中: https: //github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter#installation-instructions

clang-format获取最新版本以及git-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"
    # download it; be patient: the file is ~600 MB
    wget "$url"
    # extract it; be patient: this could take several minutes, as the file is
    # about 5 GB when extracted! On my high-speed computer with SSD, it took
    # ~1 minute
    time tar xf clang+llvm*.tar.xz
    # cd into the bin dir
    cd clang+llvm*/bin
    # make a ~/bin dir if it doesn't exist yet
    mkdir -p ~/bin
    # copy out `clang-format` into your ~/bin dir
    cp clang-format ~/bin
    # copy out `git-clang-format` into your ~/bin dir
    cp git-clang-format ~/bin
    
    # Manually edit your ~/.profile file to ensure it contains the following in
    # order to ensure ~/bin is part of your executable PATH variable. This is
    # part of your default Ubuntu ~/.profile file (for which you have a backup
    # copy in /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
    
    # Now, if this is your first time creating and using the ~/bin dir, log out
    # of Ubuntu and log back in. 
    
    # check your clang-format version to ensure it shows the version you just
    # installed
    clang-format --version
    # Ensure it is found in your ~/bin dir; my output to this command is:
    # `/home/gabriel/bin/clang-format`
    which clang-format
    # Check `git-clang-format` too
    which git-clang-format
    
    # Check the help menus
    clang-format -h
    git clang-format -h
    # OR (same thing as the line just above):
    git-clang-format -h
    
    # manually delete the the extracted folder if desired, and the
    # downloaded *.tar.xz file as well, if desired
    
    Run Code Online (Sandbox Code Playgroud)

请注意,它git有一个非常简洁的功能,可以使路径中以开头的任何git-可执行文件自动被视为git命令。每当您运行 时git some_commandgit都会自动在系统的所有PATH变量中搜索名为 的可执行文件git-some_command。如果存在,git则运行它。因此,仅仅凭借您在名为 的路径中放置一个可执行文件的事实git-clang-formatgit您就可以将其运行为git clang-format(不带后面的破折号git)。当然,您也可以选择运行与 相同的可执行文件git-clang-format,因为这就是它的文件名。

请参阅下文了解推荐的git clang-format用法和工作流程。

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

  1. 如果您按照上面的步骤操作,您现在拥有 和 的最新clang-format 版本 git-clang-format。有了后者,您可以在将git clang-format文件git提交到git. git-clang-format是由 LLVM(clangC 和 C++ 编译器以及clang-format. 它位于官方 LLVM GitHub 存储库中:https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/git-clang-format。将其放入您的 PATH 中;ex: 在一个名为 的文件中~/bin/git-clang-format,并将该文件标记为可执行文件 ( chmod +x ~/bin/git-clang-format)。
    1. 调用和使用此自动格式化程序的推荐gitgit-clang-format工作流程将是:
      # See what git changes you have
      git difftool  
      # OR: `git diff` if you haven't configured a difftool such as meld
      git diff
      # Add (stage) a C or C++ file to be committed
      git add my_changed_file.c 
      
      # Run `git-clang-format` to have `clang-format` check and
      # auto-format **just your changed lines**. (This runs the
      # `~/bin/git-clang-format` Python script).
      git clang-format
      # See what changes `clang-format` just made to your changed lines
      git difftool
      # OR
      git diff
      
      # Add (stage) this file again since it's been changed by
      # `git-clang-format`
      git add my_changed_file.c 
      
      # commit the changed file
      git commit                
      
      Run Code Online (Sandbox Code Playgroud)
  2. 其他可能有用的信息:
    1. git-clang-formatpython脚本设置说明: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
  3. 2020 年 4 月更新:我刚刚clang-format在 GitHub 上的一个项目上启动并完全运行。我用./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,零配置


oca*_*vue 5

现在,您可以直接apt install clang-format在 Debian/Ubuntu 中使用来安装clang-format. 然而,clang-format由 Debian/Ubuntu 提供的已经过时了。例如,clang-format来自 Ubuntu 18.04的最新版本是v7但已经达到了最新的稳定版本v12。您可以clang-format按照以下步骤安装最新版本:

第 1 步:运行 wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -

第二步:/etc/apt/sources.list根据你的操作系统在末尾追加以下内容:

  • 克星(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
    
    Run Code Online (Sandbox Code Playgroud)
  • 靶心(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
    
    Run Code Online (Sandbox Code Playgroud)
  • 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
    
    Run Code Online (Sandbox Code Playgroud)
  • 仿生(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
    
    Run Code Online (Sandbox Code Playgroud)

如果您的操作系统不在此列表中,您可以查看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
Run Code Online (Sandbox Code Playgroud)

第五步:使用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
Run Code Online (Sandbox Code Playgroud)