之前在看到网上没有相关中文的VS Code的配置教程,于是就自己写一个。
必须安装windows下的gcc
配置下来就是这样,可以进行debug。
这是我的插件:
其中如果要编写C++最好能够安装所有带C++字样的插件。
// 将设置放入此文件中以覆盖默认设置
{
// 以像素为单位控制字号。
"editor.fontSize": 20,
// 控制字体系列。
"editor.fontFamily": "Consolas, monospace",
"python.autoComplete.extraPaths": [],
"python.formatting.autopep8Args": [],
"python.formatting.autopep8Path": "autopep8",
"python.formatting.outputWindow": "Python",
"python.formatting.provider": "autopep8",
"python.formatting.yapfArgs": [],
"python.formatting.yapfPath": "yapf",
"python.jupyter.appendResults": true,
"python.jupyter.defaultKernel": "",
"python.jupyter.startupCode": [
"%matplotlib inline"
],
"editor.renderIndentGuides": true,
"python.linter": "pyLint",
"python.linting.enabled": true,
"python.linting.flake8Args": [],
"python.linting.flake8Enabled": false,
"python.linting.flake8Path": "flake8",
"python.linting.lintOnSave": true,
"python.linting.lintOnTextChange": true,
"C_Cpp.clang_format_formatOnSave": true,
"clang-format.language.cpp.enable": true,
//
"C_Cpp.clang_format_style": "file",
//
"C_Cpp.autocomplete": "Default",
"C_Cpp.clang_format_path": null,
"C_Cpp.clang_format_sortIncludes": true,
"clang.cflags": [],
"clang.completion.completeMacros": true,
"clang.completion.enable": true,
"clang.completion.maxBuffer": 8388608,
"clang.completion.triggerChars": [
".",
":",
">"
],
"clang.cxxflags": [],
"clang.diagnostic.delay": 200,
"clang.diagnostic.enable": true,
"clang.diagnostic.maxBuffer": 262144,
"clang.executable": "D:\\Program Files\\mingw-w64\\x86_64-6.2.0-posix-seh-rt_v5-rev1\\mingw64\\bin\\g++.exe ",
"clang.objcflags": [],
"editor.formatOnSave": true,
"editor.tabSize": 4,
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": false,
"typescript.format.placeOpenBraceOnNewLineForFunctions": false,
"python.linting.maxNumberOfProblems": 100,
"python.linting.mypyArgs": [],
"python.linting.mypyEnabled": false,
"python.linting.mypyPath": "mypy",
"python.linting.outputWindow": "Python",
"python.linting.pep8Args": [],
"python.linting.pep8Enabled": true,
"python.linting.pep8Path": "pep8",
"python.linting.prospectorArgs": [],
"python.linting.prospectorEnabled": false,
"python.linting.prospectorPath": "prospector",
"python.linting.pydocStyleArgs": [],
}
其中
"clang.executable":
"C_Cpp.clang_format_style": "file",
这里的格式化代码必须自己写,而且保存在工程文件夹下面。
"clang-format.executable": "clang-format",
这里使用的是名字为.clang-format的格式化配置文件。
写完配置文件,先写个程序试一下
发现缩进很怪异,而且按F5无法进行调试
在这里,我们把我们的源文件改为main.cpp
选择GDB
然后在生成的.json文件中删除所有代码,换为以下代码:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"preLaunchTask": "g++",
"miDebuggerPath": "D:\\Program Files\\mingw-w64\\x86_64-6.2.0-posix-seh-rt_v5-rev1\\mingw64\\bin\\gdb.exe",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb"
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb"
}
}
]
}
配置运行任务程序,之后选other
继续把代码替换为以下代码
{
"version": "0.1.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"main.exe"
],
"probelmMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
但这意味着我们每次启动调试都得先写2个配置文件,在这里为们可以写一个json的代码片段方便调试
文件菜单-》首选项-》用户代码片段,输入json,添加如下代码:
{
"TasksSetting": {
"prefix": "TaskC++",
"body": [
"{\"version\":\"0.1.0\",",
"\"command\":\"g++\",",
"\"args\":[\"-g\",\"${$}{file}\",\"-o\",\"main.exe\"],",
"\"probelmMatcher\":{",
"\"owner\":\"cpp\",",
"\"fileLocation\":[\"relative\",\"${$}{workspaceRoot}\"],",
"\"pattern\":{\"regexp\":\"^(.*):(${\\\\\\\\}d+):(${\\\\\\\\}d+):${\\\\\\\\}s+(warning|error):${\\\\\\\\}s+(.*)${$}\",",
"\"file\":1,\"line\":2,\"column\":3,\"severity\":4,\"message\":5}}}"
],
"description": "GDB的配置"
},
"LaunchSettings": {
"prefix": "LaunchC++",
"body": [
"{",
"\"version\": \"0.2.0\",",
"\"configurations\":[",
"{\n",
"\"name\": \"C++ Launch\",",
"\"type\": \"cppdbg\",",
"\"request\": \"launch\",",
"\"program\": \"${$}{workspaceRoot}/main.exe\",",
"\"args\": [],",
"\"stopAtEntry\": false,",
"\"cwd\":\"${$}{workspaceRoot}\",\n",
"\"preLaunchTask\":\"g++\",\n",
"\"miDebuggerPath\":\"D:${\\\\}${\\\\}Program Files${\\\\}${\\\\}mingw-w64${\\\\}${\\\\}x86_64-6.2.0-posix-seh-rt_v5-rev1${\\\\}${\\\\}mingw64${\\\\}${\\\\}bin${\\\\}${\\\\}gdb.exe\",",
"\"environment\":[],",
"\"externalConsole\":true,",
"\"linux\": {",
"\"MIMode\": \"gdb\"},",
"\"osx\": {",
"\"MIMode\": \"lldb\"},",
"\"windows\": {",
"\"MIMode\": \"gdb\"}}]}"
],
"description": "GDB的配置"
}
}
LaunchC++或TaskC++
就可以直接生成配置
还有一个问题就是格式化:
先写一个.clang-format文件,添加以下代码
# We'll use defaults from the LLVM style, but with 4 columns indentation.
# 如果想进行如下格式化,需要写一个这样的文件
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp