VSCode配置C/C++ GDB调试环境[Windows]
折腾了两个小时,终于把 VSCode 调试环境弄好了 (开心
环境准备
VSCode
安装 C/C++ for Visual Studio Code 插件
- 按下 Ctrl+Shift+X
- 在搜索框中输入 C/C++
- 安装第一个插件
至此 VSCode 环境部分配置完毕
GDB
安装 MinGW
- 下载 MinGW
- 点击
Continue
开始安装,安装过程需联网,若安装时提示error
则需使用梯子进行安装 - 当
Continue
按钮恢复为可用状态,点击完成安装
GCC 环境安装
- 打开 MinGW
- 选中左栏的
Basic Setup
,然后选中mingw32-gcc-g++-bin
,右键选择Mark for installation
- 选中
All packages
,找到mingw32-gdb-bin,mingw32-gdb-doc,mingw32-gdb-info,mingw32-gdb-lang,mingw32-gdb-lic,mingw32-gdb-man
,右键选择Mark for installation
- 点击左上角的
installation
,然后点击Apply Changes
- 点击
Apply
,等待安装完成,点击 close
至此 GDB 环境部分配置完成
MinGW 配置较为繁琐,可以考虑使用MinGW64,MinGW64 安装后就不需上方的配置,一路点击下一步即可。(但是还是要配置环境变量
添加环境变量
- 进入设置,点击系统,然后选择关于,然后点击右侧系统信息
- 在弹出的系统信息中,选中左栏高级系统设置,然后选中环境变量
- 在系统变量一栏中找到
Path
,双击,然后新建一个C:\MinGW\bin
的变量 - 一路确定,直到关闭所有选卡
- 重启电脑
配置 VSCode
新建一个cpp
文件,写上以下测试代码
_8#include <iostream>_8int main()_8{_8 using namespace std;_8 cout << "Test 1";_8 cin.get ();_8 return 0;_8}
Ctrl+S 保存,然后按 F5,VSCode 会在上方弹出选择环境,选择C++(GDB/LLDB)
,然后将以下代码覆盖至launch.json
,注意miDebuggerPath
路径要对应
_28{_28 // Use IntelliSense to learn about possible attributes._28 // Hover to view descriptions of existing attributes._28 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387_28 "version": "0.2.0",_28 "configurations": [_28 {_28 "name": "(gdb) Launch",_28 "type": "cppdbg",_28 "request": "launch",_28 "program": "${fileDirname}/${fileBasenameNoExtension}.exe",_28 "args": [],_28 "stopAtEntry": false,_28 "cwd": "${workspaceFolder}",_28 "externalConsole": true,_28 "MIMode": "gdb",_28 "miDebuggerPath": "C:/MinGW/bin/gdb.exe",_28 "setupCommands": [_28 {_28 "description": "Enable pretty-printing for gdb",_28 "text": "-enable-pretty-printing",_28 "ignoreFailures": true_28 }_28 ],_28 "preLaunchTask": "Build"_28 }_28 ]_28}
回到新建的cpp
中,按下 F5,会显示找不到任务,点击配置任务,点击使用模板创建,然后点击带有Other
的选项,用以下代码覆盖tasks.json
_52{_52 "version": "2.0.0",_52 "tasks": [_52 {_52 "label": "Build",_52 "command": "g++",_52 "args": [_52 "-g",_52 "-Wall",_52 "-std=c++11",_52 "-lm",_52 "${file}",_52 "-o",_52 "${fileDirname}/${fileBasenameNoExtension}.exe"_52 ],_52 "presentation": {_52 "reveal": "always",_52 "echo": false,_52 "focus": true_52 },_52 "problemMatcher": {_52 "owner": "cpp",_52 "fileLocation": "absolute",_52 "pattern": {_52 "regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$",_52 "file": 1,_52 "line": 2,_52 "column": 3,_52 "severity": 4,_52 "message": 5_52 }_52 }_52 },_52_52 {_52 "label": "Run",_52 "type": "shell",_52 "dependsOn": "Build",_52 "command": "${fileDirname}/${fileBasenameNoExtension}.exe",_52 "args": [],_52 "presentation": {_52 "reveal": "always",_52 "focus": true_52 },_52 "problemMatcher": [],_52 "group": {_52 "kind": "test",_52 "isDefault": true_52 }_52 }_52 ]_52}
Ctrl+S 保存,然后回到新建的cpp
,按下 F5,程序就会被编译运行,至此 GDB 调试环境便配置完成.
对了,C/C++也是可以进行断点调试的,具体方法可以参考 VSCode 配置 PHP 调试环境[Windows]
在使用过程中你可能会遇到输入或输出中文乱码的问题,此时只需要将文件改成 GB2312
编码即可
VSCode配置C/C++ GDB调试环境[Windows]
https://blog.ixk.me/post/vscode-configuration-c-cpp-gdb-debugging-environment-windows许可协议
发布于
2018-09-04
本文作者
Otstar Lin
转载或引用本文时请遵守许可协议,注明出处、不得用于商业用途!