launch.json文件
launch.json
文件是 visual studio code (vs code) 中用于配置调试会话的文件。它定义了调试器如何启动和运行程序。以下是 launch.json
文件的详细配置说明,包括常见的属性及其用途。
基本结构
launch.json
文件通常位于 .vscode
目录下,具有以下基本结构:
{ "version": "0.2.0", "configurations": [ { // 配置块 } ] }
主要属性
每个配置块代表一个调试配置,包含多个属性。以下是一些常见属性的说明:
- type: 调试器类型,如
python
,cppdbg
,node
,java
, 等。 - request: 调试请求类型,通常为
launch
(启动)或attach
(附加)。 - name: 配置名称,用户可以在调试配置列表中看到。
- program: 要调试的程序路径或文件。
- args: 传递给程序的命令行参数,数组形式。
- cwd: 当前工作目录。
- env: 环境变量设置。
- sourcemaps: 是否启用源映射(通常用于 javascript 调试)。
- prelaunchtask: 调试前要执行的任务(通常用于编译等)。
- postdebugtask: 调试结束后要执行的任务。
- stoponentry: 是否在程序入口处停止。
- console: 控制台类型,如
integratedterminal
,externalterminal
, 或internalconsole
。 - justmycode: 是否只调试用户代码(用于 python)。
- pythonpath: python 可执行文件的路径(用于 python)。
示例配置
以下是一些常见语言的 launch.json
配置示例:
python
{ "version": "0.2.0", "configurations": [ { "name": "python: current file", "type": "python", "request": "launch", "program": "${file}", "console": "integratedterminal" } ] }
c++
{ "version": "0.2.0", "configurations": [ { "name": "c++: g++ build and debug active file", "type": "cppdbg", "request": "launch", "program": "${filedirname}/${filebasenamenoextension}.out", "args": [], "stopatentry": false, "cwd": "${workspacefolder}", "environment": [], "externalconsole": true, "mimode": "gdb", "setupcommands": [ { "description": "enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignorefailures": true } ], "prelaunchtask": "g++ build active file", "midebuggerpath": "/usr/bin/gdb", "setupcommands": [ { "description": "enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignorefailures": true } ], "midebuggerargs": "", "stopatentry": false, "logging": { "moduleload": false, "programoutput": false, "trace": false, "traceresponse": false }, "windows": { "mimode": "gdb", "midebuggerpath": "gdb.exe" }, "osx": { "mimode": "lldb" }, "pipetransport": { "pipeprogram": "", "pipeargs": [], "debuggerpath": "/usr/bin/gdb", "pipecwd": "" }, "sourcefilemap": { "/mnt/c": "c:\\", "/mnt/d": "d:\\" } } ] }
node.js
{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "launch program", "skipfiles": ["<node_internals>/**"], "program": "${workspacefolder}/app.js" } ] }
常见配置项
1. python
- program: 要调试的 python 文件。
- pythonpath: python 解释器路径。
- args: 传递给 python 脚本的命令行参数。
- env: 环境变量。
- console: 控制台类型。
2. c++
- program: 可执行文件路径。
- args: 命令行参数。
- stopatentry: 是否在程序入口处暂停。
- cwd: 当前工作目录。
- environment: 环境变量。
- externalconsole: 是否使用外部控制台。
- mimode: 调试器模式(如
gdb
,lldb
)。 - midebuggerpath: 调试器路径。
3. node.js
- program: 要调试的 node.js 文件。
- args: 命令行参数。
- runtimeexecutable: node.js 可执行文件路径。
- runtimeargs: 传递给 node.js 的参数。
- env: 环境变量。
- sourcemaps: 是否启用源映射。
- outfiles: 编译输出文件路径。
使用示例
假设我们有一个 python 项目,并且我们希望配置一个调试会话,可以这样写:
{ "version": "0.2.0", "configurations": [ { "name": "python: current file", "type": "python", "request": "launch", "program": "${file}", "console": "integratedterminal", "justmycode": true } ] }
这个配置会使用当前打开的 python 文件作为程序入口,运行调试,并在 vs code 的集成终端中显示输出。
通过理解和正确配置 launch.json
文件,可以极大地提高调试效率和开发体验。不同语言和不同项目可能需要不同的配置,用户可以根据具体需求进行调整。
tasks.json
tasks.json
文件是 visual studio code (vs code) 中用于配置任务(tasks)的文件。这些任务可以是编译代码、运行测试、构建项目等自动化任务。以下是 tasks.json
文件的详细配置说明,包括常见的属性及其用途。
基本结构
tasks.json
文件通常位于 .vscode
目录下,具有以下基本结构:
{ "version": "2.0.0", "tasks": [ { // 任务配置块 } ] }
主要属性
每个任务配置块代表一个任务,包含多个属性。以下是一些常见属性的说明:
- label: 任务的名称或标签,用于在任务列表中标识任务。
- type: 任务类型,例如
shell
或process
。shell
表示任务将在 shell 中运行,process
表示任务将作为独立的进程运行。 - command: 要执行的命令,可以是编译器、构建工具、脚本等。
- args: 传递给命令的参数,数组形式。
- group: 任务分组,可以设置为
build
或test
,用于标识构建任务或测试任务。 - presentation: 控制任务输出的呈现方式,例如是否显示在终端中,是否清除之前的输出等。
- problemmatcher: 配置错误和警告的匹配器,用于从任务输出中解析错误和警告。
- options: 任务执行的选项,例如环境变量、当前工作目录等。
示例配置
以下是一些常见的 tasks.json
配置示例:
c++ 编译任务
{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${filedirname}/${filebasenamenoextension}.out" ], "group": { "kind": "build", "isdefault": true }, "problemmatcher": ["$gcc"], "detail": "generated task for building a c++ file using g++" } ] }
python 运行任务
{ "version": "2.0.0", "tasks": [ { "label": "run python file", "type": "shell", "command": "python", "args": [ "${file}" ], "group": { "kind": "build", "isdefault": true }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "problemmatcher": [] } ] }
node.js 运行任务
{ "version": "2.0.0", "tasks": [ { "label": "run node.js file", "type": "shell", "command": "node", "args": [ "${file}" ], "group": { "kind": "build", "isdefault": true }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "problemmatcher": [] } ] }
常见配置项
- label
任务的标签名称,用于在 vs code 任务列表中标识任务。
"label": "build"
- type
任务类型,可以是 shell
或 process
。shell
表示任务将在 shell 中运行,process
表示任务将作为独立的进程运行。
"type": "shell"
- command
要执行的命令,例如编译器、脚本或构建工具。
"command": "g++"
- args
传递给命令的参数,数组形式。
"args": [ "-g", "${file}", "-o", "${filedirname}/${filebasenamenoextension}.out" ]
- group
任务分组,用于标识任务的类别,可以是 build
或 test
。
"group": { "kind": "build", "isdefault": true }
- presentation
控制任务输出的呈现方式。
"presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }
- problemmatcher
用于解析任务输出中的错误和警告。vs code 内置了多种匹配器,例如 $gcc
, $eslint
等。
"problemmatcher": ["$gcc"]
- options
任务执行的选项,例如环境变量、当前工作目录等。
"options": { "cwd": "${workspacefolder}" }
使用示例
假设我们有一个 c++ 项目,并且我们希望配置一个编译任务,可以这样写:
{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${filedirname}/${filebasenamenoextension}.out" ], "group": { "kind": "build", "isdefault": true }, "problemmatcher": ["$gcc"], "detail": "generated task for building a c++ file using g++" } ] }
这个配置会使用 g++
编译当前打开的 c++ 文件,并将输出文件放在相同目录下,文件名与源文件相同但扩展名为 .out
。
通过理解和正确配置 tasks.json
文件,可以极大地提高构建和运行任务的自动化和效率。不同语言和不同项目可能需要不同的配置,用户可以根据具体需求进行调整。
tasks.json与launch.json文件的区别
tasks.json
和 launch.json
是 visual studio code (vs code) 中用于配置不同类型任务的文件,它们各自有不同的用途和配置方式。
tasks.json
tasks.json
用于配置和管理各种任务,例如编译代码、运行脚本、构建项目等。它定义了一些可以自动执行的任务,主要用于自动化构建、测试和其他开发流程。
主要功能和用途:
- 编译代码:如编译 c++ 或 java 代码。
- 运行脚本:如执行 python 或 shell 脚本。
- 构建项目:如使用构建工具(make、gradle、maven)构建项目。
- 其他任务:如清理生成文件、打包等。
主要属性:
- label: 任务的名称或标签。
- type: 任务类型,例如
shell
或process
。 - command: 要执行的命令。
- args: 传递给命令的参数。
- group: 任务分组,可以设置为
build
或test
。 - presentation: 控制任务输出的呈现方式。
- problemmatcher: 配置错误和警告的匹配器。
- options: 任务执行的选项,例如环境变量、当前工作目录等。
示例:
{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${filedirname}/${filebasenamenoextension}.out" ], "group": { "kind": "build", "isdefault": true }, "problemmatcher": ["$gcc"] } ] }
launch.json
launch.json
用于配置调试器的启动和运行参数。它定义了调试配置,主要用于在调试会话中启动程序、附加到正在运行的程序等。
主要功能和用途:
- 启动调试会话:配置调试器如何启动程序。
- 附加调试:配置调试器如何附加到正在运行的程序。
- 设置断点和观察点:调试过程中设置断点和观察点。
主要属性:
- type: 调试器类型,如
python
,cppdbg
,node
,java
等。 - request: 调试请求类型,通常为
launch
(启动)或attach
(附加)。 - name: 配置名称,用户可以在调试配置列表中看到。
- program: 要调试的程序路径或文件。
- args: 传递给程序的命令行参数。
- cwd: 当前工作目录。
- env: 环境变量设置。
- sourcemaps: 是否启用源映射(通常用于 javascript 调试)。
- prelaunchtask: 调试前要执行的任务(通常用于编译等)。
- postdebugtask: 调试结束后要执行的任务。
- stoponentry: 是否在程序入口处停止。
- console: 控制台类型,如
integratedterminal
,externalterminal
或internalconsole
。 - justmycode: 是否只调试用户代码(用于 python)。
示例:
{ "version": "0.2.0", "configurations": [ { "name": "python: current file", "type": "python", "request": "launch", "program": "${file}", "console": "integratedterminal" } ] }
主要区别
用途:
tasks.json
:用于配置和管理自动化任务(如编译、构建、运行脚本等)。launch.json
:用于配置调试器,定义调试会话的启动和运行参数。
配置内容:
tasks.json
:定义要执行的任务及其参数和选项。launch.json
:定义调试会话的参数和选项,包括要调试的程序、调试器类型、启动或附加模式等。
工作流:
tasks.json
:适用于日常开发中的重复任务,自动化构建和测试流程。launch.json
:适用于调试代码,启动调试会话或附加到正在运行的程序。
结合使用
在许多情况下,tasks.json
和 launch.json
可以结合使用。例如,可以在 launch.json
中定义一个调试配置,并在调试前执行一个由 tasks.json
配置的编译任务:
// launch.json { "version": "0.2.0", "configurations": [ { "name": "c++ debug", "type": "cppdbg", "request": "launch", "program": "${workspacefolder}/a.out", "args": [], "stopatentry": false, "cwd": "${workspacefolder}", "environment": [], "externalconsole": true, "mimode": "gdb", "setupcommands": [ { "description": "enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignorefailures": true } ], "prelaunchtask": "build" } ] }
// tasks.json { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${filedirname}/${filebasenamenoextension}.out" ], "group": { "kind": "build", "isdefault": true }, "problemmatcher": ["$gcc"] } ] }
这样,当你启动调试会话时,vs code 会先执行 tasks.json
中定义的编译任务,然后再启动调试。
总结
到此这篇关于vscode中launch.json与tasks.json文件的文章就介绍到这了,更多相关vscode launch.json与tasks.json文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论