VSCode 開發(fā) C 在Mac的配置

一、先安裝clang

brew install --with-toolchain llvm

注意安裝的時(shí)候要接電源,因?yàn)榫幾g需要花很長時(shí)間;我的Macbook pro 2017花了將近40分鐘才編譯完成,電池消耗大約30%

參考:https://embeddedartistry.com/blog/2017/2/20/installing-clangllvm-on-osx

二、安裝插件

三、配置

(一)創(chuàng)建工作區(qū)文件夾

建立一個(gè)文件夾,比如命名為 c_workspace,然后在里面建立一個(gè) .vscode 文件夾

在VSCode里面選擇 打開文件夾 打開工作區(qū),例如這里的 c_workspace

(二)配置文件

涉及到兩個(gè)配置文件

  1. tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile",
            "type": "shell",
            "command": "clang",
            "args": [
                "${file}",
                "-o", // 指定輸出文件名,不加該參數(shù)則默認(rèn)輸出a.exe,Linux下默認(rèn)a.out
                "${fileDirname}/${fileBasenameNoExtension}",
                "-g", // 生成和調(diào)試有關(guān)的信息
                "-Wall", // 開啟額外警告
                "-std=c11" // C語言最新標(biāo)準(zhǔn)為c11,或根據(jù)自己的需要進(jìn)行修改
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true
            },
            "dependsOn": [
                "Sleep"
            ]
        },
        {
            "label": "Clean",
            "type": "shell",
            "command": "rm",
            "args": [
                "-rf",
                "${fileDirname}/${fileBasenameNoExtension}",
                "${fileDirname}/${fileBasenameNoExtension}.dSYM"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true
            }
        },
        {
            "label": "Sleep", // 沒有任何功能,只是簡單暫停一下,增加視覺效果
            "type": "shell",
            "command": "sleep",
            "args": [
                "1"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true
            },
            "dependsOn": [
                "Clean"
            ]
        },
        {
            "label": "Build",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": [
                "Compile"
            ],
            "problemMatcher": "$gcc"
        }
    ]
}
  1. 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": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true, // 如果要調(diào)試有輸入互動(dòng)的程序,那么就要從外部控制臺(tái)啟動(dòng);vscode的集成控制臺(tái)不支持輸入;
            "MIMode": "lldb",
            "preLaunchTask": "Build"
        }
    ]
}

這兩個(gè)文件的寫法參考:https://code.visualstudio.com/docs/languages/cpp

屏幕快照 2018-09-23 00.17.18.png

(三)注意事項(xiàng)

  1. Task 之間的依賴關(guān)系:

每一個(gè)Task需要依賴的前置Task,寫在 dependsOn: [ ... ] 當(dāng)中,不分先后,并發(fā)執(zhí)行;

如果需要有明確的依賴,則應(yīng)該把 Task 依次連接,形成依賴鏈,例如上面的例子:

Build <-- Compile <-- Sleep <-- Clean

錯(cuò)誤的做法:

Build <-- Compile, Sleep, Clean

即如果在 Build 當(dāng)中:

"dependsOn": [
    "Compile",
    "Sleep",
    "Clean"
]

的實(shí)際效果是:Build 在 這三個(gè)Task 全部完成之后再執(zhí)行,但是這三個(gè)任務(wù)是并發(fā)的。

四、使用方法

(一)編譯

  • 快捷鍵:shift + command + b :編譯,實(shí)際上是調(diào)用上面定義的 launch.json 里的任務(wù)

  • 命令面板:shift + command + p ,鍵入 Tasks: Run Task,然后選擇上面定義的 Build 任務(wù)

屏幕快照 2018-09-22 23.31.13.png
屏幕快照 2018-09-22 23.31.53.png
屏幕快照 2018-09-22 23.32.38.png

(二)運(yùn)行

  1. 在終端中運(yùn)行

ctrl + ` 打開終端

屏幕快照 2018-09-22 23.35.10.png
  1. 在調(diào)試中運(yùn)行

別忘記設(shè)置斷點(diǎn)。由于程序是在外部命令行窗口執(zhí)行的,別忘記把程序的窗口切換出來。

屏幕快照 2018-09-22 23.36.59.png

五、其它參考

Windows 下的安裝和配置:https://www.zhihu.com/question/30315894

Clang 常用參數(shù):http://www.itdecent.cn/p/96058bf1ecc2

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

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

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