1.下载VSCode
2.安装插件
3.打开一个包含C/C++文件的目录
没有,可以自己创建一个cpp文件
4.command+shift+p打开命令行工具窗口
配置c_cpp_properties.json文件
选择C/Cpp: Edit Configurations命令
此时会在当前工作空间目录生成.vscode配置目录
同时在配置目录会生成一个c_cpp_properties.json文件
"name": "Mac",
"includePath": [
"${workspaceFolder}",
"/Library/Developer/CommandLineTools/usr/include/c++/v1",
"/usr/local/include",
"/Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include",
"/Library/Developer/CommandLineTools/usr/include",
"/usr/include"
],
将要用到的库的路径添加到includePath数组中
配置tasks.json文件
选择Tasks: Configure Task命令
此时会在配置目录生成一个tasks.json文件
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}
修改该配置文件如下
"tasks": [
{
"label": "c++",
"command": "clang++",
"type": "shell",
"args": [
"hello.cpp",
"-std=c++11",
"-g"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
}
]
该文件其实就是一个命令行构建工具
把运行程序时在终端输入的命令和参数对于"command"和"args"的值
输入shift+command+b,便可构建成功,生成可执行文件a.out
配置launch.json文件
选择Debug: Open launch.json命令
此时会在配置目录生成一个launch.json文件
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
修改文件如下
"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++"
}
]
preLaunchTask后面配置的是task的label,作用是当源代码发生改变时,启动调试会自动编译,而不用主动输入shift+command+b进行编译
该文件是一个配置调试的文件
json文件说明
上述3个文件需要做一定的修改
5. 开启调试
修改hello.cpp
#include<stdio.h>
int
main()
{
printf("Hello World\nWelcome to go to C++\n");
}
点击调试汉字旁边的绿色小三角,就会出现执行结果
至此一个简单的c/c++程序就完成了从编写到执行的过程