主要设置和json内容如下:
cpp_properties.json内容:
{
"configurations": [ //C++ intellisense插件需要这个文件,主要是用于函数变量等符号的只能解析
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"browse":{//解析工作路径下的所有文件夹或者文件中的符号
"path": ["${workspaceFolder}/**"]
},
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\mingw64\\bin\\gcc.exe",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
lanuch.json内容(更改launch中的可执行文件路径,也可以调试makefile和cmake管理生成的项目)
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [ //launch.json主要用于调试程序
{
"name": "(gdb) 启动",
"type": "cppdbg", //注意这个地方,如果为gdb,遇到断点时可能变量的值显示不正确
"request": "launch",
"program": "${workspaceFolder}/pointerTest.exe", //需要调试的可执行文件的路径
"args": [],
"stopAtEntry": false, //程序是否停在开始处
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false, //程序运行时是否新建终端,为false直接在vscode编辑器里面的终端显示
"MIMode": "gdb",
"miDebuggerPath": "C:/mingw64/bin/gdb.exe", //gdb命令的路径,不指定路径程序可能会运行不正常
"setupCommands": [ //gdb调试前需要执行的命令
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
},
{
"description": "忽略GDB中的SIGUSR1信号",
"text": "handle SIGUSR1 nostop noprint pass", //三个参数notop noprint pass在vscode中需要全部加上才能生效
"ignoreFailures": true
}
]
}
]
}
task.json内容
{
"tasks": [ //task.json用于编译程序时使用
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "C:\\mingw64\\bin\\gcc.exe", //指定编译器的绝对路径
"args": [ //编译器进行编译时的指定的参数:可进行额外的参数添加,如 "-std=c++11"、指定需要链接的库的路径
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
另附vscode C++环境的一些推荐插件
cscope插件可用于智能解析项目中的符号, 一定程度上可替代C Call Hierarchy插件。
Ubuntu安装ctags:
1、首先从github上下载ctags可执行文件,使用tar -xf解压下载下来的.tar.xz文件
2、将可执行文件的路径添加到环境变量: vim /etc/profile
export PATH=$PATH:/usr/bin/uctags/bin
其中/user/bin/uctags/bin是我的ctags可执行文件的路径,操作时需要修改为博友们自己的路径
3 执行source /etc/profile命令使得添加的环境变量立即生效
4 在ubuntu终端执行命令:apt-get install cscope universal-ctags即可生效。
5在vscode中按f1,弹出窗口输入c call ,如下图所示
6选择显示调用层次或者build database(有时候会显示build database,有时候不会显示),然后会构建项目的解析文件,如下图所示:
7构建完成之后,.vscode文件夹下面会有cscope.out和ctags.out文件,此时C Call Hierarchy即可正常工作。
注意:此处uctags可能会被装了两遍,第一遍在步骤1,第二遍在步骤4。只执行步骤4,vscode并不能主动识别到uctags的路径,因此C Call Hierarchy插件也就不能正常工作。
另附clang-format格式化模板
---
BasedOnStyle: Google
---
Language: Cpp
AccessModifierOffset: -4
# AlignAfterOpenBracket: Align
# AlignConsecutiveMacros: false
# 连续赋值时,对齐所有等号
AlignConsecutiveAssignments: true
# AlignConsecutiveDeclarations: false
# AlignEscapedNewlines: Left
# AlignOperands: true
# AlignTrailingComments: true
# AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: false
# AllowAllParametersOfDeclarationOnNextLine: true
# AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: true
# AllowShortFunctionsOnASingleLine: All
# AllowShortLambdasOnASingleLine: All
# AllowShortIfStatementsOnASingleLine: WithoutElse
# AllowShortLoopsOnASingleLine: true
# AlwaysBreakAfterDefinitionReturnType: None
# AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
# AlwaysBreakTemplateDeclarations: Yes
# BinPackArguments: true
# BinPackParameters: true
BraceWrapping:
# AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
# AfterObjCDeclaration: false
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
# IndentBraces: false
# SplitEmptyFunction: true
# SplitEmptyRecord: true
# SplitEmptyNamespace: true
# BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
# BreakBeforeInheritanceComma: false
# BreakInheritanceList: BeforeColon
# BreakBeforeTernaryOperators: true
# BreakConstructorInitializersBeforeComma: false
# BreakConstructorInitializers: BeforeColon
# BreakAfterJavaFieldAnnotations: false
# BreakStringLiterals: true
ColumnLimit: 100
CommentPragmas: "^ NOLINT:"
# 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: 4
# 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: Right
# 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: false
SortUsingDeclarations: false
# SpaceAfterCStyleCast: false
# SpaceAfterLogicalNot: false
# SpaceAfterTemplateKeyword: true
# SpaceBeforeAssignmentOperators: true
# SpaceBeforeCpp11BracedList: false
# SpaceBeforeCtorInitializerColon: true
# SpaceBeforeInheritanceColon: true
# SpaceBeforeParens: ControlStatements
# SpaceBeforeRangeBasedForLoopColon: true
# SpaceInEmptyBlock: false
# SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
# SpacesInAngles: false
# SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
# SpacesInCStyleCastParentheses: false
# SpacesInParentheses: false
# SpacesInSquareBrackets: false
# SpaceBeforeSquareBrackets: false
Standard: Cpp11
# StatementMacros:
# - Q_UNUSED
# - QT_REQUIRE_VERSION
TabWidth: 4
# UseCRLF: false
UseTab: Never