簡體   English   中英

如何設置 VSCode C++ 以顯示局部變量

[英]how to setup VSCode C++ to show local variables

我正在關注 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"
    }
]

}

我是一名中級 python 程序員,剛從 C++ 開始; 我的猜測是我的 VSCode 顯示的是一些 memory 分配或類型變量的東西,但這對於軟件調試不是很有用。 有人知道如何配置 VSCode 或編譯器以正確顯示變量的內容嗎? 我正在使用 Windows。

謝謝!

更新:

這是我的任務。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"

}

我卸載了 minGW,從這個站點http://mingw-w64.org/doku.php/download安裝了 MinGW-w64,更改了 MinGW 環境變量,問題解決了!

在此處輸入圖像描述

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM