尝试在 Mac 上的 Visual Studio Code 上调试 C++

问题描述 投票:0回答:1

为了让它发挥作用,我已经筋疲力尽了。我的tasks.json是

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/opt/homebrew/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: /opt/homebrew/bin/g++"
        }
    ]
}

我的 launch.json 是

{
  "version": "0.2.0",
  "configurations": [
      {
          "name": "C++ Launch",
          "type": "cppdbg",
          "request": "launch",
          "program": "${fileDirname}/${fileBasenameNoExtension}",
          "cwd": "${workspaceFolder}",
          "MIMode": "lldb"
      }
  ]
}

但是当我点击 Visual Studio Code 底部的 bug 进行调试时,收到错误消息

无法开始调试。项目系统提供的启动选项字符串无效。无法确定调试器的路径。 请指定“MIDebuggerPath”选项。

我该怎么办?

我花了很长时间询问聊天gpt并在谷歌上搜索答案。

c++ visual-studio-code
1个回答
0
投票

在运行调试之前必须先生成可执行文件,并且还应该从 launch.json 启动此任务。

示例:

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ compilar archivo activo",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                 "isDefault": true
                },
            "detail": "compilador: /usr/bin/clang++"
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) M1 Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ compilar archivo activo"
        }
    ]
}

其中program是编译main.cpp的可执行输出。

记住运行任务并在代码文件聚焦时启动。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.