在 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/7/20 21:27:59 阅读更多 →
Claude Code:从代码补全到深度理解的AI编程代理实践指南

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

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

2026/7/22 1:20:02 阅读更多 →
AI时代浏览器架构变革:六款新一代AI代理浏览器深度解析

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

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

2026/7/22 3:45:26 阅读更多 →

最新新闻

全球100所顶尖高校的AI转型给中国高校带来什么启示?

全球100所顶尖高校的AI转型给中国高校带来什么启示?

2026年6月,全球可持续发展大会在印度尼西亚雅加达举行。北京师范大学智慧学习研究院联席院长黄荣怀教授在会上正式发布了研究报告《引领全球教育变革:百所顶尖高校AI创新实践》。这份历时两年、覆盖六大洲30个国家和地区的100所高校的研究,从…

2026/7/23 21:13:52 阅读更多 →
露易丝·海的诗歌17

露易丝·海的诗歌17

我热爱并赞赏自己在我所处的无穷无尽的生命中,一切都完美、完整、完全。我将健康视为自然状态。现在,我有意识地抛弃内心中可能会表现为多种疾病的思维模式。我热爱并赞赏自己。我用营养丰富的饮食滋养自己。我以各种有趣的方式进行锻炼,增强…

2026/7/23 21:13:52 阅读更多 →
AI绘图实战:把人类演化时间轴做成3D写实信息图

AI绘图实战:把人类演化时间轴做成3D写实信息图

🔥 个人主页: 杨利杰YJlio ❄️ 个人专栏: 《Windows 疑难杂症与工单复盘案例库》 《Sysinternals实战教程》 《WINDOWS教程》 《Windows PowerShell 实战》 《人工智能实战合集》 《超简单:用Python让Excel飞起来…

2026/7/23 21:13:52 阅读更多 →
红队蓝队怎么选,2026 网络安全两大主流方向深度对比

红队蓝队怎么选,2026 网络安全两大主流方向深度对比

站在十字路口:红队与蓝队的职业抉择 2026 年的网络安全行业早已不再是当年那个“脚本小子”横行的草莽时代。随着 AI 攻防对抗的常态化、云原生架构的全面普及以及合规要求的日益严苛,安全从业者的职业路径发生了深刻的分化。对于刚入门或者处于转型期的…

2026/7/23 21:13:52 阅读更多 →
锚定AI语义主权:深度拆解搜极星与InsGEO双轨GEO监测体系的技术演进与实战逻辑

锚定AI语义主权:深度拆解搜极星与InsGEO双轨GEO监测体系的技术演进与实战逻辑

引言:告别“盲盒式”GEO优化,用数据穿透AI黑箱2026年,生成式AI已从辅助工具演变为用户获取信息的首要入口。当用户向DeepSeek、豆包、ChatGPT提问时,品牌是否能被“看见”、能否被“优先推荐”、信息是否“真实可信”,…

2026/7/23 21:13:52 阅读更多 →
云平台多少钱?拆解物联网监测平台的成本构成与选型逻辑(含真实报价区间)

云平台多少钱?拆解物联网监测平台的成本构成与选型逻辑(含真实报价区间)

“云平台多少钱?”——这是采购或技术负责人咨询物联网监测方案时,最先抛出的问题。但这个问题很难用一个数字回答,因为物联网监测云平台的成本不是“买软件”,而是由硬件接入、数据存储、算法模型、运维服务等多个模块叠加而成。…

2026/7/23 21:12:51 阅读更多 →

日新闻

从单点好评到指数级传播:AI副业主理人必须掌握的4层口碑渗透模型(含ROI测算表)

从单点好评到指数级传播:AI副业主理人必须掌握的4层口碑渗透模型(含ROI测算表)

更多请点击: https://intelliparadigm.com 第一章:从单点好评到指数级传播:AI副业主理人必须掌握的4层口碑渗透模型(含ROI测算表) 当AI副业主理人不再仅满足于单次服务交付,而是主动构建可复用、可裂变、可…

2026/7/23 0:00:25 阅读更多 →
AI写作开头钩子设计:为什么你的AI文案完读率不足18%?——基于2,346篇A/B测试报告的归因分析

AI写作开头钩子设计:为什么你的AI文案完读率不足18%?——基于2,346篇A/B测试报告的归因分析

更多请点击: https://codechina.net 第一章:AI写作开头钩子设计:为什么你的AI文案完读率不足18%?——基于2,346篇A/B测试报告的归因分析 在对2,346篇跨行业AI生成文案的A/B测试数据进行聚类分析后,我们发现&#xff1…

2026/7/23 0:01:26 阅读更多 →
Chitchatter完整指南:免费开源的终极点对点安全聊天工具

Chitchatter完整指南:免费开源的终极点对点安全聊天工具

Chitchatter完整指南:免费开源的终极点对点安全聊天工具 【免费下载链接】chitchatter Secure peer-to-peer chat that is serverless, decentralized, and ephemeral 项目地址: https://gitcode.com/gh_mirrors/ch/chitchatter Chitchatter是一款革命性的安…

2026/7/23 0:01:26 阅读更多 →

周新闻

Go语言静态资源打包方案对比与实践指南

Go语言静态资源打包方案对比与实践指南

1. 项目背景与核心需求在Go语言开发中,我们经常需要处理静态资源文件的打包问题。无论是Web应用的模板文件、前端资源,还是配置文件、证书等,都需要随程序一起分发。传统做法是将这些文件与编译后的二进制文件放在同一目录下,但这…

2026/7/22 8:58:19 阅读更多 →
Go语言实现高性能LDAP认证服务的架构与实践

Go语言实现高性能LDAP认证服务的架构与实践

1. 项目背景与核心价值LDAP(轻量级目录访问协议)作为企业级身份认证的黄金标准,已经服务了超过80%的财富500强公司。我在金融科技领域实施统一认证体系时,发现传统Java方案存在启动慢、内存占用高等痛点。而Go语言凭借其协程并发模…

2026/7/22 19:43:43 阅读更多 →
【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

更多请点击: https://intelliparadigm.com 第一章:AI面试官实战指南的核心价值与适用场景 AI面试官并非替代人类HR的“黑箱工具”,而是以可解释、可审计、可迭代的方式,赋能招聘全链路的关键基础设施。其核心价值在于将主观经验沉…

2026/7/23 17:49:47 阅读更多 →

月新闻