Windows中VS code無法查看C++ STL容器的值 - 解決方法


Windows中VS code debug時無法查看C++ STL容器內容

首先,你很可能用的是x64版本的Windows。


我發現一個有效的解決方法,但在x64版本的Windows上安裝MinGW時,雖然官方推薦MinGW版本的是x86_64的,但實踐后發現如果選擇安裝 x86_64的, 很可能Debug時會無法看到STL容器(vecotr、map等)的具體信息,看到的是相應的內存地址~

故建議選 i686 (win32)的,然后安裝步驟的下一步及后面的操作都按默認的來就好。

MinGW-x86版


最后的效果:
pretty-printing-not-work-with-MinGW GDB-solution

win32 版本的 MinGW官方下載地址:

i686-posix-dwarf


我從這里下載到 MinGW 壓縮包,然后解壓到文件夾 D:\MinGW 中,接下來把MinGW的bin目錄,即 D:\MinGW\i686-8.1.0-release-posix-dwarf-rt_v6-rev0\mingw32\bin 加到了系統變量的 PATH 中。

將MinGW添加到系統變量

而我相應的配置文件如下:

1、.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"
}

2、.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"
        }
    ]
}

3、 .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://www.geekzl.com/windows-vs-code-cpp-stl-not-work.html
本文地址:www.geekzl.com/windows-vs-code-cpp-stl-not-work.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM