VSCode尝试配置C ++ GDB

时间:2018-09-27 20:51:25

标签: c++ visual-studio-code

我试图在Visual Studio代码上配置调试器,但无法使其正常工作。我已经在Visual Studio代码上安装了MING-W64和c / c ++扩展,当我运行代码时,这是我尝试在带有cin >> x >> y >> oper;的行上使用断点进行调试时的输出:

=thread-group-added,id="i1"
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"

这是我要执行的代码。

#include <iostream>
using namespace std;

class Calculator 
{
    public:
    int Calculate(int, int, char); 

};
int main() 
{
    int x,y,result; 
    char oper; 
    cout << "hello! I'm a calculator!" << endl;
    cout << "Please enter num1 operator num2: " <<endl;
    cin >> x >> y >> oper; 
    Calculator c; 
    result=c.Calculate(x,y,oper); 
    cout << "Result is: " << result << endl; 

    cin.ignore(); 
    cin.get();
    return 0;
}

int Calculator::Calculate(int x, int y, char oper)

{
    switch (oper)
    {
        case '+':
            return x+y;
        case '-':
            return x-y;
        case '*':
            return x*y;
        case '/':
            if(y!=0)
                return x/y;
        default: 
            return 0;
    }
}

下面是我的task.json文件:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++ -g Calculator.cpp -o Calculator", 
            "group": {
                "kind": "build",
                "isDefault": true
            }, "problemMatcher":"$gcc"

        }
    ]
}

这是我的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": " ${workspaceFolder}/Calculator",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我通过以下方法解决了该问题:

(在Linux上)

安装gdb:

sudo apt install gdb

然后重新加载窗口/关闭并重新打开,然后重试。这应该可以解决。


(在Windows上)

使用以下配置创建c_cpp_properties.json文件:

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}",
            //the path to your c++ folder in the mingw, in my case:
            "C:\\MinGW\\lib\\gcc\\mingw32\\8.2.0\\include\\c++"

        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "browse": {
            "path": [
                "${workspaceRoot}",
                "C:\\MinGW\\lib\\gcc\\mingw32\\8.2.0\\include\\c++"
            ]
        },
        "intelliSenseMode": "msvc-x64"
    }
],
"version": 4

}

然后,创建具有以下规范的launch.jason文件:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/file_name.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        //miDebuggerPath, normally as folow:
        "miDebuggerPath": "C:\\Mingw\\bin\\gdb.exe",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
         //optional, if you want to automatically build it
        "preLaunchTask": "build"
    }
]

}

您还将需要一个taks.json文件:

{
"version": "2.0.0",
"tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "g++ -g file_name.cpp -o output_name.exe",
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

}

*编辑: 以前我只是告诉过如何在Linux上修复它,还介绍了如何在Windows上进行配置。