MinGW安装
安装mingw.exe(分64位和32位),这里选择64位安装。(安装源参看链接1,安装教程参看链接2)
链接1:https://sourceforge.net/projects/mingw/files/latest/download
链接2:https://blog.csdn.net/my_Wade/article/details/46941645
如果上述安装完后,bin目录下没有gdb.exe,可以通过下面命令获取。
mingw-get install gdb
Vs Code中脚本准备
task.json文件
{
"version": "2.0.0",
"type": "shell",
"tasks": [
{
"type": "shell",
"label": "build_active_file",
"command": "D:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"-std=c++11",
"-o", "${workspaceRoot}\\main.exe",
"test.cpp"
],
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": [
"$gcc"
]
}
]
}
/*
Predefined variables
The following predefined variables are supported:
${workspaceFolder} - the path of the folder opened in VS Code
${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
${file} - the current opened file
${relativeFile} - the current opened file relative to workspaceFolder
${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder
${fileBasename} - the current opened file's basename
${fileBasenameNoExtension} - the current opened file's basename with no file extension
${fileDirname} - the current opened file's dirname
${fileExtname} - the current opened file's extension
${cwd} - the task runner's current working directory on startup
${lineNumber} - the current selected line number in the active file
${selectedText} - the current selected text in the active file
${execPath} - the path to the running VS Code executable
${defaultBuildTask} - the name of the default build task
Predefined variables examples
Supposing that you have the following requirements:
A file located at /home/your-username/your-project/folder/file.ext opened in your editor;
The directory /home/your-username/your-project opened as your root workspace.
So you will have the following values for each variable:
${workspaceFolder} - /home/your-username/your-project
${workspaceFolderBasename} - your-project
${file} - /home/your-username/your-project/folder/file.ext
${relativeFile} - folder/file.ext
${relativeFileDirname} - folder
${fileBasename} - file.ext
${fileBasenameNoExtension} - file
${fileDirname} - /home/your-username/your-project/folder
${fileExtname} - .ext
${lineNumber} - line number of the cursor
${selectedText} - text selected in your code editor
${execPath} - location of Code.exe
*/
launch.json文件
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86",
"program": "${workspaceRoot}\\main.exe",
"miDebuggerPath":"D:\\MinGW\\bin\\gdb.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceRoot}",
"externalConsole": false,
"preLaunchTask": "build_active_file"
}
]
}