解決Mac OS Catalina下Visual Studio Code對於C/C++語言不能斷點調試的問題

背景

最近剛考完試,有空來回復一下博客上的評論。在回覆一條關於克魯斯卡爾最短路徑算法的評論時,爲了能夠讓回覆的內容更直觀,決定貼上代碼的調試信息(代碼是用C寫的)。我的電腦是MBP,沒有安裝XCode,只有Visual Studio Code,於是決定使用它來獲取一些調試信息,但是我按照網上的教程配置好調試環境後,一直出現調試進程無法在我打的斷點處停下的情況,一閃就過去了。度娘上面搜了好久也沒有結果,最後在微軟的官方教程上找到的端倪,原來這是Mac OS Catalina 10.15的BUG,也就是調試進程能夠認到斷點的存在,但是程序就直接運行完退出了。如下圖所示:

上圖中紅框表明能夠認到斷點

上圖中,退出代碼爲0,正常退出,沒有在斷點處暫停。

環境

我的設備環境是

  • Mac os Catalina 10.15.2
  • Visual Studio Code 1.14.1

解決辦法

新的插件

解決的辦法也很簡單,在VSCode的插件中心搜索CodeLLB的插件安裝,重啓VSCode即可。

配置步驟

Step1:按住鍵盤上的++P,搜索Debug: Open launch.json

或者點擊左邊的小蟲子,選擇create a launch.json file

Step2:選擇LLDB

Step3:編寫基礎配置文件

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]

Step4 :保存配置後++B編譯後即可斷點調試
在這裏插入圖片描述

配置文件文件分享

最後分享一下我的配置文件,希望能幫助到大家

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}"
        }
    ],
    "version": 4
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "Build with Clang",
        "type": "shell",
        "command": "clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "${file}",
          "-o",
          "${fileBasenameNoExtension}.out",
          "--debug"
        ],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

參考資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章