1.安装vscode
2.插件安装
安装“C/C++”、“C/C++ Clang Command Adapter”
3.新建cpp文件
4.配置2 c_cpp_properties.json
按 Command + Shift + P
打开命令行面板,输入 C/Cpp: Edit Configurations
命令
此时会在当前工作空间目录生成.vscode配置目录
同时在配置目录会生成一个c_cpp_properties.json文件
将要用到的库的路径添加到includePath数组中
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}",
"/Library/Developer/CommandLineTools/usr/include/c++/v1",
"/usr/local/include",
"/Library/Developer/CommandLineTools/usr/include",
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++98",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
5.配置1 tasks.json
按 Command + Shift + P
打开命令行面板,输入 tasks
,选择 Tasks:Configure Task
生成 tasks.json
配置文件
该文件其实就是一个命令行构建工具
把运行程序时在终端输入的命令和参数对于"command"和"args"的值
输入shift+command+b,便可构建成功,生成可执行文件a.out
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "c++",
"command": "clang++",
"type": "shell",
"args": [
"main.cpp",
"-std=c++11",
"-g"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new"
}
}
]
}
6.配置3 launch.json
如果需要对项目进行进行 debug
,则需要先生成 launch.json
文件并对其做相应的配置
-
按
Command + Shift + D
进入到运行界面 -
点击
创建 launch.json 文件
,选择C++(GDB/LLDB)
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "c/c++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask":"c++"
}
]
![image-20210816193726334](/Users/liuxuan14/Library/Application Support/typora-user-images/image-20210816193726334.png)
7.shift+command+b编译失败
于VScode报错“终端将被任务重用,按任意键关闭”的解决方案:
原因是本地端口是被共享的,所以必须关闭一个进程才能执行另一个,只需要对 tasks.json
文件做一些修改即可。
tasks.json里把"panel"="shared"改成:"panel"="new"
8.如何运行?
输入shift+command+b,便可构建成功,生成可执行文件a.out
编译完成后,会在项目路径下生成 main
文件,使用 ./main
即可执行