mac下vscode調(diào)試c的環(huán)境配置

mac下vscode調(diào)試c的環(huán)境配置

想在vscode上調(diào)試c代碼,找了一圈配置,發(fā)現(xiàn)都是c++的教程。這里給出一篇可行的配置c的教程,希望對(duì)大家有幫助。

基本的環(huán)境,比如xcode、vscode的安裝,這里就不提了,默認(rèn)你是可以在vscode中可以運(yùn)行c代碼的。

主要是兩步,第一步安裝對(duì)應(yīng)的插件,第二步配置對(duì)應(yīng)的launch.jsontask.json。

1、安裝對(duì)應(yīng)的插件

下圖紅框圈住的插件都需要安裝:


在這里插入圖片描述

最關(guān)鍵的是CodeLLDB,解決了Catalina對(duì)系統(tǒng)lldb的不兼容問(wèn)題。

2、配置launch.json和task.json

生成launch.json文件:

工具類(lèi)點(diǎn)擊"運(yùn)行" --> 添加配置 --> LLDB,此時(shí)項(xiàng)目的根目錄下會(huì)生成.vscode目錄,目錄下會(huì)有launch.json文件生成。

{
    // 使用 IntelliSense 了解相關(guān)屬性。 
    // 懸停以查看現(xiàn)有屬性的描述。
    // 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/<your program>",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

不過(guò)這個(gè)需要做一下修改,修改后的launch.json文件如下:

{
    // 使用 IntelliSense 了解相關(guān)屬性。 
    // 懸停以查看現(xiàn)有屬性的描述。
    // 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - 生成和調(diào)試活動(dòng)文件",
            "type": "lldb",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}", // 重點(diǎn)
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: gcc 生成活動(dòng)文件"
        }
    ]
}

生成tasks.json文件

shift + command + p --> Tasks:Configure Tasks --> Create tasks.json form templates --> Others,操作完之后.vscode目錄下會(huì)自動(dòng)生成tasks.json文件,如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

我們需要對(duì)其做一些小改動(dòng):關(guān)鍵是args中添加的參數(shù),其實(shí)就是運(yùn)行是gcc相關(guān)的參數(shù)。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build with Clang",
            "type": "shell",
            "command": "gcc",
            "args": [
                //"test.cpp", // 這里是官方寫(xiě)法,不具有普遍性,注意兩個(gè)配置文件的統(tǒng)一性即可
                "${fileDirname}/${fileBasename}",
                "-o",
                //"test.out",
                "${fileDirname}/${fileBasenameNoExtension}",
                "--debug"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

3、運(yùn)行

在vscode中打開(kāi)要運(yùn)行的c文件,然后在工具類(lèi)"運(yùn)行",接著就會(huì)在斷點(diǎn)處hold住,具體如下圖:

在這里插入圖片描述

接著你就可以進(jìn)行調(diào)試了,如何調(diào)試我就不再贅述了。

參考

Debug C++ in Visual Studio Code
Mac使用vscode調(diào)試c/c++
Mac Catalina VSCode C++ 調(diào)試配置

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容