VSCode调试python和cpp
python代码调试-launch.josn
python调试只需建一个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": [
{
"name": "Python Debugger: Current File", //调试器名称
"type": "debugpy",
"request": "launch",
"program": "${file}", //运行哪一个程序
"program": "demo.py", // 指定文件
"console": "integratedTerminal",
"justMyCode": true
}
]
}
cpp代码调试-tasks.json
cpp调试需要预先编译,则需要建一个tasks.json
-
点击
Terminal
, 在目录下点击Run Build Task
,然后点击齿轮形状的按钮自动生成tasks.json
-
{ "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", //g++的路径 "args": [ "-fdiagnostics-color=always",//输出颜色 "-std=c++11", "-g", //调试信息 "-Wall", //开启所有警告 "${file}", //编译文件 "-o", //输出文件 "${fileDirname}/${fileBasenameNoExtension}" //文件所在的文件夹路径/当前文件的文件名,不带后缀 ], "options": { "cwd": "${fileDirname}" //编译路径 }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build",//编译 "isDefault": true //默认编译 }, "detail": "compiler: /usr/bin/g++" } ] }
-
在
launch.json
中添加配置gdb(launch)
。(a)添加字段"preLaunchTask"
为tasks.json
的label
进行编译。(b)修改字段"program"
为编译输出文件的路径。{ // 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) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/release/${fileBasenameNoExtension}", //文件位置 "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "Set Disassembly Flavor to Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++ build active file", //编译tasks.json的label } ] }
VSCode变量解释
以 /home/Coding/Test/.vscode/tasks.json为例
${workspaceFolder}: 当前workspace文件夹路径 即/home/Coding/Test/
${workspaceRootFolderName}: 当前workspace文件夹名 即Test
${file}: 文件自身的绝对路径 即/home/Coding/Test/.vscode/tasks.json
${relativeFile}: 文件的相对路径 即.vscode/tasks.json
${fileBasenameNoExtension}: 当前文件的文件名,不带后缀 即tasks
${filebasename}: 当前文件的文件名 即tasks.json
${fileDirname}: 文件所在的文件夹路径 即/home/Coding/Test/.vscode/
${fileExtname}: 当前文件的后缀 即.json
${lineNumber}: 当前文件的光标所在的行号
${env:PATH}: 系统中的环境变量