簡體   English   中英

如何在 Visual Studio 代碼中在 Mac 上使用調試器設置 C++

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

我正在嘗試設置 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 中的終端應該運行並且我能夠調試程序。

正如@ alan-birtles指出的那樣,添加-g將是解決方案。 另外我在這里發現,由於lldb不支持,因此無法將vscode的集成終端用於c ++。

這是最終配置。

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

因為你的VSCode Debugger Extension有 bug。

在 mac 上使用 VSCode 成功設置 C++ Debug:我使用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、調試和代碼瀏覽。Microsoft 官方出品的擴展程序),因為這個 CPPtools 和那個 CodeLLDb 沖突。

為避免任何問題,請不要下載任何 VSCode 擴展。 這是我在 mac 上使用 VSCode 調試 C++ 的配置: VSCode v1.63.2

Code Runner v0.11.6 (作者:韓君)

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

CodeLLDB v1.4.5 (作者:Vadim Chugunov)

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

配置成功詳情參考這里:

使用 VSCode(mac) 手動調試 C++11

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM