1.下载mingw64并配置环境变量
题主下载的为mingw64_6.4.0
将安装目录下的bin目录配置进环境变量
例如
D:\Coding_Program\ENVIRONMENT\mingw\mingw64_6.4.0\bin
2.安装插件
安装C/C++插件
3.配置launch.json和task.json文件
配置成功后新建一个cpp文件,并用VSCode打开该目录。
点击左方的运行和调试(Ctrl+Shift+D)按钮。
在未配置过时,会显示“创建launch.json文件”字样,点击,选择C++(GDB/LLDB),再选择g++ build and debug active file;
之后会自动弹出.vscode/launch.json文件。
我们对其进行更改与配置
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"internalConsoleOptions": "neverOpen",
// "externalConsole": true则为外部控制台
"MIMode": "gdb",
"miDebuggerPath": "D:\\Coding_Program\\ENVIRONMENT\\mingw\\mingw64_6.4.0\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}
之后按(Ctrl+Shift+P)搜索Task进入Tasks:Configuration Task或者F5按提示进行
会自动创建.vscode/tasks.json
对其进行修改和配置
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "g++.exe build active file",
"command": "D:\\Coding_Program\\ENVIRONMENT\\mingw\\mingw64_6.4.0\\bin\\g++.exe",
"args": [
"-g",
// "${file}",只能单文件运行
"${workspaceFolder}\\*.cpp", //支持同目录多文件运行
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17"
],
"options": {
// "cwd": "${workspaceFolder}"
"cwd": "D:\\Coding_Program\\ENVIRONMENT\\mingw\\mingw64_6.4.0\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
],
}
4.测试
再进入cpp文件,按F5即可运行或者设断点进行调试。
且会在同目录下生成同名exe文件。