最近在工作中使用C++开发框架,期间需要进行debug,所以学习了下如何在vscode中配置相关编译和调试的配置。

🌵 vscode配置c++开发环境

vscode使用三种文件实现对c++开发的环境管理、编译和调试

  • c_cpp_properties.json:C++开发环境的管理
  • tasks.json:编译过程的配置
  • launch.json:调试配置

熟悉vscode c++配置中的常用的变量

  • ${workspaceFolder}:当前工作区目录

创建c_cpp_properties.json

快捷键:command + p,选择> C/C++: Edit Configuration(UI)

根据提示进行配置

创建tasks.json

快捷键:command+p,选择>Tasks: Configure Task -> Create tasks.json file from template -> Others

1
2
3
4
5
6
7
8
9
10
11
12
{
// 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"
}
]
}

修改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run_cmake",
"type": "shell",
"command": "mkdir -p ./build; cd ./build; cmake -DCMAKE_BUILD_TYPE=Debug ..; make -j8;",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "clean",
"type": "shell",
"command": "make clean",
}
]
}

vscode使用tasks实现与外界功能的交互,有点像Gitlab CI的配置

  • label:当前task的名称
  • group:当前task所属的组

创建launch.json

点击vscode左侧的Run and Debug图标,选择create a launch.json file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
// 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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}

修改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
// 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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/run_main",
"preLaunchTask": "run_cmake", // 依赖之前的run_cmake, 每次debug时会
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}", // 运行debug的路径
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}

References: