Windows中VS code無法檢視C++ STL容器的值 - 解決方法
阿新 • • 發佈:2021-02-17
## Windows中VS code debug時無法檢視C++ STL容器內容
首先,你很可能用的是x64版本的Windows。
我發現一個有效的解決方法,但在x64版本的Windows上安裝MinGW時,雖然官方推薦MinGW版本的是x86_64的,但實踐後發現如果選擇安裝 x86_64的, 很可能Debug時會無法看到STL容器(vecotr、map等)的具體資訊,看到的是相應的記憶體地址~ 故建議選 `i686 (win32)`的,然後安裝步驟的下一步及後面的操作都按預設的來就好。 ![MinGW-x86版](https://img2020.cnblogs.com/blog/436938/202102/436938-20210217182002682-1991483970.png)
最後的效果: ![pretty-printing-not-work-with-MinGW GDB-solution](https://pic.rmb.bdstatic.com/bjh/686c742dc7eb16e7f9817287184a0d3b.png "極客中心") **win32 版本的 MinGW官方下載地址:** [i686-posix-dwarf](https://mmbizurl.cn/s/Xtn6Gu8sa)
我從這裡下載到 MinGW 壓縮包,然後解壓到資料夾 `D:\MinGW` 中,接下來把MinGW的bin目錄,即 `D:\MinGW\i686-8.1.0-release-posix-dwarf-rt_v6-rev0\mingw32\bin` 加到了系統變數的 `PATH` 中。
![將MinGW新增到系統變數](https://pic.rmb.bdstatic.com/bjh/bd22be136a484839b8c7a0934b88a522.png "將MinGW新增到系統變數-極客中心")
### 而我相應的配置檔案如下:
### 1、`.vscode\tasks.json`
``` 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`
```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`
```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/)
> 原文地址:
> 本文地址:`www.geekzl.com/windows-vs-code-cpp-stl-not-wor
我發現一個有效的解決方法,但在x64版本的Windows上安裝MinGW時,雖然官方推薦MinGW版本的是x86_64的,但實踐後發現如果選擇安裝 x86_64的, 很可能Debug時會無法看到STL容器(vecotr、map等)的具體資訊,看到的是相應的記憶體地址~ 故建議選 `i686 (win32)`的,然後安裝步驟的下一步及後面的操作都按預設的來就好。 ![MinGW-x86版](https://img2020.cnblogs.com/blog/436938/202102/436938-20210217182002682-1991483970.png)
最後的效果: ![pretty-printing-not-work-with-MinGW GDB-solution](https://pic.rmb.bdstatic.com/bjh/686c742dc7eb16e7f9817287184a0d3b.png "極客中心") **win32 版本的 MinGW官方下載地址:** [i686-posix-dwarf](https://mmbizurl.cn/s/Xtn6Gu8sa)