launch.json用于配置vscode调试,包含version和configurations数组,常见字段有name、type、request、program等,支持node.js、python、c++等语言调试,可通过调试面板快速生成。
VSCode 的调试配置通过项目根目录下的 .vscode/launch.json 文件定义。这个文件告诉 VSCode 如何启动和调试你的程序。下面介绍常见写法和关键字段。
1. 基本结构
每个 launch.json 都包含一个 version 和一个 configurations 数组,数组中每个对象代表一种调试配置:
{ "version": "0.2.0", "configurations": [ { "name": "调试 node.js 程序", "type": "node", "request": "launch", "program": "${workspaceFolder}/index.js" } ] }
2. 常用字段说明
以下是核心字段的含义:
- name:配置名称,出现在调试侧边栏下拉菜单中
- type:调试器类型,如 node(Node.js)、python、cppdbg(C++)等
- request:请求类型,通常为 launch(启动程序)或 attach(附加到已运行进程)
- program:要运行的入口文件,常用变量如 ${workspaceFolder} 表示项目根目录
- args:传递给程序的命令行参数,以数组形式书写
- cwd:程序运行时的工作目录
- env:环境变量设置
- console:控制台类型,可选 integratedTerminal、internalConsole 或 externalTerminal
3. 常见语言示例
Node.js 调试
{ "name": "启动应用", "type": "node", "request": "launch", "program": "${workspaceFolder}/app.js", "cwd": "${workspaceFolder}", "console": "integratedTerminal", "env": { "NODE_ENV": "development" } }
Python 调试(需安装 Python 扩展)
{ "name": "运行 Python 文件", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "args": ["--verbose"], "cwd": "${workspaceFolder}" }
C++ 调试(使用 gdb)
{ "name": "调试 C++", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/app", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }
4. 快速生成 launch.json
在 VSCode 中打开调试面板(Ctrl+Shift+D),点击“创建 launch.json”按钮,选择对应环境(如 Node.js、Python),会自动生成模板。 确保你已经安装了对应语言的调试扩展,否则 type 类型无法识别。 基本上就这些。根据你的运行环境调整字段即可。