首页 > 解决方案 > 在 VS 代码中使用 gcc 无法看到 std::vector 的元素

问题描述

我目前正在使用 VS Code 来学习 C++,因为它易于设置并且比 VS Studio 轻得多。但是,我无法做的一件事是在调试模式下查看数组(或字符串等)的元素。

向量

我在这里搜索了解决方案,似乎启用漂亮的打印可以解决这个问题,但不幸的是它没有(我安装了 Python 3.6)。我也尝试过使用 VS Studio 编译器和调试器,但无法让它以我想要的方式工作(基本上单击 F5 来编译单个 cpp 文件而无需更改任何选项,只需单击一下)。

你们能帮我解决这个问题吗?我目前在 Windows 10 上使用 MinGW 编译器,具有以下任务文件:

"version": "2.0.0",
"tasks": [
    {
        "label": "echo",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g", "${relativeFile}", "-o", "example"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

并启动:

"version": "0.2.0",
"configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/example.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
        "preLaunchTask": "echo",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
]

预先感谢!

标签: c++gccvisual-studio-codemingw

解决方案


对我来说这可行,但首先检查你是否有'c:\msys64\mingw64\share\gcc-8.3.0\python''c:\usr\share\gcc-8.3.0\python'文件夹并调整片段

launch.json
...
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "python import sys;sys.path.insert(0, 'c:\\msys64\\mingw64\\share\\gcc-8.3.0\\python');from libstdcxx.v6.printers import register_libstdcxx_printers;register_libstdcxx_printers(None)",
                    "ignoreFailures": false
                },
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
...

推荐阅读