【问题标题】:How to setup C++ with debugger on mac in visual studio code如何在 Visual Studio 代码中在 Mac 上使用调试器设置 C++
【发布时间】:2019-12-17 22:33:35
【问题描述】:

我正在尝试设置 Visual Studio 代码以在 macOS 上使用 g++ 编译器构建/运行和调试 c++ 文件。但是,当我构建或调试代码时,我可以看到输出文件已创建并且我也可以运行它但是我无法在 vscode 中调试它,因为调试会产生奇怪的行为。

到目前为止,我已经能够编写构建 .cpp 文件并执行它的任务。输出进入 vscode 的终端。但是,当我尝试调试时,会打开一个新终端并且没有命中断点。

这里是tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build with g++",
      "type": "shell",
      "command": "g++",
      "args": [
        "-Wall",
        "-Wextra",
        "-Wpedantic",
        "-std=c++11",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "run",
      "type": "shell",
      "command": "cd ${fileDirname}/ && ./${fileBasenameNoExtension}",
      "dependsOn": ["Build with g++"]
    }
  ]
}

这是我的 launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(lldb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "showDisplayString": false,
      "environment": [],
      "externalConsole": true,
      "internalConsoleOptions": "openOnSessionStart",
      "MIMode": "lldb",
      "logging": {
        "moduleLoad": false,
        "programOutput": true,
        "trace": false
      },
      "preLaunchTask": "Build with g++",
      "osx": {
        "MIMode": "lldb"
      }
    }
  ]
}

我希望当我点击调试或按 f5 时,vscode 中的终端应该运行并且我能够调试程序。

【问题讨论】:

  • +1 因为我也很好奇这个问题的答案。我的 google-fu 让我失望了,尽管答案可能是一个指向“如何在 Mac 上/为 Mac 设置 VSC”网页的链接。
  • -g 添加到您的构建命令中
  • gcc gdb no debugging info的可能重复

标签: c++ macos visual-studio-code vscode-debugger vscode-tasks


【解决方案1】:

因为你的VSCode Debugger Extension 有虫子。

在 mac 上使用 VSCode 成功设置 C++ 调试: 我使用clang/gcc 作为编译器,Code-Runner(VSCode Extension) 作为运行器,C/C++ Clang Command Adopter(VSCode Extension) 作为提供静态检测,CodeLLDB(VSCode Extension) 作为调试 C++。 不要下载Microsoft C/C++(标识符:ms-vscode.cpptools,用于 C/C++ IntelliSense、调试和代码浏览。微软官方出品的Extension),因为这个CPPtools和那个CodeLLDb冲突。

为避免任何问题,请不要下载任何 VSCode 扩展。 这是我在 mac 上使用 VSCode 调试 C++ 的配置: VSCode v1.63.2;

Code Runner v0.11.6(作者:韩俊)

C/C++ Clang Command Adapter v0.2.4(作者:三谷靖明)

CodeLLDB v1.4.5(作者:Vadim Chugunov)

Clang v9.0.0gcc v4.2.1Intel CPUmacOS 10.12

配置成功详情参考这里:

Debug C++11 manually with VSCode(mac)

【讨论】:

    【解决方案2】:

    正如@alan-birtles 指出的那样,添加 -g 将是解决方案。 我还发现here 没有办法使用 vscode 的集成终端 for c++,因为 lldb 不支持它。

    这是最终配置。

    launch.json

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "(lldb) Launch",
          "type": "cppdbg",
          "request": "launch",
          "program": "${fileDirname}/${fileBasenameNoExtension}",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "showDisplayString": false,
          "environment": [],
          "externalConsole": true,
          "internalConsoleOptions": "openOnSessionStart",
          "MIMode": "lldb",
          "logging": {
            "moduleLoad": false,
            "programOutput": true,
            "trace": false
          },
          "preLaunchTask": "Build with g++",
          "osx": {
            "MIMode": "lldb"
          }
        }
      ]
    }
    
    

    task.json

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "Build with g++",
          "type": "shell",
          "command": "g++",
          "args": [
            "-g",
            "-Wall",
            "-Wextra",
            "-Wpedantic",
            "-std=c++11",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
          ],
          "group": {
            "kind": "build",
            "isDefault": true
          }
        },
        {
          "label": "run",
          "type": "shell",
          "command": "cd ${fileDirname}/ && ./${fileBasenameNoExtension}",
          "dependsOn": ["Build with g++"]
        }
      ]
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2016-10-12
      • 2019-06-05
      • 1970-01-01
      • 2019-10-29
      • 1970-01-01
      相关资源
      最近更新 更多