首页 > 解决方案 > 如何设置 VSCode C++ 以显示局部变量

问题描述

我正在关注 Udacity 的 C++ 纳米学位项目。在本课程中,讲师使用 VSCode、C/C++ 扩展和 Clang 格式。

在此处输入图像描述

根据课程说明,我已经安装了 g++ 编译器和 gdb 调试器,这两个工具都可以正常工作。我遇到的问题是,当我尝试从 VSC 调试时,我得到的结果与 tue 讲师所显示的完全不同。我正在使用以下代码

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  std::vector<std::string> brothers{"David", "Ethan", "Adam"};
  for (std::string const &brother : brothers) {
    std::cout << "Hello " << brother << "!\n";
  }
}

我的调试显示了一些奇怪的信息。

在此处输入图像描述

这与讲师在他的屏幕上看到的不同(他在 Linux 上)。他正在获取局部变量的实际内容。

在此处输入图像描述

这是我用于调试器的 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": "g++.exe - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}\\${fileBasenameNoExtension}.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
            }
        ],
        "preLaunchTask": "C/C++: g++.exe build active file"
    }
]

}

我是一名刚开始接触 C++ 的中级 Python 程序员;我的猜测是我的 VSCode 显示的是一些内存分配或类型变量的东西,但这对于软件调试不是很有用。有人知道如何配置 VSCode 或编译器以正确显示变量的内容吗?我正在使用 Windows。

谢谢!

更新:

这是我的 task.json

{
"tasks": [
    {
        "type": "cppbuild",
        "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": "build",
        "detail": "Task generated by Debugger."
    },
    {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file ver(1)",
        "command": "C:\\MinGW\\bin\\g++.exe",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build",
        "detail": "Task generated by Debugger."
    },
    {
        "type": "cppbuild",
        "label": "C/C++: cpp.exe build active file",
        "command": "C:\\MinGW\\bin\\cpp.exe",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build",
        "detail": "Task generated by Debugger."
    },
    {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file ver(2)",
        "command": "C:\\MinGW\\bin\\g++.exe",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
"version": "2.0.0"

}

标签: c++visual-studio-codegdbg++

解决方案


我卸载了 minGW,从这个站点http://mingw-w64.org/doku.php/download安装了 MinGW-w64 ,更改了 MinGW 环境变量,问题解决了!

在此处输入图像描述

谢谢!


推荐阅读