launch.json和tasks.json作用
时间: 2023-06-05 22:47:42 浏览: 190
launch.json和tasks.json是用于配置调试和任务的文件。
launch.json用于配置调试,例如设置调试的目标程序、断点、环境变量等等。
tasks.json用于配置任务,例如运行Python程序、调用外部脚本等等。
这两个文件都是在VS Code中使用时非常实用和必要的。
相关问题
windows操作系统下,在使用vscode时,编译一次后,再修改过代码,代码不生效,给出3钟解决方法,并给出相应的launch.json和tasks.json的配置文件代码
问题描述:在使用 Visual Studio Code 编写 C/C++ 代码时,修改代码后再次编译运行时发现代码没有生效。
解决方法:
1. 关闭 Visual Studio Code,重新开启,再次编译运行。
2. 使用 Visual Studio Code 自带的终端进行编译运行。
3. 在 tasks.json 文件中添加 "args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}"] 配置,以便在每次编译时清除缓存。
launch.json 配置文件代码:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"preLaunchTask": "C++ Build"
}
]
}
```
tasks.json 配置文件代码:
```json
{
"version": "2.0.0",
"tasks": [
{
"label": "C++ Build",
"type": "shell",
"command": "g++",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}"],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
```
注意:以上配置文件代码是基于 MinGW 环境下的,如果你使用的是其他编译器环境,需要相应地进行修改。
tasks.json和launch.json的区别
### VSCode 中 `tasks.json` 与 `launch.json` 的作用
#### `tasks.json`
`tasks.json` 文件主要用于定义各种可以在 Visual Studio Code (VSCode) 内部执行的任务。这些任务可以涉及编译、构建或其他任何可以通过命令行完成的操作。对于C++项目的构建,此文件中会具体描述如何调用编译器来创建最终的可执行文件.exe[^1]。
例如,在处理.NET Core程序时,通过配置`tasks.json`中的特定条目,能够指定使用`dotnet build`这样的指令来进行项目的编译工作[^2]。
```json
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$msCompile"
}
```
这段JSON片段展示了怎样设置一个简单的编译任务,它告诉VSCode当用户想要构建项目的时候应该做什么操作。
#### `launch.json`
相比之下,`launch.json` 则专注于调试功能。该文件包含了启动配置项,允许开发者设定断点并逐步跟踪代码执行过程。每一个配置对象都代表了一种可能的调试场景;比如上面提到的例子就展示了一个针对Python脚本的简单调试环境设置[^3]。
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build", // 这里关联了之前定义好的task
"internalConsoleOptions": "openOnSessionStart"
}
]
}
```
这里给出的是适用于C/C++应用程序的一个典型GDB调试配置实例,值得注意的是`preLaunchTask`字段用来指向前述提及过的`tasks.json`里的某个预设任务名称——这使得每次开始调试前都会自动先尝试重新构建最新版本的应用程序。
### 主要区别总结
- **目的不同**: `tasks.json`负责定义一系列可用于自动化流程(如编译)的任务列表;而`launch.json`则侧重于提供多种方式让用户能够在不同的环境中有效地测试和排查问题。
- **触发机制差异**: 用户可以直接从终端或者菜单选项卡手动触发由`tasks.json`所规定的各项活动;相反地,只有在启动调试会话期间才会应用到`launch.json`内的参数集。
阅读全文
相关推荐
















