3

VSCode配置C/C++ GDB调试环境[Windows]

 2 years ago
source link: https://blog.ixk.me/post/vscode-configuration-c-cpp-gdb-debugging-environment-windows
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

VSCode配置C/C++ GDB调试环境[Windows]

2018-09-04 • Otstar Lin •
本文最后更新于 268 天前,文中所描述的信息可能已发生改变

折腾了两个小时,终于把 VSCode 调试环境弄好了 (开心

VSCode

安装 C/C++ for Visual Studio Code 插件

  1. 按下 Ctrl+Shift+X
  2. 在搜索框中输入 C/C++
  3. 安装第一个插件

至此 VSCode 环境部分配置完毕

安装 MinGW

  1. 下载 MinGW
  2. 点击Continue开始安装,安装过程需联网,若安装时提示error则需使用梯子进行安装
  3. Continue按钮恢复为可用状态,点击完成安装

GCC 环境安装

  1. 打开 MinGW
  2. 选中左栏的Basic Setup,然后选中mingw32-gcc-g++-bin,右键选择Mark for installation
  3. 选中All packages,找到mingw32-gdb-bin,mingw32-gdb-doc,mingw32-gdb-info,mingw32-gdb-lang,mingw32-gdb-lic,mingw32-gdb-man,右键选择Mark for installation
  4. 点击左上角的installation,然后点击Apply Changes
  5. 点击Apply,等待安装完成,点击 close

至此 GDB 环境部分配置完成

MinGW 配置较为繁琐,可以考虑使用MinGW64,MinGW64 安装后就不需上方的配置,一路点击下一步即可。(但是还是要配置环境变量

添加环境变量

  1. 进入设置,点击系统,然后选择关于,然后点击右侧系统信息
  2. 在弹出的系统信息中,选中左栏高级系统设置,然后选中环境变量
  3. 在系统变量一栏中找到Path,双击,然后新建一个C:\MinGW\bin 的变量
  4. 一路确定,直到关闭所有选卡

配置 VSCode

新建一个cpp文件,写上以下测试代码

1#include <iostream>
2int main()
3{
4    using namespace std;
5    cout << "Test 1";
6    cin.get ();
7    return 0;
8}

Ctrl+S 保存,然后按 F5,VSCode 会在上方弹出选择环境,选择C++(GDB/LLDB),然后将以下代码覆盖至launch.json,注意miDebuggerPath路径要对应

1{
2  // Use IntelliSense to learn about possible attributes.
3  // Hover to view descriptions of existing attributes.
4  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5  "version": "0.2.0",
6  "configurations": [
7    {
8      "name": "(gdb) Launch",
9      "type": "cppdbg",
10      "request": "launch",
11      "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
12      "args": [],
13      "stopAtEntry": false,
14      "cwd": "${workspaceFolder}",
15      "externalConsole": true,
16      "MIMode": "gdb",
17      "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
18      "setupCommands": [
19        {
20          "description": "Enable pretty-printing for gdb",
21          "text": "-enable-pretty-printing",
22          "ignoreFailures": true
23        }
24      ],
25      "preLaunchTask": "Build"
26    }
27  ]
28}

回到新建的cpp中,按下 F5,会显示找不到任务,点击配置任务,点击使用模板创建,然后点击带有Other的选项,用以下代码覆盖tasks.json

1{
2  "version": "2.0.0",
3  "tasks": [
4    {
5      "label": "Build",
6      "command": "g++",
7      "args": [
8        "-g",
9        "-Wall",
10        "-std=c++11",
11        "-lm",
12        "${file}",
13        "-o",
14        "${fileDirname}/${fileBasenameNoExtension}.exe"
15      ],
16      "presentation": {
17        "reveal": "always",
18        "echo": false,
19        "focus": true
20      },
21      "problemMatcher": {
22        "owner": "cpp",
23        "fileLocation": "absolute",
24        "pattern": {
25          "regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$",
26          "file": 1,
27          "line": 2,
28          "column": 3,
29          "severity": 4,
30          "message": 5
31        }
32      }
33    },
34
35    {
36      "label": "Run",
37      "type": "shell",
38      "dependsOn": "Build",
39      "command": "${fileDirname}/${fileBasenameNoExtension}.exe",
40      "args": [],
41      "presentation": {
42        "reveal": "always",
43        "focus": true
44      },
45      "problemMatcher": [],
46      "group": {
47        "kind": "test",
48        "isDefault": true
49      }
50    }
51  ]
52}

Ctrl+S 保存,然后回到新建的cpp,按下 F5,程序就会被编译运行,至此 GDB 调试环境便配置完成.

对了,C/C++也是可以进行断点调试的,具体方法可以参考 VSCode 配置 PHP 调试环境[Windows]

在使用过程中你可能会遇到输入或输出中文乱码的问题,此时只需要将文件改成 GB2312 编码即可


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK