VSCode 在 macOS下無法調試C/C++ (LLDB)

問題描述

系統:macOS Catalina 版本 10.15.2
xcode版本:11.3
配置好c_cpp_properties.json, tasks.json, launch.json 後, vscode無法進行debug,一debug就跑出結果,斷點無效了。

解決方法

在牆內尋找了一圈,有關於lldb簽名的問題link,我嘗試了一下,還是沒有辦法debug。讓我很鬱悶,又不想轉投像CLion這樣的IDE。。。

後來實在沒有辦法就牆外找了一下,果然國外這種問題就有了。link.
從他們一來一回的聊天記錄,我發現原來居然是xcode版本的問題——我的版本太新了。蘋果的東西真不能早更新。。。
所以我將xcode退回到10.3的版本。
可以先去這裏下載;
先通過左側搜索框搜索,然後把這個.xip文件下載下來。
在這裏插入圖片描述
然後卸載新版的xcode,直接安裝xode10.3。

安裝完成後,將在vscode的配置文件launch.json中加入"miDebuggerPath": “/Applications/Xcode.app/Contents/Developer/usr/bin/lldb-mi”,問題解決。
溫馨提示,vscode11.3這個版本中並沒有lldb-mi, vscode10.3中是有的,這就是蘋果更新的鍋。。。

爲了方便大家,我這裏附上配置文件(個人建議還是自己手動生成更適合自己的機器)
c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

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": [
        {
            "name": "clang++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "miDebuggerPath": "/Applications/Xcode.app/Contents/Developer/usr/bin/lldb-mi",
            "MIMode": "lldb",
            "preLaunchTask": "clang++ build active file"
        }
    ]
}

後記

其實,還有一種方法,使用另一種擴展(CodeLLDB)取代。
強力推薦:
首先安裝擴展CodeLLDB:
在這裏插入圖片描述
然後依次配置好c_cpp_properties.json, tasks.json文件(建議手動配置,和上面一樣);
後面就是最重要的不同之處:
1、點擊創建
在這裏插入圖片描述
2、選擇第二個(我們剛剛安裝的擴張CodeLLDB)
在這裏插入圖片描述
3、生成的launch.json文件
在這裏插入圖片描述
4、很不幸,如果這時候你進行調試,就會報錯:Internal debugger error: unable to find executable for '/Users/****/Doc/CplusWorkspace/<your program>
5、你需要把<your program>替換成你的編譯程序的名字,顯然,你每編譯一個項目就要修改它顯然不合適,所以我們想到了之前tasks.json裏面有:

"args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],

所以把<your program>替換成${fileBasenameNoExtension}
附上最終的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}",
            "terminal": "external",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

寫到這裏,我之所以推薦該方法是因爲方法1雖然解決了debug的問題,但本身還是有bug的。方法二就很完美了!
其實,我試錯了很久才解決了該問題,主要是vscode這個編輯器真的很棒!有了他,我不需要sublime,不需要pycharm,不要要clion,不需要eclipse,哈哈哈!

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