在 Visual Studio Code 中配置 Clang-Format 格式化 (排版) 工具
在 Visual Studio Code 中配置 Clang-Format 格式化 {排版} 工具1. Install clang-format on Linux2. Create the .clang-format file3. Install the Clang-Format extension4. Configure ClangFormat options5. Code formatting6. 自定义 clang-format 代码格式规范ReferencesClang Formathttps://clang.llvm.org/docs/ClangFormat.htmlClang-Format Style Optionshttps://clang.llvm.org/docs/ClangFormatStyleOptions.htmlclang-formatis located inclang/tools/clang-formatand can be used to format C/C/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.1. Installclang-formaton LinuxInstallclang-formatby entering the following commandsyongqiangyongqiang:~$ sudo apt update yongqiangyongqiang:~$ sudo apt-cache search clang-format # This command will list all the available versions. yongqiangyongqiang:~$ sudo apt install clang-format -yDisplay the version of this programyongqiangyongqiang:~$ clang-format --version clang-format version 10.0.0-4ubuntu1 yongqiangyongqiang:~$ yongqiangyongqiang:~$ whereis clang-format clang-format: /usr/bin/clang-format /usr/share/man/man1/clang-format.1.gz yongqiangyongqiang:~$ yongqiangyongqiang:~$ which clang-format /usr/bin/clang-format yongqiangyongqiang:~$Ifclang-formatis in your system path, you shouldn’t need to do anything.Create a symbolic link for clang-format:sudo ln -s /usr/bin/clang-format-9.0 /usr/bin/clang-format sudo ln -s /usr/bin/clang-format-6.0 /usr/bin/clang-format2. Create the.clang-formatfileAn easy way to get a valid.clang-formatfile containing all configuration options of a certain predefined style is:clang-format -stylegoogle -dump-config .clang-format clang-format -stylellvm -dump-config .clang-formatyongqiangyongqiang:~/software$ clang-format -stylegoogle -dump-config .clang-format yongqiangyongqiang:~/software$ ll total 16 drwxr-xr-x 2 yongqiang yongqiang 4096 May 8 22:46 ./ drwxr-xr-x 13 yongqiang yongqiang 4096 May 7 10:10 ../ -rw-r--r-- 1 yongqiang yongqiang 4523 May 8 22:46 .clang-format yongqiangyongqiang:~/software$The style used for all options not specifically set in the configuration. Possible values:LLVM: A style complying with the LLVM coding standardsGoogle: A style complying with Google’s C style guideChromium: A style complying with Chromium’s style guideMozilla:A style complying with Mozilla’s style guideWebKit: A style complying with WebKit’s style guideMicrosoft: A style complying with Microsoft’s style guideGNU: A style complying with the GNU coding standards3. Install the Clang-Format extension4. Configure ClangFormat optionsFile - Preferences - Settings - Remote [*] - Clang-Format configurationAssume Filename:/home/yongqiang/software/.clang-formatWhen reading from stdin, clang-format assumes this filename to look for a style config file (with-stylefile) and to determine the language.Executable:/usr/bin/clang-formatclang-format executable pathFallback Style:Googleclang-format fallback style. (-fallback-stylevalue, value can be none, LLVM, Google, Chromium, Mozilla, WebKit)Style:fileclang-format style.(-stylevalue, value can be file, LLVM, Google, Chromium, Mozilla, WebKit or json configure)5. Code formattingThe C/C extension for Visual Studio Code supports source code formatting using clang-format which is included with the extension.Format Document With…Configure Default Formatter…Clang-FormatYou can format an entire file withFormat Document(ShiftAltF) or just the current selection withFormat Selection(CtrlK CtrlF) in right-click context menu. You can also configure auto-formatting with the following settings:editor.formatOnSave- to format when you save your file.editor.formatOnType- to format as you type (triggered on the;character).By default, the clang-format style is set to “file” which means it looks for a.clang-formatfile inside your workspace. If the.clang-formatfile is found, formatting is applied according to the settings specified in the file. If no.clang-formatfile is found in your workspace, formatting is applied based on a default style specified in theC_Cpp.clang_format_fallbackStylesetting instead.6. 自定义 clang-format 代码格式规范可以将生成的.clang-format文件提交到 Git 仓库。这样团队里的每一个人在按下“格式化代码”快捷键时代码风格都能保持绝对一致。clang-format -stylegoogle -dump-config .clang-format以 Google 的代码风格为蓝本生成一份完整的 clang-format 配置文件 (命名为.clang-format) 并保存到当前目录下。-stylegoogle: 指定基础的代码风格-dump-config: 导出/转储完整的配置参数 .clang-format: 重定向输出到文件yongqiangyongqiang:~/software$ clang-format -stylegoogle -dump-config .clang-format yongqiangyongqiang:~/software$ ll total 16 drwxr-xr-x 2 yongqiang yongqiang 4096 May 8 22:46 ./ drwxr-xr-x 13 yongqiang yongqiang 4096 May 7 10:10 ../ -rw-r--r-- 1 yongqiang yongqiang 4523 May 8 22:46 .clang-format yongqiangyongqiang:~/software$ yongqiangyongqiang:~/software$ cat ./.clang-format --- Language: Cpp # BasedOnStyle: Google AccessModifierOffset: -1 AlignAfterOpenBracket: Align AlignConsecutiveMacros: false AlignConsecutiveAssignments: false AlignConsecutiveDeclarations: false AlignEscapedNewlines: Left AlignOperands: true AlignTrailingComments: true AllowAllArgumentsOnNextLine: true AllowAllConstructorInitializersOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortBlocksOnASingleLine: Never AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: All AllowShortLambdasOnASingleLine: All AllowShortIfStatementsOnASingleLine: WithoutElse AllowShortLoopsOnASingleLine: true AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: true AlwaysBreakTemplateDeclarations: Yes BinPackArguments: true BinPackParameters: true BraceWrapping: AfterCaseLabel: false AfterClass: false AfterControlStatement: false AfterEnum: false AfterFunction: false AfterNamespace: false AfterObjCDeclaration: false AfterStruct: false AfterUnion: false AfterExternBlock: false BeforeCatch: false BeforeElse: false IndentBraces: false SplitEmptyFunction: true SplitEmptyRecord: true SplitEmptyNamespace: true BreakBeforeBinaryOperators: None BreakBeforeBraces: Attach BreakBeforeInheritanceComma: false BreakInheritanceList: BeforeColon BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true ColumnLimit: 80 CommentPragmas: ^ IWYU pragma: CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true DeriveLineEnding: true DerivePointerAlignment: true DisableFormat: false ExperimentalAutoDetectBinPacking: false FixNamespaceComments: true ForEachMacros: - foreach - Q_FOREACH - BOOST_FOREACH IncludeBlocks: Regroup IncludeCategories: - Regex: ^ext/.*\.h Priority: 2 SortPriority: 0 - Regex: ^.*\.h Priority: 1 SortPriority: 0 - Regex: ^.* Priority: 2 SortPriority: 0 - Regex: .* Priority: 3 SortPriority: 0 IncludeIsMainRegex: ([-_](test|unittest))?$ IncludeIsMainSourceRegex: IndentCaseLabels: true IndentGotoLabels: true IndentPPDirectives: None IndentWidth: 2 IndentWrappedFunctionNames: false JavaScriptQuotes: Leave JavaScriptWrapImports: true KeepEmptyLinesAtTheStartOfBlocks: false MacroBlockBegin: MacroBlockEnd: MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCBinPackProtocolList: Never ObjCBlockIndentWidth: 2 ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: true PenaltyBreakAssignment: 2 PenaltyBreakBeforeFirstCallParameter: 1 PenaltyBreakComment: 300 PenaltyBreakFirstLessLess: 120 PenaltyBreakString: 1000 PenaltyBreakTemplateDeclaration: 10 PenaltyExcessCharacter: 1000000 PenaltyReturnTypeOnItsOwnLine: 200 PointerAlignment: Left RawStringFormats: - Language: Cpp Delimiters: - cc - CC - cpp - Cpp - CPP - c - C CanonicalDelimiter: BasedOnStyle: google - Language: TextProto Delimiters: - pb - PB - proto - PROTO EnclosingFunctions: - EqualsProto - EquivToProto - PARSE_PARTIAL_TEXT_PROTO - PARSE_TEST_PROTO - PARSE_TEXT_PROTO - ParseTextOrDie - ParseTextProtoOrDie CanonicalDelimiter: BasedOnStyle: google ReflowComments: true SortIncludes: true SortUsingDeclarations: true SpaceAfterCStyleCast: false SpaceAfterLogicalNot: false SpaceAfterTemplateKeyword: true SpaceBeforeAssignmentOperators: true SpaceBeforeCpp11BracedList: false SpaceBeforeCtorInitializerColon: true SpaceBeforeInheritanceColon: true SpaceBeforeParens: ControlStatements SpaceBeforeRangeBasedForLoopColon: true SpaceInEmptyBlock: false SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 2 SpacesInAngles: false SpacesInConditionalStatement: false SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false SpaceBeforeSquareBrackets: false Standard: Auto StatementMacros: - Q_UNUSED - QT_REQUIRE_VERSION TabWidth: 8 UseCRLF: false UseTab: Never ... yongqiangyongqiang:~/software$Yongqiang Cheng 自定义Google 风格可能 90% 符合你的习惯但有些细节你想改 (比如 Google 默认缩进 2 空格你想改成 4 空格)。生成这个文件后你可以直接打开它修改其中的某几行。ColumnLimit: 120 (单行代码最大长度限制)The column limit.允许单行代码最多包含 120 个字符。超过 120 个字符时clang-format 才会强制将代码拆分换行。IndentWidth: 4 (缩进宽度)The number of columns to use for indentation.每一次代码缩进都使用 4 个字符宽度。TabWidth: 4 (Tab 键宽度)The number of columns used for tab stops.定义一个制表符 (Tab 键) 在显示时应该占据几个字符的宽度。References[1] Yongqiang Cheng (程永强), https://yongqiang.blog.csdn.net/[2] Edit C in Visual Studio Code, https://code.visualstudio.com/docs/cpp/cpp-ide[3] Clang-Format Style Options, https://clang.llvm.org/docs/ClangFormatStyleOptions.html[4] Options, Text Editor, C/C, Formatting, https://learn.microsoft.com/en-us/visualstudio/ide/reference/options-text-editor-c-cpp-formatting[5] 在 Visual Studio Code 中配置 Clang-Format 格式化 (排版) 工具, https://mp.weixin.qq.com/s/b4Qv78TleWn400PINP_Xzw

相关新闻

C++与Easy-X实战:从零开发贪吃蛇游戏,掌握图形化编程与游戏逻辑

C++与Easy-X实战:从零开发贪吃蛇游戏,掌握图形化编程与游戏逻辑

1. 项目概述:为什么选择C与Easy-X来复刻贪吃蛇?如果你是一名C的初学者,或者已经学完了基础语法,正苦于找不到一个能串联起所有知识点的实战项目,那么“贪吃蛇”绝对是一个完美的起点。它逻辑清晰、体量适中&#xff0c…

2026/10/16 19:15:32 阅读更多 →
Claude Code:从代码补全到深度理解的AI编程代理实践指南

Claude Code:从代码补全到深度理解的AI编程代理实践指南

如果你是一名开发者,最近可能已经感受到了AI编程助手带来的效率革命。但当你面对一个全新的代码库,需要快速理解架构、修复bug或实现功能时,传统的代码补全工具往往显得力不从心。这正是Claude Code试图解决的核心痛点——它不仅仅是一个代码…

2026/10/17 15:34:01 阅读更多 →
AI时代浏览器架构变革:六款新一代AI代理浏览器深度解析

AI时代浏览器架构变革:六款新一代AI代理浏览器深度解析

如果你还在用Chrome或Safari进行日常开发,可能已经感受到了某种瓶颈——页面白屏闪烁、插件冲突、内存泄漏,甚至简单的视频播放都无法正常调用GPU。这些看似琐碎的问题背后,其实是传统浏览器架构在面对现代Web应用时的力不从心。更关键的是&a…

2026/10/15 13:52:58 阅读更多 →

最新新闻

MySQL学生成绩管理系统数据库设计:需求分析、建表、视图与存储过程完整方案

MySQL学生成绩管理系统数据库设计:需求分析、建表、视图与存储过程完整方案

简介:这份资源是北邮研一数据库课程大作业的完整详解文档,面向正在修读数据库系统课程、需要完成课程设计的研究生及高年级本科生。内容围绕学生成绩管理系统展开,覆盖需求分析、数据库设计、ER图绘制、逻辑结构设计与建表程序等完整流程&…

2026/10/17 16:15:27 阅读更多 →
Unity运行时导入3D模型:基于TriLib源码的模型加载与替换实践

Unity运行时导入3D模型:基于TriLib源码的模型加载与替换实践

简介:这套基于 Unity3D C# 与 TriLib 插件实现的运行时 3D 模型导入加载源码工程,面向需要为游戏或应用添加实时模型替换、关卡/场景编辑器以及 AR/VR 可视化等功能的 Unity 开发者。压缩包共 879 个文件,约 26.37MB,以 C# 脚本、…

2026/10/17 16:15:27 阅读更多 →
数据库关系代数与SQL实战入门:从课本习题到可执行查询

数据库关系代数与SQL实战入门:从课本习题到可执行查询

简介:本资源是《数据库系统概论(第5版)》王珊、萨师煊主编教材的配套课后习题详细答案,面向高校计算机、软件工程、信息管理等专业本科生及数据库初学者,用于巩固关系模型、关系代数、SQL语言等核心理论知识&#xff0…

2026/10/17 16:15:27 阅读更多 →
Unity3D数字孪生案例:从模型导入到数据驱动的完整落地路径

Unity3D数字孪生案例:从模型导入到数据驱动的完整落地路径

简介:面向数字孪生初学者与全栈开发爱好者的一套 Unity3d(WebGL)小案例代码包,聚焦硬件终端、webGL、Web端与服务端之间的数据交互链路,帮助解决 Unity3d 建模在网页端运行及 IoT 数据回流三维场景的问题。压缩包共3个…

2026/10/17 16:15:27 阅读更多 →
WordPress故障排查实战:从白屏、数据库到迁移陷阱一次说清

WordPress故障排查实战:从白屏、数据库到迁移陷阱一次说清

说起WordPress问题,我这几年遇到最多的不是某个单一故障,而是一堆"莫名其妙"的现象:前台白屏、后台登录死循环、文章保存按钮转圈、图片传不上去、网站被搜索引擎收录了一堆垃圾页面、搬家之后样式全丢。很多朋友的第一反应是"…

2026/10/17 16:15:27 阅读更多 →
Vim+GCC+库文件:打通Linux开发工具链的完整闭环

Vim+GCC+库文件:打通Linux开发工具链的完整闭环

我见过太多刚切到 Linux 的开发者,第一周就被劝退。倒不是 C 语言难,而是卡在了工作流上:在终端里不知道怎么快速改代码,写完程序不知道怎么编译,编译完了遇到各种链接报错又一头雾水。说白了,就是 Vim 操作…

2026/10/17 16:14:27 阅读更多 →

日新闻

DeepSeek选型与部署:V3/R1、Ollama、API全攻略

DeepSeek选型与部署:V3/R1、Ollama、API全攻略

简介:《DeepSeek使用方法.pdf》是一份面向DeepSeek入门及进阶用户的完整使用指南,重点围绕V3与R1两款模型的能力差异与适用场景展开,帮助读者根据任务类型与成本选择合适的模型。资源共1个PDF文件,压缩包大小约311KB,篇…

2026/10/17 0:00:25 阅读更多 →
200+Deepseek润色指令拆解:从底层原理到场景落地的完整指南

200+Deepseek润色指令拆解:从底层原理到场景落地的完整指南

简介:这是一份聚焦DeepSeek提示词优化的《200Deepseek润色指令》PDF文档,共收录两百余条可直接套用的润色指令,涵盖语法修正、风格统一、逻辑梳理、表达精炼等维度,并针对商务写作、学术论文、广告文案等场景给出差异化建议。文档…

2026/10/17 0:00:25 阅读更多 →
OpenCV轮廓法物体计数系统:轻量、可控、可落地的工业级实现

OpenCV轮廓法物体计数系统:轻量、可控、可落地的工业级实现

简介:本资源是一份基于OpenCV的本科课程设计完整报告,面向计算机视觉初学者、图像处理学习者及高校通信/信息类专业学生,解决物体智能计数与检测结果结构化记录的实际问题。项目覆盖图像预处理、轮廓检测、边界框绘制、轨迹栏交互式参数调节、…

2026/10/17 0:00:25 阅读更多 →

周新闻

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

简介:基于 ARIMA、LSTM、Transformer 等模型的流感时间序列预测 Python 源码,面向计算机相关专业课程设计与期末大作业学生,以及项目实战学习者。内容覆盖预处理、平稳性检验、定阶、残差分析、多模型对比预测的完整时序建模流程,…

2026/10/16 21:35:51 阅读更多 →
影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别 做影刀RPA自动化,十个新手有八个栽在"往输入框里填东西"这件事上:要么填不进去,要么填了一半,要么直接把原来内容追加在后面。这背后的根因&…

2026/10/17 1:44:59 阅读更多 →
影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容 1. 认识影刀:什么场景该用RPA采小说数据 起点中文网的页面结构相对稳定——分类榜单、书籍详情、章节内容三块独立页面,跳转链路清晰。这种场景非常适合影刀自动化&#x…

2026/10/16 15:04:24 阅读更多 →

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/16 21:54:38 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/16 3:49:43 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/16 14:23:36 阅读更多 →