使用VsCode进行C++开发时,除了在机器上安装必要的编译工具(例如,gcc、g++、cmake等)之外,还需要在VsCode配置编译任务,从而可以通过点击或者快捷键的方式进行快速编译。
配置编译任务需要配置两个文件:
配置c_cpp_properties.json:
ctrl + p,输入> C/C++,在其中找到Edit Configuration:
需要配置的项主要有:
配置tasks.json:
ctrl + p,输入> tasks:
然后将json修改为:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
"-G",
"Unix Makefiles",
"-DCMAKE_BUILD_TYPE=Debug"
],
"group": "build",
"problemMatcher": [],
"detail": "CMake template build task"
},
{
"label": "make",
"command": "make",
"problemMatcher": []
},
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn": [
"cmake",
"make"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
tasks中有三个对象,第一个是执行cmake命令,第二个是make命令,第三个是将第一个和第二个整合起来,顺序调用cmake和make,并且设置为默认操作,因此,当选择Terminial->Run Build Task
时,就会依次执行cmake和make进行编译。
ctrl + p,输入> Debug:
随便选择一个应用类型,然后将launch.json修改为:
{
// 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": [
{
"type": "cppdbg",
"request": "launch",
"name": "gdb",
"program": "${workspaceFolder}/test",
"MIMode": "gdb",
"cwd": "${workspaceFolder}"
}
]
}
然后就可以打开调试窗口启动调试:
在调试窗口,可以设置断点,然后进行调试,调试过程中可能会使用到4个快捷键: