macOS VSCode配置c/c++环境
注:本教程适用于M1系列芯片
环境
- macOS Monterey 12.0.1
- VSCode 1.62.0 M1版
终端配置
打开Mac终端执行:
xcode-select --install
注意:此步骤必须执行,否则会出现
检测到 #include 错误,请更新 includepath
报错,以前配置过的可以忽略直接进行下一步。
安装插件
- C\C++
- C\C++ Clang Command Adapter
- CodeLLDB(用来debug,解决Catalina不支持lldb调试问题)
- code runner(用来编译)
添加配置文件
- c_cpp_properties.json
- tasks.json
- launch.json
如果VSCode已经创建好的话直接修改文件内容即可。
配置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": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
配置tasks.json文件
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"${file}",
"-std=c++11",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-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}",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
注意
如果更改了程序的内容,保存之后,需要重新shift+command+B
,产生新的exec(Unix可执行文件)文件,然后再按F5
调试才是修改后的结果,否则仍然是修改前的运行结果。