c++ - 如何设置 VSCode C++ 以显示局部变量
问题描述
我正在关注 Udacity 的 C++ 纳米学位项目。在本课程中,讲师使用 VSCode、C/C++ 扩展和 Clang 格式。
根据课程说明,我已经安装了 g++ 编译器和 gdb 调试器,这两个工具都可以正常工作。我遇到的问题是,当我尝试从 VSC 调试时,我得到的结果与 tue 讲师所显示的完全不同。我正在使用以下代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
std::vector<std::string> brothers{"David", "Ethan", "Adam"};
for (std::string const &brother : brothers) {
std::cout << "Hello " << brother << "!\n";
}
}
我的调试显示了一些奇怪的信息。
这与讲师在他的屏幕上看到的不同(他在 Linux 上)。他正在获取局部变量的实际内容。
这是我用于调试器的 launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"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"
}
]
}
我是一名刚开始接触 C++ 的中级 Python 程序员;我的猜测是我的 VSCode 显示的是一些内存分配或类型变量的东西,但这对于软件调试不是很有用。有人知道如何配置 VSCode 或编译器以正确显示变量的内容吗?我正在使用 Windows。
谢谢!
更新:
这是我的 task.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file ver(1)",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: cpp.exe build active file",
"command": "C:\\MinGW\\bin\\cpp.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file ver(2)",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
解决方案
推荐阅读
- python - tkinter 和库 PhotoImage
- javascript - 如何使用方法重置变量?
- javascript - 使用 javascript 类转换对象
- bash - 相当于 bash 函数中的 -e
- node.js - 使用 Node Twitter 库的多个回调函数
- node.js - 尝试将时间戳附加到字符串但时间戳未定义
- python - 附加列值以将长度与其他数据匹配
- functional-programming - 部分应用的地图的名称是什么
- c - 禁用中断会保护非易失性变量还是会发生重新排序?
- angular - 我在 Angular 6 表单中使用 [(ngModel)],但在更改路线时,表单字段未清除