官网中对应的页面如下
https://code.visualstudio.com...
1 筹备
- vscode 软件
- C/C++ Extension Pack 插件
clang/clang++ 编译器
查看已装置clang++
clang++ -v
- 如果未装置,请返回 app store 下载 xcode
2 .vscode配置
在当前工作区筹备以下文件(夹)
.vscode
- tasks.json # 用于编译c++文件
- launch.json # 用于应用vscode自带的debug工具(左侧的小虫图标)
- c_cpp_properties.json # 用于应用vscode自带的代码提醒工具如 IntelliSense
main.cpp
#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;}
配置tasks.json
command + shift + b,vscode会执行tasks.json中的工作。
本文配置的是c++编译,替换tasks.json的内容如下:
{ "version": "2.0.0", "tasks": [ { "label": "Build with Clang", //这个工作的名字在launch.json最初一项配置 "type": "shell", "command": "clang++", "args": [ "-std=c++17", "-stdlib=libc++", "-g", // 生成调试信息,GUN可应用该参数 "${file}", // file指正在关上的文件 "-o", // 生成可执行文件 "${fileDirname}/${fileBasenameNoExtension}" // fileDirname指正在关上的文件所在的文件夹 // fileBasenammeNoExtension指没有扩展名的文件,unix中可执行文件属于此类 ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true } } ]}
关上main.cpp,按command + shift + b(留神,按这组快捷键时main.cpp文件在vscode中是正在关上的),如下图
工作区中没有扩展名的main就是在mac os下生成的可执行文件(同理,如果是windows,有扩展名.exe)
在vscode内置的终端中,能够运行main,如下图:
配置 c_cpp_properties.json
c_cpp_properties.json 的作用是:代码提醒、代码跳转等
{ "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**", "/Library/Developer/CommandLineTools/usr/include", "/Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include", // 请依据你的clang版本批改,我这里是11.0.3 "/usr/local/include" ], "defines": [], "macFrameworkPath": [ "/System/Library/Frameworks", "/Library/Frameworks" ], "compilerPath": "/usr/bin/clang++", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" // "compileCommands": "${workspaceFolder}/build/compile_commands.json" } ], "version": 4}
配置launch.json
launch.json是调用vscode debug性能的配置文件
{ "version": "0.2.0", "configurations": [ { "type": "lldb", "request": "launch", "name": "Debug", "program": "${workspaceFolder}/${fileBasenameNoExtension}", "args": [], "cwd": "${workspaceFolder}", "preLaunchTask": "Build with Clang" } ]}
关上debug窗口,打好断点,按下图操作
这三个配置文件中的参数阐明,在官网都有,我把一部分写在了正文里
References:
https://code.visualstudio.com...