【问题标题】:Unable to Inspect C++ STL content in VS Code无法检查 VS Code 中的 C++ STL 内容
【发布时间】:2021-01-19 04:49:51
【问题描述】:

问题陈述:

调试器无法提供 STL 容器的内容(即向量或字符串)。


概述:

下面是我的launch.json,我按照this thread 添加了-enable-pretty-printing,但我看不到STL Container 的内容。

{

    "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"
        }
    ]
}

我什至尝试在监视窗口中添加表达式。但这对我也不起作用。或者,也许我错过了一些东西。 this thread

【问题讨论】:

  • 你扩展_M_start了吗?
  • @sweenish,它只是给了我向量的第一个元素。
  • 是的,那里可能还有另一个成员,展开后会显示下一个成员。事情就是这样。我看到的只是push_back()s,您可能会看到a.back() 的值,但同时向量的行为是正确的。可见断点看起来毫无意义。

标签: c++ visual-studio-code gdb vscode-debugger


【解决方案1】:

首先,您可能使用的是 x64 Windows。

我找到了一个有效的解决方案,在 x64 Windows 中安装 MinGW 时,安装 i686 (win32) 版本的 MinGW(此评论底部给出其官方下载链接)而不是 x86_64 版本,见下文:

win32版MinGW下载:

i686-posix-dwarf

我刚刚把下载的文件解压到文件夹D:\MinGW,然后将MinGW的bin路径D:\MinGW\i686-8.1.0-release-posix-dwarf-rt_v6-rev0\mingw32\bin添加到环境系统变量PATH中。

相关配置文件如下:

.vscode\tasks.json

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

.vscode\launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.    
    "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": "gdb",
            "setupCommands": [
                {   // Display content in STL containers pretty
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

.vscode\c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "g++", // Or complete absolute path "D:/MinGW/i686-8.1.0-release-posix-dwarf-rt_v6-rev0/mingw32/bin/g++.exe"
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x86"
        }
    ],
    "version": 4
}

我的电脑环境

VSCode Version: 1.53.2 (system setup)
OS Version: Windows 10 x64 (Windows_NT x64 10.0.19041)
MinGW version: i686-8.1.0-release-posix-dwarf-rt_v6-rev0
GDB version: 8.1.0 (Target: i686-w64-mingw32)

最初发布于https://github.com/microsoft/vscode-cpptools/issues/3921#issuecomment-780379258

希望对你有帮助。

【讨论】:

  • 这不适用于字符串向量或地图。
  • 刚刚测试了这个向量。它确实有效,但直到 C++14 并且 args 中没有 -O2 标志。将测试
  • @Nuetrino 使用此方法在我的 Windows 10 x64 上运行良好。
【解决方案2】:

你确定你已经完成了这个thread的每一步吗?

  1. MinGw-get.exe install gdb-python
  2. 安装 Python 2.7
  3. 确保 PYTHONPATH 和 PYTHONHOME 变量位于环境变量中。
  4. 确保%PYTHONHOME% 在路径变量上。
  5. 在 cwd 文件夹中创建 .gdbinit 文件。不要忘记更改sys.path.insert(0, 'your\MinGw\share\gcc-x.y.z\python')
  6. 不要忘记更改miDebuggerPath="your\\mingw\\bin\\gdb-python27.exe" 而不是miDebuggerPath="your\\mingw\\bin\\gdb.exe"

注意:如果您使用的是 MinGW x86,请确保您安装的 Python 架构是 x86。

【讨论】:

    猜你喜欢
    • 2017-08-10
    • 2022-11-11
    • 2022-01-05
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 2022-01-27
    相关资源
    最近更新 更多