(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
参考:https://zhuanlan.zhihu.com/p/513588054

1、关于clang-format

clang-format用于格式化代码,并能够指定采用的格式名称,例如google,microsoft,GNU代码样式格式等。

在github上一些c/c++代码提交时,有些库明确要求代码格式需要使用clang-fomat格式化。

例如下面这个摘抄的说明:

Consistent Style: This project conforms to the Google C++ Style Guide. To ensure your changes are properly formatted please run:

clang-format -i --style=file <file>

2、centos上安装

首先yum search,找到远程库中有的clang-format包

$ sudo yum search clang-format
Last metadata expiration check: 1:27:55 ago on Tue 29 Nov 2022 08:27:57 AM CST.
Name & Summary Matched: clang-format
git-clang-format.x86_64 : Integration of clang-format for git
git-clang-format.i686 : Integration of clang-format for git

然后安装现有的clang-format包,就选git-clang-format.x86_64这个包吧

$ sudo yum install git-clang-format.x86_64
Last metadata expiration check: 1:24:22 ago on Tue 29 Nov 2022 08:27:57 AM CST.
Dependencies resolved.

Complete!

clang-format使用

查看clang-format的版本
$ clang-format --version
clang-format version 12.0.1 (Red Hat 12.0.1-4.module_el8.5.0+1025+93159d6c)

3、使用-格式化例子

例如下面代码的格式化:

$ cat main.c #include  "stdio.h"
int main()
{
printf("hello world");
return 0;
}

查看格式化后输出: 格式化后对齐好了很多

$ clang-format main.c
#include "stdio.h"
int main() {printf("hello world");return 0;
}

直接在源文件上格式化代码

$ clang-format -i main.c
$ cat main.c
#include "stdio.h"
int main() {printf("hello world");return 0;
}

指定格式化代码的风格: currently supports LLVM, GNU, Google, Chromium, Microsoft, Mozilla, WebKit.

$ clang-format -style=Google main.c
#include "stdio.h"
int main() {printf("hello world");return 0;
}
$ clang-format -style=Microsoft main.c
#include "stdio.h"
int main()
{printf("hello world");return 0;
}

附:使用帮助

命令帮助说明:

$ clang-format --help
OVERVIEW: A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.If no arguments are specified, it formats the code from standard input
and writes the result to the standard output.
If <file>s are given, it reformats the files. If -i is specified
together with <file>s, the files are edited in-place. Otherwise, the
result is written to the standard output.USAGE: clang-format [options] [<file> ...]OPTIONS:Clang-format options:--Werror                   - If set, changes formatting warnings to errors--Wno-error=<value>        - If set don't error out on the specified warning type.=unknown                 -   If set, unknown format options are only warned about.This can be used to enable formatting, even if theconfiguration contains unknown (newer) options.Use with caution, as this might lead to dramaticallydiffering format depending on an option beingsupported or not.--assume-filename=<string> - Override filename used to determine the language.When reading from stdin, clang-format assumes thisfilename to determine the language.--cursor=<uint>            - The position of the cursor when invokingclang-format from an editor integration--dry-run                  - If set, do not actually make the formatting changes--dump-config              - Dump configuration options to stdout and exit.Can be used with -style option.--fallback-style=<string>  - The name of the predefined style used as afallback in case clang-format is invoked with-style=file, but can not find the .clang-formatfile to use.Use -fallback-style=none to skip formatting.--ferror-limit=<uint>      - Set the maximum number of clang-format errors to emit before stopping (0 = no limit). Used only with --dry-run or -n-i                         - Inplace edit <file>s, if specified.--length=<uint>            - Format a range of this length (in bytes).Multiple ranges can be formatted by specifyingseveral -offset and -length pairs.When only a single -offset is specified without-length, clang-format will format up to the endof the file.Can only be used with one input file.--lines=<string>           - <start line>:<end line> - format a range oflines (both 1-based).Multiple ranges can be formatted by specifyingseveral -lines arguments.Can't be used with -offset and -length.Can only be used with one input file.-n                         - Alias for --dry-run--offset=<uint>            - Format a range starting at this byte offset.Multiple ranges can be formatted by specifyingseveral -offset and -length pairs.Can only be used with one input file.--output-replacements-xml  - Output replacements as XML.--sort-includes            - If set, overrides the include sorting behavior determined by the SortIncludes style flag--style=<string>           - Coding style, currently supports:LLVM, GNU, Google, Chromium, Microsoft, Mozilla, WebKit.Use -style=file to load style configuration from.clang-format file located in one of the parentdirectories of the source file (or currentdirectory for stdin).Use -style="{key: value, ...}" to set specificparameters, e.g.:-style="{BasedOnStyle: llvm, IndentWidth: 8}"--verbose                  - If set, shows the list of processed filesGeneric Options:--help                     - Display available options (--help-hidden for more)--help-list                - Display list of available options (--help-list-hidden for more)--version                  - Display the version of this program

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

安装使用clang-format相关推荐

  1. clang format 官方文档自定义参数介绍(中英文)

    官方文档:http://clang.llvm.org/docs/ClangFormatStyleOptions.html 中文 在代码中配置样式当使用 clang::format::reformat( ...

  2. 安装LLVM+Clang教程

    目录 前言 一.LLVM简介 二.LLVM+Clang的安装 前言 最近可能要学习编译器相关的知识,因此总结一下LLVM的安装方法,以防忘记如何安装. 接下来就是正式的安装过程了... ------- ...

  3. 【MSYS2】Windows 无MSVC 安装 MinGW Clang

    要点 Windows 通过MSYS2安装Clang Clangd Clang不使用MSVC MinGW Clang mingw clang 一.安装MSYS2 下载MSYS2安装包,如msys2-x8 ...

  4. clang format

    clang format入门 一:VsCode下载 二:ubuntu下载 三:使用(common) 导出 .clang-format 格式化文件 自己找了一些资料,感觉不是太完整,零零碎碎自己来总结一 ...

  5. carla安装之clang版本问题

    carla要求的是clang8.0,我最一开始没有关注到这个. 使用的方法是 sudo apt-get install llvm 先安装llvm,然后 sudo apt-get install cla ...

  6. 关于Ubuntu18版本下新安装Qtcreator编译报错:cannot run compiler clang++ output的解决办法

    第三次入坑 前两次本以为记住了,结果第三次再次在Linux下安装qtcreator依然捣鼓搞了半天,才决定将这些步骤记下来,<\笑哭> 1.在你安装好qtcreator之后,新建有一个qt ...

  7. Macos 安装Clang和LLVM过程记录

    Macos 安装Clang和LLVM过程记录 文章目录 Macos 安装Clang和LLVM过程记录 安装Cmake 安装homebrew 安装doc 安装svn 下载LLVM 下载Clang 下载C ...

  8. VSCode 配置 C++:VSCode + Clang + Clangd + LLDB + CMake + Git

    2022/5/17:新版教程已发布,点此查看. 2022/5/17:新版教程已发布,点此查看. 2022/5/17:新版教程已发布,点此查看. 2022/2/4:本文章不再予以更新 -1- 前言 ①目 ...

  9. Mac下安装VScode和配置C++环境

    Mac下安装VScode和配置C++环境 1. 下载.安装VScode软件 2. 配置C/C++环境 参考文章 1. 下载.安装VScode软件 VScode的下载地址: https://code.v ...

  10. 在CentOS 6.6 64bit上安装vim智能补全插件YouCompleteMe

    简介 YouCompleteMe是一个随键而动的.支持模糊搜索的.高速补全的插件.YCM由google公司搜索项目组的软件工程师Strahinja Val Markovic所开发,YCM后端调用lib ...

最新文章

  1. JFoenix: JavaFX与Google Material Design
  2. 对AFTER触发器的一些整理
  3. 科普漫画:什么是区块链?
  4. Lync Server 2010标准版系列PART6:启用Lync
  5. c语言线性表库函数大全,数据结构(C语言版)-线性表习题详解
  6. upper_bound 和lower_bound彻底搞懂
  7. vue-cli history 本地开发刷新页面丢失_react 开发入门
  8. 软件测试方法和技术_软件测试学习怎么入门?附真实学习方法
  9. 居家隔离的第5天,我们整了个六边形战士
  10. JS(获取浏览器高度)
  11. seo和sem的区别是什么?网站seo具体怎么做?
  12. 在Windows x64中加载驱动
  13. 电脑桌面app客户端、微信公众号-小程序测试方法
  14. Flutter仿学习强国填空题
  15. 【Pycharm教程】了解 PyCharm Python 控制台
  16. hdu2198 How many elements you must throw out? C语言动态规划题
  17. 计算机的硬件系统和软件系统
  18. 【数据治理】电力行业数据治理解决方案
  19. ArcGIS API For JavaScript学习笔记
  20. C++如何定义一个长度超过一百万的数组

热门文章

  1. 与甲类功率放大方式相比,乙类互补对称功放的主要优点是( C)。a.不用输出变压器b,不用输出端大电容c,效率高d.无交越失真...
  2. 回归分析模型检验评估
  3. 阿里云服务器5m带宽建网站支持多少人同时在线?并发数计算
  4. Stata17 MP/SE/BE(附赠常用模型代码和熵值法计算代码)
  5. 合肥耀之阳电商:店铺运营需要注意哪些相关事项
  6. ASP.NET 2.0揭秘(中文版)国庆献礼
  7. Matplotlib赋予Python数据一双眼
  8. 什么是模块化开发?模块化开发有哪些方法
  9. [Haxe] Reflect
  10. bentoml部署深度学习模型(一)