在 Visual Studio Code 中,launch.json 和 tasks.json 作为两个重要的配置文件,能够帮助开发者实现调试和任务运行的自动化配置,所以理解这两个配置文件的作用和其内容含义尤为重要。这两个配置文件一般位于项目根目录的 .vscode 文件夹下。
注:本文中以 c++ 的配置为例,其它语言请参考其他文章。
launch.json 文件是 Visual Studio Code 中用于配置调试会话的文件。它定义了调试器如何启动和运行程序。主要用于在调试会话中启动程序、附加到正在运行的程序等。
以下是 launch.json 文件的详细配置说明,包括常见的属性及其用途。
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/xxx.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"
}
]
}用于配置和管理各种任务,例如编译代码、运行脚本、构建项目等。它定义了一些可以自动执行的任务,主要用于自动化构建、测试和其他开发流程。
主要属性:
{
"version": "0.2.0",
"tasks": [
{
"label": "build", // 与 launch.json中的 preLaunchTask保持一致
"type": "shell"
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}在实际过程中,两者一般是配合使用的。例如上面两个示例中,在 launch.json 中指定 preLaunchTask 为 build,即 tasks.json 中 label 指定的任务。这样,当你启动调试会话时,VS Code 会先执行 tasks.json 中定义的编译任务,然后再启动调试。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。