一、先安裝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
二、安裝插件
C/C++
主頁:https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptoolsC/C++ Clang Command Adapter
主頁:https://marketplace.visualstudio.com/items?itemName=mitaki28.vscode-clang
三、配置
(一)創(chuàng)建工作區(qū)文件夾
建立一個(gè)文件夾,比如命名為 c_workspace,然后在里面建立一個(gè) .vscode 文件夾
在VSCode里面選擇 打開文件夾 打開工作區(qū),例如這里的 c_workspace
(二)配置文件
涉及到兩個(gè)配置文件
- 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"
}
]
}
- 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

(三)注意事項(xiàng)
- 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ù)



(二)運(yùn)行
- 在終端中運(yùn)行
ctrl + ` 打開終端

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

五、其它參考
Windows 下的安裝和配置:https://www.zhihu.com/question/30315894
Clang 常用參數(shù):http://www.itdecent.cn/p/96058bf1ecc2