launch.json 文件是 Visual Studio Code (VS Code) 中用于配置调试会话的文件。它定义了调试器如何启动和运行程序。以下是 launch.json 文件的详细配置说明,包括常见的属性及其用途。
launch.json 文件通常位于 .vscode 目录下,具有以下基本结构:
{ "version": "0.2.0", "configurations": [ { // 配置块 } ] } 每个配置块代表一个调试配置,包含多个属性。以下是一些常见属性的说明:
python, cppdbg, node, java, 等。launch(启动)或 attach(附加)。integratedTerminal, externalTerminal, 或 internalConsole。以下是一些常见语言的 launch.json 配置示例:
{ "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal" } ] } { "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:\\" } } ] } { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "skipFiles": ["/**"], "program": "${workspaceFolder}/app.js" } ] } gdb, lldb)。假设我们有一个 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 文件是 Visual Studio Code (VS Code) 中用于配置任务(Tasks)的文件。这些任务可以是编译代码、运行测试、构建项目等自动化任务。以下是 tasks.json 文件的详细配置说明,包括常见的属性及其用途。
tasks.json 文件通常位于 .vscode 目录下,具有以下基本结构:
{ "version": "2.0.0", "tasks": [ { // 任务配置块 } ] } 每个任务配置块代表一个任务,包含多个属性。以下是一些常见属性的说明:
shell 或 process。shell 表示任务将在 shell 中运行,process 表示任务将作为独立的进程运行。build 或 test,用于标识构建任务或测试任务。以下是一些常见的 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"], "detail": "Generated task for building a C++ file using g++" } ] } { "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": [] } ] } { "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": [] } ] } 任务的标签名称,用于在 VS Code 任务列表中标识任务。
"label": "build" 任务类型,可以是 shell 或 process。shell 表示任务将在 shell 中运行,process 表示任务将作为独立的进程运行。
"type": "shell" 要执行的命令,例如编译器、脚本或构建工具。
"command": "g++" 传递给命令的参数,数组形式。
"args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.out" ] 任务分组,用于标识任务的类别,可以是 build 或 test。
"group": { "kind": "build", "isDefault": true } 控制任务输出的呈现方式。
"presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" } 用于解析任务输出中的错误和警告。VS Code 内置了多种匹配器,例如 $gcc, $eslint 等。
"problemMatcher": ["$gcc"] 任务执行的选项,例如环境变量、当前工作目录等。
"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 是 Visual Studio Code (VS Code) 中用于配置不同类型任务的文件,它们各自有不同的用途和配置方式。
tasks.jsontasks.json 用于配置和管理各种任务,例如编译代码、运行脚本、构建项目等。它定义了一些可以自动执行的任务,主要用于自动化构建、测试和其他开发流程。
主要功能和用途:
主要属性:
shell 或 process。build 或 test。示例:
{ "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.jsonlaunch.json 用于配置调试器的启动和运行参数。它定义了调试配置,主要用于在调试会话中启动程序、附加到正在运行的程序等。
主要功能和用途:
主要属性:
python, cppdbg, node, java 等。launch(启动)或 attach(附加)。integratedTerminal, externalTerminal 或 internalConsole。示例:
{ "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 中定义的编译任务,然后再启动调试。
上一篇:IDEA版本推荐