無廢話,直接上步驟。
1) 安裝 xcode。
打開App Store,搜索xcode,進行下載安裝。
2)執行命令:
xcode-select --install
安裝命令行工具。
3)安裝VS Code
https://code.visualstudio.com/
4) 打開vs code。打開左側擴展欄,
搜索“c++”。
安裝該擴展。
5)打開一個保護.cpp文件的文件夾(沒有就自己創建)
“command+shift+p”打開命令行工具窗口,輸入或者選擇“
Edit Configurations”命令。
此時會在當前工作空間目錄生成.vscode配置目錄,同時在配置目錄會生成一個c_cpp_properties.json文件。
配置include目錄:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/Library/Developer/CommandLineTools/usr/include/c++/v1",
"/usr/local/include",
"/Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include",
"/Library/Developer/CommandLineTools/usr/include",
"/usr/include"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
6)
“command+shift+p”打開命令行工具窗口,輸入或者選擇“Tasks: Configure Task”
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "c++",
"command": "clang++",
"type": "shell",
"args": [
"./c++/hello.cpp",
"-std=c++11",
"-g"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
}
]
}
7)配置launch.json。
“command+shift+p”打開命令行工具窗口,輸入或者
選擇Debug: Open launch.json命令。
修改內容如下:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "c/c++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask":"c++"
}
]
}
8)開啟調試
中途可能會提醒控制終端,需要賦予權限,允許即可。
最后如果終端有類似的提示,輸入回車結束終端調用。
可能遇到的問題:
可能遇到“xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun”
這樣的報錯,終端輸入命令:
sudo xcode-select --switch /Applications/Xcode.app
更多精彩內容,關注微信訂閱號“玄魂工作室”(xuanhun521)