Vscode Modern CPP 环境配置
摘要
本文介绍了clangd, clang-format, clang-tidy的相关内容,主要是给出了包括VSCode在内的若干配置文件。下载VSCode、安装相关插件等过程略。
clangd版本配置
clangd是一个…(clangd官网),简单来说就是一个提供高亮补全等功能的lsp,不能直接用,一般需要通过插件进行访问。
要想在VSCode中使用clangd进行高亮、补全,而且指定使用的c++标准,大致需要以下操作:
- 关闭官方包的 intelligence, 可以在settings.json中指定
"C_Cpp.intelliSenseEngine": "disabled",
- 指定clangd的版本。打开vscode设置,搜索 clangd: fallback,然后添加你想用的c++标准
更精细的配置是使用配置文件,如下:
常用配置文件
.clangd
或
config.yaml
在当前项目目录,或者全局目录(LINUX: ~/.config/clangd/config.yaml, WINDOWS: %LocalAppData%\clangd\config.yaml)下创建配置文件。可以指定clangd使用的c++版本,以及clangd-tidy进行诊断的条目
Index:Background: Build
CompileFlags:Add:[-xc++,-Wall,-std=c++20]Compiler: clang++
Diagnostics:ClangTidy:Add:["*"]Remove:[
abseil*,
fuchsia*,
llvmlib*,
zircon*,
altera*,
google-readability-todo,
readability-braces-around-statements,
hicpp-braces-around-statements,
modernize-use-trailing-return-type,# 不要每个都加上尾返回值类型
readability-identifier-length,# 不检查变量名长度
cppcoreguidelines-avoid-magic-numbers,# 不检查魔法数字
readability-magic-numbers,#同上]
.clang-format
#基于那个配置文件BasedOnStyle: Google
Language: Cpp
# 标准: Cpp03, Cpp11, AutoStandard: c++20
ColumnLimit:120BraceWrapping:# case标签后面AfterCaseLabel:false# class定义后面AfterClass:false# 控制语句后面AfterControlStatement: Never
# enum定义后面AfterEnum:false# 函数定义后面AfterFunction:false# 命名空间定义后面AfterNamespace:false# ObjC定义后面AfterObjCDeclaration:false# struct定义后面AfterStruct:false# union定义后面AfterUnion:false#ExternBlock定义后面AfterExternBlock:false# catch之前BeforeCatch:false# else之前BeforeElse:false# lambda块之前BeforeLambdaBody:false# while之前BeforeWhile:false# 缩进大括号IndentBraces:false# 分割空函数SplitEmptyFunction:true# 分割空记录SplitEmptyRecord:true# 分割空命名空间SplitEmptyNamespace:true# 在 @property 后面添加空格, \@property (readonly) 而不是 \@property(readonly).ObjCSpaceAfterProperty:true# 访问说明符(public、private等)的偏移AccessModifierOffset:-4# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行)AlignAfterOpenBracket: Align
# 连续赋值时,对齐所有等号AlignConsecutiveAssignments: Consecutive
# 连续声明时,对齐所有声明的变量名AlignConsecutiveDeclarations: Consecutive
# 连续宏声明时,对齐空格 #clang-format-10 可用AlignConsecutiveMacros: Consecutive
# 对齐连接符: DontAlign(不对齐), Left(左对齐), Right(右对齐)AlignEscapedNewlines: Left
# 水平对齐二元和三元表达式的操作数AlignOperands: Align
# 对齐连续的尾随的注释AlignTrailingComments:true# 允许函数调用的所有参数在放在下一行AllowAllArgumentsOnNextLine:true# 允许函数声明的所有参数在放在下一行AllowAllParametersOfDeclarationOnNextLine:true# 允许短的块放在同一行AllowShortBlocksOnASingleLine: Never
# 允许短的case标签放在同一行AllowShortCaseLabelsOnASingleLine:false# 允许短的枚举放在同一行AllowShortEnumsOnASingleLine:false# 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), AllAllowShortFunctionsOnASingleLine: None
# 允许短的if语句保持在同一行AllowShortIfStatementsOnASingleLine: Never
# 允许短的Lambdas语句保持在同一行AllowShortLambdasOnASingleLine: None
# 允许短的循环保持在同一行AllowShortLoopsOnASingleLine:false# 总是在定义返回类型后换行(deprecated)AlwaysBreakAfterDefinitionReturnType: None
# 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数),# AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义)AlwaysBreakAfterReturnType: None
# 总是在多行string字面量前换行AlwaysBreakBeforeMultilineStrings:false# 总是在template声明后换行AlwaysBreakTemplateDeclarations: Yes
# false表示函数实参要么都在同一行,要么都各自一行BinPackArguments:true# false表示所有形参要么都在同一行,要么都各自一行BinPackParameters:true# 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效# 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行)BreakBeforeBinaryOperators: NonAssignment
# 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似),# Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似),# Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom# 注:这里认为语句块也属于函数BreakBeforeBraces: Custom
# 在三元运算符前换行BreakBeforeTernaryOperators:true# 在构造函数的初始化列表的逗号前换行BreakConstructorInitializersBeforeComma:falseBreakConstructorInitializers: BeforeColon
# 在类声明继承列表的逗号前换行BreakInheritanceList: BeforeColon
BreakAfterJavaFieldAnnotations:false# 允许中断长字符串BreakStringLiterals:true# 描述具有特殊意义的注释的正则表达式,它不应该被分割为多行或以其它方式改变CommentPragmas:'^ IWYU pragma:'# 允许连续的名称空间声明将在同一行CompactNamespaces:false# 构造函数的初始化列表的缩进宽度ConstructorInitializerIndentWidth:4# 延续的行的缩进宽度ContinuationIndentWidth:4# 去除C++11的列表初始化的大括号{后和}前的空格Cpp11BracedListStyle:true# 继承最常用的指针和引用的对齐方式DerivePointerAlignment:true# 关闭格式化DisableFormat:false# 修饰符后放置空行EmptyLineAfterAccessModifier: Never
# 修饰符前放置空行EmptyLineBeforeAccessModifier: LogicalBlock
# 自动检测函数的调用和定义是否被格式为每行一个参数(Experimental)ExperimentalAutoDetectBinPacking:false# 修正命名空间注释FixNamespaceComments:true# 需要被解读为foreach循环而不是函数调用的宏ForEachMacros:- foreach
- Q_FOREACH
- BOOST_FOREACH
# 需要解读为if的函数IfMacros:- KJ_IF_MAYBE
IncludeBlocks: Regroup
# 对#include进行排序,匹配了某正则表达式的#include拥有对应的优先级,匹配不到的则默认优先级为INT_MAX(优先级越小排序越靠前),# 可以定义负数优先级从而保证某些#include永远在最前面IncludeCategories:-Regex:'^<ext/.*\.h>'Priority:2SortPriority:0CaseSensitive:false-Regex:'^<.*\.h>'Priority:1SortPriority:0CaseSensitive:false-Regex:'^<.*'Priority:2SortPriority:0CaseSensitive:false-Regex:'.*'Priority:3SortPriority:0CaseSensitive:false# include块排序IncludeIsMainRegex:'([-_](test|unittest))?$'# 缩进修饰符IndentAccessModifiers:false# 缩进case块IndentCaseBlocks:false# 缩进case标签IndentCaseLabels:true# 缩进goto标签IndentGotoLabels:true# 预处理缩进IndentPPDirectives: None
# 缩进extern块IndentExternBlock: AfterExternBlock
# 缩进宽度IndentWidth:4# 函数返回类型换行时,缩进函数声明或函数定义的函数名IndentWrappedFunctionNames:false# 添加尾部注释InsertTrailingCommas: None
# 保留在块开始处的空行KeepEmptyLinesAtTheStartOfBlocks:true# Lambda块缩进LambdaBodyIndentation: Signature
# 开始一个块的宏的正则表达式MacroBlockBegin:''# 结束一个块的宏的正则表达式MacroBlockEnd:''# 连续空行的最大数量MaxEmptyLinesToKeep:1# 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), AllNamespaceIndentation: None
# 使用ObjC块时缩进宽度ObjCBlockIndentWidth:4# 在ObjC的protocol列表前添加一个空格ObjCSpaceBeforeProtocolList:false# 使用的包构造函数初始化式样式PackConstructorInitializers: NextLine
# 在call(后对函数调用换行的penaltyPenaltyBreakBeforeFirstCallParameter:19# 在一个注释中引入换行的penaltyPenaltyBreakComment:300# 第一次在<<前换行的penaltyPenaltyBreakFirstLessLess:120# 在一个字符串字面量中引入换行的penaltyPenaltyBreakString:1000# 对于每个在行字符数限制之外的字符的penaltyPenaltyExcessCharacter:1000000# 将函数的返回类型放到它自己的行的penaltyPenaltyReturnTypeOnItsOwnLine:200# 指针和引用的对齐: Left, Right, MiddlePointerAlignment: Left
PPIndentWidth:-1RawStringFormats:-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
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: pb
BasedOnStyle: google
# 引用对齐ReferenceAlignment: Pointer
# 允许重新排版注释ReflowComments:true# 允许排序#includeSortIncludes: CaseSensitive
# 允许排序声明SortUsingDeclarations:true# 单独的定义块SeparateDefinitionBlocks: Always
# 在C风格类型转换后添加空格SpaceAfterCStyleCast:false# 在赋值运算符之前添加空格SpaceBeforeAssignmentOperators:true# 在逻辑非操作符之后插入一个空格SpaceAfterLogicalNot:false# 在' template '关键字之后会插入一个空格SpaceAfterTemplateKeyword:true# 在用于初始化对象的c++ 11带括号的列表之前(在前面的标识符或类型之后)将插入一个空格SpaceBeforeCpp11BracedList:false# 构造函数初始化式冒号前的空格将被删除SpaceBeforeCtorInitializerColon:true# 在继承冒号前添加空格SpaceBeforeInheritanceColon:true# 控制括号前的单独空格。SpaceBeforeParens: ControlStatements
# 开圆括号之前添加一个空格SpaceBeforeParensOptions:AfterControlStatements:trueAfterForeachMacros:trueAfterFunctionDefinitionName:falseAfterFunctionDeclarationName:falseAfterIfMacros:trueAfterOverloadedOperator:falseBeforeNonEmptyParentheses:false# 在空的圆括号中添加空格SpaceInEmptyParentheses:false# 在基于冒号的范围循环之前 添加空格SpaceBeforeRangeBasedForLoopColon:true# 在尾随的评论前添加的空格数(只适用于//)SpacesBeforeTrailingComments:2# 在尖括号的<后和>前添加空格SpacesInAngles: Never
# 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格SpacesInContainerLiterals:true# 在C风格类型转换的括号中添加空格SpacesInCStyleCastParentheses:false# 在圆括号的(后和)前添加空格SpacesInParentheses:false# 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响SpacesInSquareBrackets:false# tab宽度TabWidth:4# 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, AlwaysUseTab: Never
DeriveLineEnding:falseUseCRLF:falseWhitespaceSensitiveMacros:- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
.cmake-format.json
如果需要对cmake文件进行格式化,需要安装 cmake-format插件或者工具,配置文件
{"_help_parse":"Options affecting listfile parsing","parse":{"_help_additional_commands":["Specify structure for custom cmake functions"],"additional_commands":{"foo":{"flags":["BAR","BAZ"],"kwargs":{"HEADERS":"*","SOURCES":"*","DEPENDS":"*"}}},"_help_override_spec":["Override configurations per-command where available"],"override_spec":{},"_help_vartags":["Specify variable tags."],"vartags":[],"_help_proptags":["Specify property tags."],"proptags":[]},"_help_format":"Options affecting formatting.","format":{"_help_disable":["Disable formatting entirely, making cmake-format a no-op"],"disable":false,"_help_line_width":["How wide to allow formatted cmake files"],"line_width":120,"_help_tab_size":["How many spaces to tab for indent"],"tab_size":4,"_help_use_tabchars":["If true, lines are indented using tab characters (utf-8","0x09) instead of <tab_size> space characters (utf-8 0x20).","In cases where the layout would require a fractional tab","character, the behavior of the fractional indentation is","governed by <fractional_tab_policy>"],"use_tabchars":false,"_help_fractional_tab_policy":["If <use_tabchars> is True, then the value of this variable","indicates how fractional indentions are handled during","whitespace replacement. If set to 'use-space', fractional","indentation is left as spaces (utf-8 0x20). If set to","`round-up` fractional indentation is replaced with a single","tab character (utf-8 0x09) effectively shifting the column","to the next tabstop"],"fractional_tab_policy":"use-space","_help_max_subgroups_hwrap":["If an argument group contains more than this many sub-groups","(parg or kwarg groups) then force it to a vertical layout."],"max_subgroups_hwrap":3,"_help_max_pargs_hwrap":["If a positional argument group contains more than this many","arguments, then force it to a vertical layout."],"max_pargs_hwrap":6,"_help_max_rows_cmdline":["If a cmdline positional group consumes more than this many","lines without nesting, then invalidate the layout (and nest)"],"max_rows_cmdline":2,"_help_separate_ctrl_name_with_space":["If true, separate flow control names from their parentheses","with a space"],"separate_ctrl_name_with_space":false,"_help_separate_fn_name_with_space":["If true, separate function names from parentheses with a","space"],"separate_fn_name_with_space":false,"_help_dangle_parens":["If a statement is wrapped to more than one line, than dangle","the closing parenthesis on its own line."],"dangle_parens":false,"_help_dangle_align":["If the trailing parenthesis must be 'dangled' on its on","line, then align it to this reference: `prefix`: the start","of the statement, `prefix-indent`: the start of the","statement, plus one indentation level, `child`: align to","the column of the arguments"],"dangle_align":"prefix","_help_min_prefix_chars":["If the statement spelling length (including space and","parenthesis) is smaller than this amount, then force reject","nested layouts."],"min_prefix_chars":4,"_help_max_prefix_chars":["If the statement spelling length (including space and","parenthesis) is larger than the tab width by more than this","amount, then force reject un-nested layouts."],"max_prefix_chars":10,"_help_max_lines_hwrap":["If a candidate layout is wrapped horizontally but it exceeds","this many lines, then reject the layout."],"max_lines_hwrap":2,"_help_line_ending":["What style line endings to use in the output."],"line_ending":"unix","_help_command_case":["Format command names consistently as 'lower' or 'upper' case"],"command_case":"canonical","_help_keyword_case":["Format keywords consistently as 'lower' or 'upper' case"],"keyword_case":"unchanged","_help_always_wrap":["A list of command names which should always be wrapped"],"always_wrap":[],"_help_enable_sort":["If true, the argument lists which are known to be sortable","will be sorted lexicographicall"],"enable_sort":true,"_help_autosort":["If true, the parsers may infer whether or not an argument","list is sortable (without annotation)."],"autosort":false,"_help_require_valid_layout":["By default, if cmake-format cannot successfully fit","everything into the desired linewidth it will apply the","last, most agressive attempt that it made. If this flag is","True, however, cmake-format will print error, exit with non-","zero status code, and write-out nothing"],"require_valid_layout":false,"_help_layout_passes":["A dictionary mapping layout nodes to a list of wrap","decisions. See the documentation for more information."],"layout_passes":{}},"_help_markup":"Options affecting comment reflow and formatting.","markup":{"_help_bullet_char":["What character to use for bulleted lists"],"bullet_char":"*","_help_enum_char":["What character to use as punctuation after numerals in an","enumerated list"],"enum_char":".","_help_first_comment_is_literal":["If comment markup is enabled, don't reflow the first comment","block in each listfile. Use this to preserve formatting of","your copyright/license statements."],"first_comment_is_literal":false,"_help_literal_comment_pattern":["If comment markup is enabled, don't reflow any comment block","which matches this (regex) pattern. Default is `None`","(disabled)."],"literal_comment_pattern":null,"_help_fence_pattern":["Regular expression to match preformat fences in comments","default= ``r'^\\s*([`~]{3}[`~]*)(.*)$'``"],"fence_pattern":"^\\s*([`~]{3}[`~]*)(.*)$","_help_ruler_pattern":["Regular expression to match rulers in comments default=","``r'^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$'``"],"ruler_pattern":"^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$","_help_explicit_trailing_pattern":["If a comment line matches starts with this pattern then it","is explicitly a trailing comment for the preceeding","argument. Default is '#<'"],"explicit_trailing_pattern":"#<","_help_hashruler_min_length":["If a comment line starts with at least this many consecutive","hash characters, then don't lstrip() them off. This allows","for lazy hash rulers where the first hash char is not","separated by space"],"hashruler_min_length":10,"_help_canonicalize_hashrulers":["If true, then insert a space between the first hash char and","remaining hash chars in a hash ruler, and normalize its","length to fill the column"],"canonicalize_hashrulers":true,"_help_enable_markup":["enable comment markup parsing and reflow"],"enable_markup":true},"_help_lint":"Options affecting the linter","lint":{"_help_disabled_codes":["a list of lint codes to disable"],"disabled_codes":[],"_help_function_pattern":["regular expression pattern describing valid function names"],"function_pattern":"[0-9a-z_]+","_help_macro_pattern":["regular expression pattern describing valid macro names"],"macro_pattern":"[0-9A-Z_]+","_help_global_var_pattern":["regular expression pattern describing valid names for","variables with global (cache) scope"],"global_var_pattern":"[A-Z][0-9A-Z_]+","_help_internal_var_pattern":["regular expression pattern describing valid names for","variables with global scope (but internal semantic)"],"internal_var_pattern":"_[A-Z][0-9A-Z_]+","_help_local_var_pattern":["regular expression pattern describing valid names for","variables with local scope"],"local_var_pattern":"[a-z][a-z0-9_]+","_help_private_var_pattern":["regular expression pattern describing valid names for","privatedirectory variables"],"private_var_pattern":"_[0-9a-z_]+","_help_public_var_pattern":["regular expression pattern describing valid names for public","directory variables"],"public_var_pattern":"[A-Z][0-9A-Z_]+","_help_argument_var_pattern":["regular expression pattern describing valid names for","function/macro arguments and loop variables."],"argument_var_pattern":"[a-z][a-z0-9_]+","_help_keyword_pattern":["regular expression pattern describing valid names for","keywords used in functions or macros"],"keyword_pattern":"[A-Z][0-9A-Z_]+","_help_max_conditionals_custom_parser":["In the heuristic for C0201, how many conditionals to match","within a loop in before considering the loop a parser."],"max_conditionals_custom_parser":2,"_help_min_statement_spacing":["Require at least this many newlines between statements"],"min_statement_spacing":1,"_help_max_statement_spacing":["Require no more than this many newlines between statements"],"max_statement_spacing":2,"max_returns":6,"max_branches":12,"max_arguments":5,"max_localvars":15,"max_statements":50},"_help_encode":"Options affecting file encoding","encode":{"_help_emit_byteorder_mark":["If true, emit the unicode byte-order mark (BOM) at the start","of the file"],"emit_byteorder_mark":false,"_help_input_encoding":["Specify the encoding of the input file. Defaults to utf-8"],"input_encoding":"utf-8","_help_output_encoding":["Specify the encoding of the output file. Defaults to utf-8.","Note that cmake only claims to support utf-8 so be careful","when using anything else"],"output_encoding":"utf-8"},"_help_misc":"Miscellaneous configurations options.","misc":{"_help_per_command":["A dictionary containing any per-command configuration","overrides. Currently only `command_case` is supported."],"per_command":{}}}
vscode常用配置
launch.json
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version":"0.2.0","configurations":[{"name":"DebugActiveCppFile","type":"cppdbg","request":"launch","program":"${workspaceFolder}/build/bin/${fileBasenameNoExtension}.out","args":[],"stopAtEntry":false,"cwd":"${workspaceFolder}","environment":[],"externalConsole":false,"MIMode":"gdb","setupCommands":[{"description":"Enable pretty-printing for gdb","text":"-enable-pretty-printing","ignoreFailures":true}],"preLaunchTask":"BuildActiveCppFile","miDebuggerPath":"/usr/bin/gdb"}]}
task.json
{"version":"2.0.0","tasks":[{"type":"shell","label":"BuildActiveCppFile","command":"/usr/bin/g++","args":["-g","${file}","-I${workspaceFolder}/include","-o","${workspaceFolder}/build/bin/${fileBasenameNoExtension}.out","-std=c++20"],"options":{"cwd":"/usr/bin"},"problemMatcher":["$gcc"],"group":"build"}]}
版权归原作者 开宝特攻 所有, 如有侵权,请联系我们删除。