sudo apt update
#通过以下命令安装编译器和调试器
sudo apt install build-essential gdb
#安装成功确认
gcc --version
g++ --version
gdb --version
#安装
sudo apt install cmake
#查看
cmake --version
在配置这个文件夹的时候只需要关注两个地方,一个是配置可执行文件的绝对路径,另一个是指定在执行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
//program:可执行文件的绝对路径
//prelauchTask:在执行调试前,需要执行task.json中label为Build的指令编译可执行文件
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
这个文件的任务主要是告诉vscode如何去编译程序,在按下F5的时候先执行这个文件以生成可执行文件,并提供给launch.json调试。那如何在配合cmake的情况下配置编译指令呢?
"options": {
"cwd": "${workspaceFolder}/build"
},
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [".."]
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "make",
"args": []
},
{
"label": "Build",
"dependsOrder": "sequence", // 按照列出的顺序执行任务依赖项
"dependsOn": [
"cmake",
"make"
]
}
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
".."
]
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "make",
"args": []
},
{
"label": "Build",
"dependsOrder": "sequence", // 按照列出的顺序执行任务依赖项
"dependsOn": [
"cmake",
"make"
]
}
]
}