环境
安装 C++ entendsion
安装 clang
检查系统是否安装了 clang
clang --version
如果没有安装,则执行
xcode-select --install
编写程序
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for(const string& word : msg) {
cout << word << " ";
}
cout << endl;
}
VS Code 配置文件
tasks.json
(compiler build settings)launch.json
(debugger settings)c_cpp_properties.json
(compiler path and IntelliSense settings)编译运行
设置编译
选择 Terminal > Configure Default Build Task.
在.vscode
下会创建一个tasks.json
文件,如下:{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
运行
选择 Terminal-> Run Build Task.Debug
选择 Run > Add Configuration… 选择 C++ (GDB/LLDB)
在.vscode
下会创建一个 launch.json
文件,如下:
选 Run > Start Debugging 开始 debug{
// 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": [
{
"name": "Launch (lldb)",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
C/C++ 配置
选择 View-> Command Palette… 选 C/C++: Edit Configurations (UI) 如下:
在.vscode
下会创建一个 c_cpp_properties.json
文件,可以根据需要修改配置。常用插件
Clang-Format
可以用快捷键很方便的格式化C++代码。TabNine
基于AI的自动代码补全,亲测好用,没有语言限制,看官网介绍就知道了(有点占内存)。
官网:https://www.tabnine.com参考