先编译以后(preLaunchTask
)才能用gdb debug。
- 生成tasks.json文件:编译:
ctrl shift p
–Tasks: Config Task
–Create tasks.json file from template
–Others
{
"version": "2.0.0",
"tasks": [
{
//If `shell` is specified, the command is interpreted as a shell command
"type": "shell",
"label": "gfortran build",
"command": "gfortran",
"args": [
"-fdiagnostics-color=always",
"${file}",
"-g3", // 生成调试信息,-g3最高级别
"-O0", // 关闭优化,编译时间短,适合调试阶段
"-fbacktrace", // 堆栈回溯信息
"-fcheck=all", // 所有运行时检查
"-Wall", // 启用所有警告
"-Wextra", // 启用额外的警告
"-Warray-temporaries", // 数组临时变量警告
"-Wconversion", // 类型转换警告
"-ffree-line-length-0", // 支持无限制的自由格式行长度
"-o", // 目标名称
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"group": {
"kind": "build",
"isDefault": true // 默认task
},
"options": {
"cwd": "${fileDirname}"
},
"detail": "Task generated by Debugger."
}
]
}
- 创建launch.json:debug
{
// 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": "(gdb) Fortran",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "gfortran build",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [], // Possible input args for a.out
"stopAtEntry": false,
"cwd": "${fileDirname}", // the task runner's current working directory
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true,
},
],
},
],
}
参考链接:
- https://marketplace.visualstudio.com/items?itemName=fortran-lang.linter-gfortran#debugging
- https://submit.mit.edu/submit-users-guide/tutorials/tutorial_5.html
- https://code.visualstudio.com/docs/editor/debugging
- https://code.visualstudio.com/docs/editor/tasks
- https://code.visualstudio.com/docs/cpp/config-mingw
- https://cs.pynote.net/sf/c/cdm/202111137/#_1
- https://zhuanlan.zhihu.com/p/513148985