简体   繁体   English

如何让调试c ++在Mac上的VSCode中工作?

[英]How to get debugging c++ to work in VSCode on a mac?

Can someone explain how to get building and debugging to work in VSCode on a Mac ? 有人可以解释如何在Mac上的 VSCode中进行构建和调试吗?

Let's assume we successfully installed cpp tools: 我们假设我们成功安装了cpp工具:

在此输入图像描述

-Including creating a proper task file that works on a mac. - 包括创建适用于mac的正确任务文件。 -The required changes to launch.json -Any other step required. - launch.json所需的更改 - 需要其他任何步骤。

(Don't get me wrong, I'm not lazy, I have been trying for more then 2 hours now and it seems that a proper answer for this question can help a lot of people.) (不要误会我的意思,我不是很懒,我现在已经尝试了2个多小时了,似乎这个问题的正确答案可以帮助很多人。)

Once you have the C/C++ extension downloaded, you can use the configurations to generate a project.json in debug window of VsCode. 下载C / C ++扩展后,可以使用配置在VsCode的调试窗口中生成project.json。 If you do not currently have a project.json under the project's .vscode folder, hit F5 and a dropdown list should show up. 如果您当前在项目的.vscode文件夹下没有project.json,请点击F5并显示一个下拉列表。 There you can select C++ (GDB/LLDB), and this should generate a project.json for you to use. 在那里你可以选择C ++(GDB / LLDB),这应该生成一个project.json供你使用。

If you want to just hit F5 so it automatically compiles and debugs your program, you will need to add a tasks.json. 如果你想点击F5以便它自动编译和调试你的程序,你需要添加一个tasks.json。 This can be done by hitting F1 and selecting Tasks: Configure Task Runner and select Other . 这可以通过点击F1并选择Tasks: Configure Task Runner并选择Other Replace "echo" with "gcc" (or clang) and replace the args with your .cpp files and don't forget to add -g. 将“echo”替换为“gcc”(或clang)并将args替换为.cpp文件,并且不要忘记添加-g。

You can find more information in their documentation: https://code.visualstudio.com/docs/languages/cpp 您可以在其文档中找到更多信息: https//code.visualstudio.com/docs/languages/cpp

I don't count the time I lost looking for an answer to this question! 我不计算我失去的时间寻找这个问题的答案!

I found the vscode-lldb extension and it works fine, all other solutions I found don't work for me. 我找到了vscode-lldb扩展,它工作正常,我找到的所有其他解决方案对我不起作用。

You still have to create configuration files, here are mine to debug my unit tests: 你仍然需要创建配置文件,这是我的调试我的单元测试:

I'm using googletest and extension c++14 in this example 我在这个例子中使用googletest和扩展名c ++ 14

tasks.json tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build & debug tests",
      "type": "shell",
      "command": "g++",
      "args": [
        "-g",
        "-std=c++14",
        "-I/src",
        "-lgtest",
        "tests/MainTest.cpp",
        "-o",
        "bin/testMain"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": [
        "$gcc"
      ]
    }
  ]
}

launch.json launch.json

{
  "version": "0.2.0",
  "configurations":
  [
    {
      "name": "Debug Tests C/C++",
      "type": "lldb",
      "request": "launch",
      "program": "${workspaceFolder}/bin/testMain",
      "args": [],
      "cwd": "${workspaceFolder}/tests",
      "preLaunchTask": "build & debug tests"
    }
  ]
}

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

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