原文出自VScode C++环境配置 (MAC) - 知乎,本文只根据自己的实际诉求进行摘抄
其他参考视频:
Mac用Visual Studio Code编写C/C++ 苹果电脑VS Code快速编写C/C++教程_哔哩哔哩_bilibili
Macbook Pro 安装vscode并配置c/c++环境
一、VS Code下载以及依赖包的安装
vscode下载
官网下载 Visual Studio Code - Code Editing. Redefined
扩展包安装
- C/C++
- CodeLLDB
- CodeLLDB 可能由于网络问题安装不成功,可手动下载:
- 打开Github:https://github.com/vadimcn/vscode-lldb/releases,打开之后根据你的电脑芯片下载对应的版本
- 如果是基于Intel的Mac选择codelldb-×86_64-darwin.vsix,
- 如果是基于Apple Silicon的Mac选择codelldb-aarch64-darwin.vsix
- 下载完成后,在扩展中点击从vsix中安装,即可。
二、VS Code的配置设置
Step1:准备一个cpp文件
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
int b = 1;
// auto a = b;
// cout << a << endl;
// vector<string> msg{"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
// int len = msg.size();
// cout << "长度" << len << endl;
for (int i=0; i<5; i++){
// ms.push_back("a");
cout << i << endl;
}
return 0;
}
Step2: 生成launch.json以及task.json
- 直接点击调试,点击运行调试(此步骤将生成launch.json)
- 选择"C++(GDB/LLDB)"
- 选择"g++ -生成和调试活动文件" (此时将生成task.json)
Step3: 设置launch.json、tasks.json 以及c_cpp_properties.json(通过command+shift+p打开)
- launch.json (用Debug工具调起已编译生成的二进制可执行文件)
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "C++ debug",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "C/C++: g++ 生成活动文件"
}
]
}
- tasks.json(launch.json中对应的preLaunchTask,用来编译生成二进制文件)
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-fdiagnostics-color=always",
"-g",
"-Wall",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
- c_cpp_properties.json(通过command+shift+p打开)
{
"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": "macos-clang-x64"
}
],
"version": 4
}
备注:基于Makefile的launch.json与tasks.json
- launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "cppdbg",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/opencv_test",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "Makefile生成可执行文件"
}
]
}
- tasks.json
{
"tasks": [
{
"type": "shell",
"label": "Makefile生成可执行文件",
"command": "make",
"args": [
"all",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}