首页 > 解决方案 > 我无法在 VSCode 中调试我的 C++ 代码

问题描述

我正在尝试调试我的 C++ 文件,但是每当我按下运行 >> 开始调试选项时,它就会运行并退出而没有任何错误或任何东西。我的程序没有运行...

这是我的代码:

#include <vector>
#include <string>
#include <iostream>
using namespace std;


int main() 
{
    int rows, question;
    std::string length;
    cin >> rows;
    for (int i = 0; i < rows; i++)
    {
        cin >> length;
    }    
   return 0;
}

这是单击调试后在终端中显示的内容:

cmd /C "c:\Users\intel\.vscode\extensions\ms-vscode.cpptools-0.29.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-oxw2rz3u.v3w --stdout=Microsoft-MIEngine-Out-5me5n3jp.wq2 --stderr=Microsoft-MIEngine-Error-wg1xnokk.ddw --pid=Microsoft-MIEngine-Pid-kfv0gezm.4wp --dbgExe=C:\MinGW\bin\gdb.exe --interpreter=mi "


C:\Users\intel\Desktop\Python programs>

这是它在输出窗口中显示的内容:

在此处输入图像描述

这是我的tasks.json:

{
    "version": "2.0.0",
    "_runner": "terminal",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "test",
                "isDefault": true
            }
        }
    ]
}

这是我的launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/a.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    },
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
    ]
}

我正在使用 Windows 机器和用于 C++ 的 MinGW

标签: c++debuggingvisual-studio-codevscode-settings

解决方案


我也使用 VSCode 在 C++ 中进行开发,但还没有尝试与 MinGW 一起使用。我将它与 MSVC 和 GCC (Linux) 一起使用。使用 GCC,我的启动配置与您的略有不同。

{
  "linux": {
    "MIMode": "gdb",
    "miDebuggerPath": "/usr/bin/gdb",
    "setupCommands": [
      {
        "description": "Enable pretty-printing",
        "text": "-enable-pretty-printing"
      }
    ]
  },
  "name": "Run gcc (linux) x64 debug",
  "request": "launch",
  "program": "/projects/CPP/GHDWS/build/gcc-linux/x64/debug/GHDWS",
  "cwd": "/projects/CPP/GHDWS/build/run",
  "args": [],
  "preLaunchTask": "Build gcc x64 debug",
  "type": "cppdbg",
  "externalConsole": false
},

我制作了自己的系统构建,它会自动为您的项目配置 VSCode。如果你愿意,你可以尝试使用它。

CppMagic


推荐阅读