文章目录
- calng-format安装
- centos下clang-format安装
- ubuntu下clang-format的安装
- vscode支持clang-format
- clang-format使用
- 参考文献
calng-format安装
centos下clang-format安装
centos 的yum仓库中并没有clang-format的安装包,需要更新repo源:
sudo yum install centos-release-scl-rh
之后下载clang-format:
sudo yum install llvm-toolset-7-git-clang-format
但是这个时候,clang-format安装的位置不在系统的PATH变量中,所以我们需要更新PATH变量。首先找到我们安装的clang-format文件夹:
sudo find / -name *clang-format*
...
821-llvm-toolset-7-git-clang-format-5.0.1-4.el7-x86_64
/opt/rh/llvm-toolset-7/root/usr/bin/clang-format
/opt/rh/llvm-toolset-7/root/usr/bin/git-clang-format
...
得到clang-format的路径/opt/rh/llvm-toolset-7/root/usr/bin/clang-format
之后,编辑/etc/profile
文件,添加如下两条环境变量信息:
export LLVM_HOME=/opt/rh/llvm-toolset-7/root/usr
export PATH=$PATH:$LLVM_HOME/bin
之后使配置生效:
source /etc/profile
ubuntu下clang-format的安装
这个直接从apt仓库安装:
sudo apt-get install clang-format
vscode支持clang-format
直接搜索商店安装如下clang-format插件:
![在这里插入图片描述](https://img-blog.csdnimg.cn/4d7b9310ee0941f7bcfe26a7fb4354fe.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc2hlbm1pbmdpaw==,size_20,color_FFFFFF,t_70,g_se,x_16)
clang-format使用
输入以下命令就会按照llvm的格式在在当前路径下生成.clang-format文件。
[ik@localhost test]$ clang-format -style=LLVM -dump-config > .clang-format
![在这里插入图片描述](https://img-blog.csdnimg.cn/eb287b276ceb45e1a9bfc66533da77ba.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc2hlbm1pbmdpaw==,size_20,color_FFFFFF,t_70,g_se,x_16)
clang-format内部原生支持五种格式:
- LLVM
- Google
- Chromium
- Mozilla
- WebKit
我们可以在vscode中输入快捷点ctrl+shift+p
打开设置面板,之后在输入框输入clang-format,找到style
选项:
![在这里插入图片描述](https://img-blog.csdnimg.cn/671033e772914edeb338fbe01adfb4a3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc2hlbm1pbmdpaw==,size_20,color_FFFFFF,t_70,g_se,x_16)
可以看到这里的文件格式是file
,其表示按照我们自己定义的.clang-format
文件格式来,下面是吴咏炜老师使用的clang-format配置:
---
AccessModifierOffset: -4
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: true
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: true
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
ColumnLimit: 76
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^<.*\.hpp>$'
Priority: 3
- Regex: '^<.*\.h>$'
Priority: 1
- Regex: '^<'
Priority: 2
- Regex: '.*'
Priority: 4
IncludeIsMainRegex: '(_test)?$'
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Latest
TabWidth: 4
UseTab: Never
...
我们将其写入.clang-format
(clang-format需要在工作根目录下)文件,之后在想要格式化的文件中使用快捷键ctrl+shift+i就可以了格式化文档了。
![在这里插入图片描述](https://img-blog.csdnimg.cn/a25dcb7c16c94e22b285ff5cec6ab44f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAc2hlbm1pbmdpaw==,size_20,color_FFFFFF,t_70,g_se,x_16)
参考文献
[1] 吴咏炜.现代C++实战30讲.极客时间
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)