1.下載VSCode
2.安裝插件
3.打開一個(gè)包含C/C++文件的目錄
沒有,可以自己創(chuàng)建一個(gè)cpp文件
4.command+shift+p打開命令行工具窗口
配置c_cpp_properties.json文件
選擇C/Cpp: Edit Configurations命令
此時(shí)會(huì)在當(dāng)前工作空間目錄生成.vscode配置目錄
同時(shí)在配置目錄會(huì)生成一個(gè)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數(shù)組中
配置tasks.json文件
選擇Tasks: Configure Task命令
此時(shí)會(huì)在配置目錄生成一個(gè)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"
}
}
]
該文件其實(shí)就是一個(gè)命令行構(gòu)建工具
把運(yùn)行程序時(shí)在終端輸入的命令和參數(shù)對(duì)于"command"和"args"的值
輸入shift+command+b饮六,便可構(gòu)建成功做院,生成可執(zhí)行文件a.out
配置launch.json文件
選擇Debug: Open launch.json命令
此時(shí)會(huì)在配置目錄生成一個(gè)launch.json文件
{
// 使用 IntelliSense 了解相關(guān)屬性。
// 懸停以查看現(xiàn)有屬性的描述粤攒。
// 欲了解更多信息望抽,請(qǐng)?jiān)L問: 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算途,作用是當(dāng)源代碼發(fā)生改變時(shí),啟動(dòng)調(diào)試會(huì)自動(dòng)編譯隘梨,而不用主動(dòng)輸入shift+command+b進(jìn)行編譯
該文件是一個(gè)配置調(diào)試的文件
json文件說明
上述3個(gè)文件需要做一定的修改
5. 開啟調(diào)試
修改hello.cpp
#include<stdio.h>
int
main()
{
printf("Hello World\nWelcome to go to C++\n");
}
點(diǎn)擊調(diào)試漢字旁邊的綠色小三角程癌,就會(huì)出現(xiàn)執(zhí)行結(jié)果
至此一個(gè)簡單的c/c++程序就完成了從編寫到執(zhí)行的過程