使用Vscode格式化代码
可以在vscode上使用clang-format对C/C++代码自动格式化,配置自己想要的格式规范,解脱代码格式的束缚。本文主要介绍Mac上的配置和操作,Windows上配置步骤基本一致,可以自行探索。
操作步骤
- 首先打开vscode的应用商店,安装C/C++拓展,这个会自动安装clang- format
- 设置clang-format的配置参数
打开设置,搜索C_CppC_Cpp,修改下述三项内容的参数
C_Cpp: Clang_format_fallback Style:Visual Studio
C_Cpp: Clang_format_path:clang-format执行文件的完整路径
C_Cpp: Clang_format_style:file
如果不清楚自己的clang-format安装路径,可以使用如下命令到,一般是在Users路径下,例如我的路径:/Users/xxx/.vscode/extensions/ms-vscode.cpptools-1.8.4/LLVM/bin/clang-format
sudo find /Users -name clang-format
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: false
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
...
- 创建一个test.cpp文件测试一下效果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)