【问题标题】:How to debug C++ code in Visual studio code如何在 Visual Studio 代码中调试 C++ 代码
【发布时间】:2017-09-05 17:54:57
【问题描述】:

有人使用 Visual Studio 代码进行 C++ 编程吗?请告诉我,当我使用 g++ 编译器进行编译时,如何在 Visual Studio 代码中调试我的代码。

【问题讨论】:

  • 你的意思是“launch.json”吗?
  • 哦,是的,launch.json 我的错。
  • 您使用的是 Cygwin、MinGW 还是 WSL?如果您使用 WSL,可以在此处找到更多信息:github.com/Microsoft/vscode-cpptools/blob/master/Documentation/… 在您的 tasks.json 中,确保使用符号的 -g 标志进行编译。
  • 使用 minGW。你能告诉我它的语法吗,在编译之前添加 -g 。

标签: debugging visual-studio-code c++14


【解决方案1】:

C++ 调试需要几个步骤来配置 VSCode。完成后,可以使用 F5 轻松调试 C++ 代码。
我写了一篇文章,指导如何在 VSCode 中运行和调试 C/C++ 文件。

https://medium.com/@jerrygoyal/run-debug-intellisense-c-c-in-vscode-within-5-minutes-3ed956e059d6

【讨论】:

  • 我需要用 VS Code 调试一个预编译的代码。请您看一下我的问题here,看看您是否可以提供解决方案?
【解决方案2】:

用于调试项目目录内的多个 cpp 文件。

您的 launch.json 和 task.json 应该如下所示

tasks.json



{
        "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": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }

launch.json

{
        "name": "g++.exe - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "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"
    }

【讨论】:

    【解决方案3】:

    要使用 Visual C++ 调试器进行调试,必须使用带有调试符号的 Visual C++ 编译器编译代码,然后才能进行调试。您可以创建一个 task.json 来执行此操作。

    This resourcethis resource 可能会有所帮助。

    【讨论】:

    • 我实际上从未这样做过,只是阅读一下。这就是为什么我在我读到它的地方包含了链接。也许在这方面有更多经验的人可以通过更具体的说明给你一个答案,但我看到到目前为止还没有人参与进来,所以我分享了我所知道的,希望它会对你有所帮助。抱歉,我帮不上忙!
    • 这不能回答问题。问题是关于 Visual Studio Code,它是一个代码编辑器。这个答案是关于 Visual Studio,它是一个 IDE。
    猜你喜欢
    • 2023-03-27
    • 2021-10-18
    • 2015-08-04
    • 2013-12-19
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    相关资源
    最近更新 更多