简体   繁体   English

如何在 Visual Studio 代码中在 Mac 上使用调试器设置 C++

[英]How to setup C++ with debugger on mac in visual studio code

I am trying to setup visual studio code to build/run and debug c++ files using g++ compiler on macOS.我正在尝试设置 Visual Studio 代码以在 macOS 上使用 g++ 编译器构建/运行和调试 c++ 文件。 However when I build or debug the code, I can see that output file is created and i can run it as well however i am not able to debug it in vscode, for debugging gives weird behaviour.但是,当我构建或调试代码时,我可以看到输出文件已创建并且我也可以运行它但是我无法在 vscode 中调试它,因为调试会产生奇怪的行为。

So far I have been able to write tasks which builds the .cpp file and and executes it.到目前为止,我已经能够编写构建 .cpp 文件并执行它的任务。 The output comes into the terminal of vscode.输出进入 vscode 的终端。 However when I try with debug a new Terminal opens and no breakpoint is hit.但是,当我尝试调试时,会打开一个新终端并且没有命中断点。

Here is the tasks.json这是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++"]
    }
  ]
}

and here is my launch.json这是我的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"
      }
    }
  ]
}

I expect that when i hit debug or press f5 the terminal in the vscode should run and I am able to debug the program.我希望当我点击调试或按 f5 时,vscode 中的终端应该运行并且我能够调试程序。

As @alan-birtles pointed out adding -g would be the solution. 正如@ alan-birtles指出的那样,添加-g将是解决方案。 Also I found out here that there is no way to use integrated terminal of vscode for c++ as lldb doesn't support it. 另外我在这里发现,由于lldb不支持,因此无法将vscode的集成终端用于c ++。

Here are the final configuration. 这是最终配置。

launch.json 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 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++"]
    }
  ]
}

Because your VSCode Debugger Extension has bugger.因为你的VSCode Debugger Extension有 bug。

Successfully Setup C++ Debug with VSCode on mac: I use clang/gcc as compiler, Code-Runner (VSCode Extension) as runner, C/C++ Clang Command Adopter (VSCode Extension) as to provide static detection, CodeLLDB (VSCode Extension) to debug C++.在 mac 上使用 VSCode 成功设置 C++ Debug:我使用clang/gcc作为编译器, Code-Runner (VSCode Extension)作为运行器, C/C++ Clang Command Adopter (VSCode Extension)作为提供静态检测, CodeLLDB (VSCode Extension)进行调试C++。 Do Not Download Microsoft C/C++ (Identifier: ms-vscode.cpptools , for C/C++ IntelliSense, debugging, and code browsing. Microsoft Officially produced Extension), Because This CPPtools and that CodeLLDb conflict.请勿下载Microsoft C/C++ (标识符: ms-vscode.cpptools ,用于 C/C++ IntelliSense、调试和代码浏览。Microsoft 官方出品的扩展程序),因为这个 CPPtools 和那个 CodeLLDb 冲突。

To avoid any issues, please do not download any VSCode Extensions yet.为避免任何问题,请不要下载任何 VSCode 扩展。 Here is my configuration about debug C++ with VSCode on mac: VSCode v1.63.2 ;这是我在 mac 上使用 VSCode 调试 C++ 的配置: VSCode v1.63.2

Code Runner v0.11.6 (Author: Jun Han) Code Runner v0.11.6 (作者:韩君)

C/C++ Clang Command Adapter v0.2.4 (Author: Yasuaki MITANI) C/C++ Clang Command Adapter v0.2.4 (作者:Yasuaki MITANI)

CodeLLDB v1.4.5 (Author: Vadim Chugunov) CodeLLDB v1.4.5 (作者:Vadim Chugunov)

Clang v9.0.0 gcc v4.2.1 Intel CPU macOS 10.12 Clang v9.0.0 gcc v4.2.1 Intel CPU macOS 10.12

Details of successful configuration refer to here:配置成功详情参考这里:

Debug C++11 manually with VSCode(mac) 使用 VSCode(mac) 手动调试 C++11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM