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中安装,即可。
首先准备一个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;
}
launch.json
直接点击调试,点击运行调试
选择C++(GDB/LLDB)
选择g++ -生成和调试活动文件
在.vscode中会生成两个文件,我们对这两个文件进行配置即可。
launch.json
{
// 使用 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++ 生成活动文件"
}
]
}
可以看到我们的C++环境基本配置成功
但如果我们将代码做如下更改,就会编译不通过,原因是目前的编译不支持C++11及以后的标准。
#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;
}
所以还需要进行其他配置。
task.json
{
"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": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
至此,我们的C++环境已经全部配置完成。