【问题标题】:How to enable pretty printing for C++ in Visual Studio Code?如何在 Visual Studio Code 中为 C++ 启用漂亮打印?
【发布时间】:2021-05-18 01:06:08
【问题描述】:

我正在尝试使用 MinGW GDB python 调试器在 Visual Studio Code 中为 C++ 启用漂亮的打印。

我按照 “这适用于使用 Eclipse CDT 的 MinGW 用户” 标题下的 here 描述的步骤进行操作,但是当我尝试使用 "externalConsole": true 启动调试器时,我得到以下日志输出调试控制台:

1: (189) LaunchOptions{"name":"g++.exe - Build and debug active file","type":"cppdbg","request":"launch","program":"c:\\Users\\Ben Wilson\\Documents\\Scripts\\CodingChallenges\\movie.exe","args":[],"stopAtEntry":false,"cwd":"C:\\Users\\Ben Wilson\\Documents\\Scripts\\CodingChallenges","environment":[],"externalConsole":true,"MIMode":"gdb","miDebuggerPath":"C:\\MinGW\\bin\\gdb-python27.exe","setupCommands":[{"description":"Enable pretty-printing for gdb","text":"-enable-pretty-printing","ignoreFailures":true}],"preLaunchTask":"C/C++: g++.exe build active file","logging":{"engineLogging":true},"__configurationTarget":5,"__sessionId":"c62bc9d3-8c55-4dfb-b413-c91d22f3232e"}
1: (315) Starting: "C:\MinGW\bin\gdb-python27.exe" --interpreter=mi -x "C:\Users\Ben Wilson\Documents\Scripts\CodingChallenges\.gdbinit"
1: (328) DebuggerPid=11376
1: (391) "C:\MinGW\bin\gdb-python27.exe" exited with code -1073741515 (0xC0000135).
Starting: "C:\MinGW\bin\gdb-python27.exe" --interpreter=mi -x "C:\Users\Ben Wilson\Documents\Scripts\CodingChallenges\.gdbinit"
"C:\MinGW\bin\gdb-python27.exe" exited with code -1073741515 (0xC0000135).

1: (407) Send Event AD7MessageEvent
1: (408) <-logout

我尝试设置"externalConsole": false,并将此输出发送到调试控制台:

1: (123) LaunchOptions{"name":"g++.exe - Build and debug active file","type":"cppdbg","request":"launch","program":"c:\\Users\\Ben Wilson\\Documents\\Scripts\\CodingChallenges\\movie.exe","args":[],"stopAtEntry":false,"cwd":"C:\\Users\\Ben Wilson\\Documents\\Scripts\\CodingChallenges","environment":[],"externalConsole":false,"MIMode":"gdb","miDebuggerPath":"C:\\MinGW\\bin\\gdb-python27.exe","setupCommands":[{"description":"Enable pretty-printing for gdb","text":"-enable-pretty-printing","ignoreFailures":true}],"preLaunchTask":"C/C++: g++.exe build active file","logging":{"engineLogging":true},"__configurationTarget":5,"__sessionId":"cfbd8955-0807-495d-8bfa-e06052797523"}
1: (225) Wait for connection completion.

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-python27.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file",
            "logging": { "engineLogging": true }
        }
    ]
}

tasks.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": "C:\\MinGW\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

另外,单独运行 gdb-python27 可执行文件似乎没有任何作用(不确定这是否是预期行为)

C:\Users\Ben Wilson\Documents\Scripts\CodingChallenges>C:\MinGW\bin\gdb-python27.exe

C:\Users\Ben Wilson\Documents\Scripts\CodingChallenges>

有没有人经历过这种情况或知道在这里做什么?

【问题讨论】:

    标签: c++ debugging visual-studio-code gdb mingw


    【解决方案1】:

    解决方案

    修复问题:

    1. 从此link卸载mingw并安装mingw64
    2. 修改launch.jsoncommand 中的miDebuggerPathtasks.json 中的cwd 以匹配新文件位置
    3. 将新的 mingw64 bin 位置添加到 PATH
    4. 修改c_cpp_properties.json 中的includePathcompilerPath 以匹配新文件位置

    【讨论】:

      【解决方案2】:

      我也遇到过同样的问题。我的环境:

      • 本地/远程:Ubuntu 18.04 桌面/服务器
      • VSCode 1.60 + vscode_remote
      • g++ 与 gdb 8

      -enable-pretty-printing 的设置根本不起作用:

      以下解决方案适用于我:

      1. 检查您的 gdb 是否支持 gdb 的 python 模块提供的“漂亮打印”: readelf -d $(which gdb) | grep python。 如果没有列出任何内容,很遗憾,您必须从源头重新构建您的 gdb。
      2. 在重建 gdb 之前必须安装一些要求: sudo apt install python libpython-dev # will install python 2.7 and PythonLib in Ubuntu 18.04
      3. here下载最新的gdb并解压。
      4. 使用以下配置执行构建和安装:
        ./configure --with-python
        make all -j16
        sudo make install -j16
        
      5. 完成!

      附言。 conda config --set auto_activate_base false 并重新打开你的 shell 以确保 gdb 加载了正确版本的 python。

      【讨论】:

        猜你喜欢
        • 2014-06-04
        • 2011-06-26
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 2010-09-13
        • 1970-01-01
        • 2019-03-13
        相关资源
        最近更新 更多