我目前正在使用 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:\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
}
],
...
对于 Windows:
如果调试时仅看到 STL 容器的内存地址,则您可能使用的是 x64 Windows。
我找到了一个有效的解决方案,在 x64 Windows 中安装 MinGW 时,安装 MinGW 的
i686 (win32)
版本(此评论底部给出了其官方下载链接)而不是 x86_64
版本,如下所示:
win32版本的MinGW下载:
我只是将下载的文件解压到文件夹
D:\MinGW
中,然后将MinGWD:\MinGW\i686-8.1.0-release-posix-dwarf-rt_v6-rev0\mingw32\bin
的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。
愿对你有帮助。
在远程 Linux 机器上对我有用的东西。
请检查服务器上的路径“/usr/share/gcc-10/python”。对你来说可能会有所不同。
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "python import sys; sys.path.append('/usr/share/gcc-10/python');sys.path.insert(0, '/usr/bin/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
}
]
我使用的方法就像上面Georgy描述的那样。
*((int(*)[10])array._M_impl._M_start)
Debug console output:
-exec p array
$7 = {<std::_Vector_base<int, std::allocator<int> >> = {_M_impl = {<std::allocator<int>> = {<__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>}, _M_start = 0x2924cb0, _M_finish = 0x2924cd8, _M_end_of_storage = 0x2924cf0}}, <No data fields>}
-exec p *((int(*)[10])array._M_impl._M_start)
$8 = {41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464}
此问题可能是由于 gdb 的 python 问题造成的。我推荐使用msys2 + mingw。它工作正常,您将获得最新版本的 gcc。 如何使用msys2安装mingw:
我遇到了类似的问题,最终使用user3324131解决方案进行了微小的修改,我能够将其安装到正确的位置:
$pacman -Suy
$pacman -Suy mingw-w64-x86_64-gcc
(注意-Suy
)$pacman -Suy mingw-w64-x86_64-gdb
(注意-Suy
)$pacman -Suy mingw-w64-x86_64-make
(注意-Suy
)C:\msys64\mingw64\bin
添加到路径 ENV太棒了,谢谢并享受!
我认为 Sublime Text 或 Vim 可能更适合你。它比 Visual Studio Code 更轻,甚至更容易设置。
如果您仍然使用 VS Code,请单击并安装名为:Code Runner 的扩展。