Visual Studio Code -> VSCode 开发环境搭建 ---- C++ 代码调试(code runner 插件)

前记:VScode C/C++ 开发环境搭建及代码运行参考另一篇文章:

Visual Studio Code -> VSCode 开发环境搭建 ---- C/C++ 开发环境搭建 及 代码运行(code runner 插件)


VSCode 开发环境搭建  ----  C++ 代码调试

平台:Windows 10

IDE:Visual Studio Code :VSCode

功能环境:C++ 代码调试环境


目录

1. 开发平台确认

【1】. Windows 系统信息

【2】. VScode 版本信息

【3】. VScode 需安装插件信息

2. 创建待调试 C++ 文件

【1】. 创建工作区

【2】. VScode加载工作区

【3】. 代码 C++ 编写

3. 配置 C++ 调试环境

【1】. launch.json文件配置

【2】. tasks.json文件配置

4. C++ 代码调试

5. 后记


严文年 -- 记于苏州

1. 开发平台确认

确认开发平台的系统信息:目前基于Windows 10 - 64位操作系统进行VSCode配置。

【1】. Windows 系统信息

【2】. VScode 版本信息

【3】. VScode 需安装插件信息

2. 创建待调试 C++ 文件

【1】. 创建工作区

A. 创建代码调试使用的工作区:

【2】. VScode加载工作区

A. 在创建的工作空间名称上单击鼠标右键,通过VScode打开工作空间:

B. VScode目录显示:

【3】. 代码 C++ 编写

A. 新建文件,输入 C++ 代码,保存文件到创建好的工作区。

附:C++ 待调试代码

#include <iostream>  
using namespace std;  
int main()  
{  
  int n, reverse=0, rem;    
  cout<<"Enter a number: ";    
  cin>>n;    
  while(n!=0)    
  {    
     rem=n%10;      
     reverse=reverse*10+rem;    
     n/=10;    
  }    
  cout<<"Reversed Number: "<<reverse<<endl;     
  return 0;  
}

 

 

3. 配置 C++ 调试环境

【1】. launch.json文件配置

附:launch.json配置文件

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\SoftWare\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build active file"
        }
    ]
}

【2】. tasks.json文件配置

附:tasks.json配置文件

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "D:\\SoftWare\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\SoftWare\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

4. C++ 代码调试

 

5. 后记

1. 以上是使用VScode + code runner 调试 C++ 代码的完整配置过程。

2. 关于VScode + code runner 对同一工作区的多个文件(.h,.cpp) 代码进行debug的配置:

见另一篇文章:Visual Studio Code  -> VSCode 开发环境搭建  ----  多个C++文件链接调试(code runner 插件)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章