按 F5 会创建默认的。
给 vscode 的运行任务 tasks.json 添加快捷键绑定功能 默认的是 ctrl+shift+b 来选择任务,如果配置了默认任务则会直接运行,否则要手动选择。
// tasks.json
{
// https://code.visualstudio.com/docs/editor/tasks
"version": "2.0.0",
"tasks": [
{
"label": "Build", // 任务的名字叫Build,注意是大小写区分的,等会在launch中调用这个名字
"type": "shell", // 任务执行的是shell命令,也可以是
"command": "g++", // 命令是g++
"args": [
"'-Wall'",
"'-std=c++17'", //使用c++17标准编译
"'${file}'", //当前文件名
"-o", //对象名,不进行编译优化
"'${fileBasenameNoExtension}.exe'", //当前文件名(去掉扩展名)
],
// 所以以上部分,就是在shell中执行(假设文件名为filename.cpp)
// g++ filename.cpp -o filename.exe
"group": {
"kind": "build",
"isDefault": true
// 任务分组,因为是tasks而不是task,意味着可以连着执行很多任务
// 在build组的任务们,可以通过在Command Palette(F1) 输入run build task来运行
// 当然,如果任务分组是test,你就可以用run test task来运行
},
"problemMatcher": [
"$gcc" // 使用gcc捕获错误
],
}
]
}
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", //这个应该是F1中出现的名字
"preLaunchTask": "Build", //在launch之前运行的任务名,这个名字一定要跟tasks.json中的任务名字大小写一致
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe", //需要运行的是当前打开文件的目录中,名字和当前文件相同,但扩展名为exe的程序
"args": [],
"stopAtEntry": false, // 选为true则会在打开控制台后停滞,暂时不执行程序
"cwd": "${workspaceFolder}", // 当前工作路径:当前文件所在的工作空间
"environment": [],
"externalConsole": true, // 是否使用外部控制台,选false的话,我的vscode会出现错误
"MIMode": "gdb",
"miDebuggerPath": "c:/MinGW/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}]
}
.launch.json
所需要调试的文件的路径、调试时的CWD(工作路径)、调试器的路径及一些调试参数(程序启动参数等);
{
// 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": [
{
"cwd": "${workspaceRoot}",
"executable": "./bin/HAL_Test.elf",
"name": "stm32 Debug",
"request": "launch",
"type": "cortex-debug",
"servertype": "stutil",
"device": "STM32F407ZG",
"preLaunchTask": "编译并下载",
"postDebugTask": "复位设备"
}
]
}
3.tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "编译",
"type": "shell",
"command": "make -j6",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "编译并下载",
"type": "shell",
"command": "make -j6 && make update",
"problemMatcher": []
},
{
"label": "重新编译",
"type": "shell",
"command": "make clean && make -j6",
"problemMatcher": []
},
{
"label": "复位设备",
"type": "shell",
"command": "STM32_Programmer_CLI -c port=SWD -hardRst",
"problemMatcher": []
}
]
}