配置環境:Macbook Pro操作系統MacOS Catalina 10.15.4.
首先在瀏覽器中搜索vscode,打開搜索結果第一條,然後點擊“Download for Mac”然後在頁面停留一會就下載了vscode安裝程序
解壓縮,
將文件拉入應用程序即可。
安裝插件C\C++,C\C++ Clang Command Adapter,CodeLLDB(用來debug,解決Catalina不支持lldb調試問題)以及code runner(用來編譯)。
添加配置文件:tasks.json、launch.json、以及c_cpp_properties.json.
shift+command+P點擊C/C++ 編輯配置(JSON),出現文件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": "clang-x64"
}
],
"version": 4
}
shift+command+B一直點擊直到other,如下配置tasks.json文件。
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"${file}",
"-std=c++11",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"-g",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
創建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": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
創建測試代碼文件test.cpp
#include <iostream>
int main()
{
for(int i=0;i<5;i++){
std::cout<<"hey you"<<std::endl;
}
return 0;
}
點擊runcode 小三角運行結果:
但是假如在for循環輸出語句設置斷點,調試控制檯出現錯誤:
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
解決辦法:
將之前的launch.json文件刪除,添加lldb 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": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
設置斷點:點擊運行,按F5錯誤消失,並且按照每一步輸出結果:
另外需要注意的一點:如果更改了程序的內容,保存之後,需要重新shift+command+B,產生新的.out文件,這樣再按F5調試纔是修改後的結果,否則仍然是修改前的結果。
參考鏈接:
https://www.bilibili.com/video/BV1U741157Rd/?p=2&t=40
https://www.bilibili.com/video/BV1z7411N7Pg?from=search&seid=13295932737773533081
希望對大家有幫助。