37

我正在尝试使用 clang-tools 特别是 clang-format 在 vi​​m 中进行自动代码格式化,但我无法通过 apt-get search 找到这个工具。

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

4

7 回答 7

44

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

sudo apt install clang-format

于 2018-04-08T14:33:27.003 回答
20

clang-format 在 ubuntu-precise 12.04 中不可用,但在 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 搜索 clang 格式

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

sudo apt-get install clang-format-3.3

于 2013-12-25T08:46:27.937 回答
7

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

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

/etc/apt/sources.list步骤2:根据您的操作系统在末尾附加以下内容:

  • 克星(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
    
  • 赛尼尔(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
    

如果您的操作系统不在此列表中,您可以查看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

Step 5: Use apt install to install the clang-format you want. Then you can use --version to check the installed clang-format. You can install multiple versions of clang-format in a same environment.

$ 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
于 2021-03-16T11:13:13.050 回答
5

安装

尝试(按此顺序,一次一个,直到一个有效):

sudo apt install clang-format
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

然后在此答案下方评论您拥有哪个版本的 Linux 或 Linux Ubuntu,以及哪个命令适合您。

我:

Ubuntu 14.04 与sudo apt install clang-format-3.6

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

  1. 这是一个git-clang-formatpython 脚本,您可以将git clang-format其用作 git 命令:https ://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format 。把它放到你的 PATH 中;例如:在一个名为“~/bin/git-clang-format”的文件中,并将该文件标记为可执行文件(chmod +x ~/bin/git-clang-format)。
  • 调用和使用此文件的 git 工作流程将是:

        git add my_changed_file.c # stage a file
        git clang-format          # let clang-format fix it up (this runs your "~/bin/git-clang-format" Python script)
        git add my_changed_file.c # re-stage it since it's been changed by clang-format
        git commit                # commit the changed file
    
  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

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
于 2019-07-04T00:39:11.500 回答
2

pip3 install clang-format给我零配置的 ubuntu 18.04 上的最新版本 9.0

于 2020-06-05T09:36:04.013 回答
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

ENV 中没有命令 clang-format

于 2020-08-27T08:26:08.353 回答
1

This is an update, because I had troubles with the suggestions above on Ubuntu 21.04

sudo apt install clang-format installs v.12 and I needed anything older.

sudo apt install clang-format-9.0 won't work. ("Package wasn't found")

This worked fine: sudo apt install clang-format-9

于 2021-11-06T09:05:00.273 回答