vs code 技巧:使用clang-format来格式化代码

下载C/C++插件

配置format

点开插件的配置小齿轮图标进行设置,找到Formatting

配置方法

配置可以直接在GUI界面改,也可以使用专门的配置文件。一般使用专门的配置文件,这样移植效率高。

要使用配置文件,需要修改Clang_format_style选项,将配置文件.clang-format文件路径添加进去
其他保持默认即可,有兴趣也可以研究下是什么

  1. C_Cpp: Clang_format_fallback Style :当设置clang-format且没有.clang-format文件时,会使用这里选择的默认设置来设置格式。
  2. C_Cpp: Clang_format_path:这个是clang-format.exe的绝对路径。C:\Users\Administrator.vscode\extensions\ms-vscode.cpptools-1.13.9-win32-x64\LLVM\bin\clang-format.exe
  3. C_Cpp: Clang_format_style:这个是.clang-format文件的路径。file:C:\Users\Administrator.vscode\extensions\ms-vscode.cpptools-1.13.9-win32-x64\LLVM\bin/.clang-format。若这里只设置为file,则从当前目录或父目录中的 .clang-format 文件加载样式,若当前目录或父目录中也没有.clang-format则使用C_Cpp: Clang_format_fallback Style中的设置。

配置选项

内容需要和官网内容说明保持一样,官网地址:https://clang.llvm.org/docs/ClangFormatStyleOptions.html
示例如下:

Language:        Cpp
Standard: Auto
ColumnLimit:     120

IndentWidth: 4

#花括号包裹方式
BreakBeforeBraces: Custom
BraceWrapping:
  AfterEnum: true
  AfterStruct: true
  AfterFunction : true

#对齐方式
AlignConsecutiveAssignments: AcrossEmptyLinesAndComments

AlignConsecutiveMacros:
  Enabled: true
  AcrossEmptyLines: true
  AcrossComments: true

建议先用某一标准style,再根据习惯个别修改

出错

一般来说直接安装vscode中的插件是无法直接运行的,还需要先安装LLVM,并把对应的clang-format程序执行路径配置到插件中

posted on   不回本不改名  阅读(9009)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示