一、 介绍
Clang-format是一个功能强大的格式化工具。
在vs code通过C/C++扩展后即可使用Clang-format工具进行代码的格式化。
其自带的排版格式主要有:LLVM, Google, Chromium, Mozilla, WebKit等; 利用style参数配置。
通过编写 .clang-format 文件,可以实现代码风格的配置。
二、安装步骤
2.1 安装c/c++拓展
![image-20230323103407208](https://img-blog.csdnimg.cn/img_convert/a693db9ad2531fa354435931b71b0336.png)
2.2 在vscode中安装clang-format应用
![image-20230323103539156](https://img-blog.csdnimg.cn/img_convert/d2589f75c420cd5c87ae3a9cc8ecedd5.png)
2.3 先下载LLVM
Release LLVM 15.0.0 · llvm/llvm-project · GitHub
![image-20230323103619546](https://img-blog.csdnimg.cn/img_convert/ee84cc287d2389686ee15be97e1ed31e.png)
将LLVM安装路径设置到环境变量中
我的是C:\Program Files\LLVM\bin
![image-20230323104653348](https://img-blog.csdnimg.cn/img_convert/3837403955a4d98e37c4a2ff579f6a5b.png)
2.4 vscode设置
在settings里搜索format,然后按照如下配置,将默认格式化工具改为clang-format,勾选format on save保存时再修改格式
![image-20230323103926362](https://img-blog.csdnimg.cn/img_convert/e3a344e2c2e2025b6b60a2b438d42269.png)
进入clang-format configuration选项,将assume filename改为./.clang-format
作用是在当前工程目录下生成 .clang-format配置文件
Clang-format: 设置为Google模式(也可以选择其他)
![image-20230323104143357](https://img-blog.csdnimg.cn/img_convert/9d60830bf5cdb2fe260fa0a256a2284c.png)
C_Cpp: Clang_format_style,格式化方式,若为file,则调用在workspace中的.clang-format
C_Cpp: Clang_format_fallback Style ,若C_Cpp: Clang_format_style为file,但无.clang-format文件则按照此处规则。
![image-20230323104355942](https://img-blog.csdnimg.cn/img_convert/c811bd434e585a7b02e467fb81656952.png)
2.5 使用方式
若当前工程下有配置文件,可以直接右键选择格式化当前文档
![image-20230323104942986](https://img-blog.csdnimg.cn/img_convert/2507cb30a51030c6dfdd661d1f20f6f3.png)
不存在则在终端输入:
clang-format -style=llvm -dump-config > .clang-format
或者
clang-format -style=Google -dump-config > .clang-format
生成 .clang-format
注意该文件需要utf-8格式
![image-20230323105304971](https://img-blog.csdnimg.cn/img_convert/80e64b6dc1f64a275882991c686ad304.png)
参考资料:
【VS】visual studio 代码格式化工具–clang-format_vs格式化代码_伐尘的博客-CSDN博客
VS CODE中C/C++的Clang-format的使用_vs clang-format_Huskiey的博客-CSDN博客
vscode设置C++代码格式化(Clang-Format) - 简书 (jianshu.com)
de设置C++代码格式化(Clang-Format) - 简书 (jianshu.com)](https://www.jianshu.com/p/1e157fc0d18c)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)