VScode tasks.json和launch.json的设置

目录

  1. C++(方法1:不使用VSCode插件,较繁琐)
  2. C++(方法2:使用Native Debug插件)
  3. C++(方法3:使用C/C++ Compile Run插件)
  4. Rust(使用Native Debug插件)
  5. Typescript(仅做参考,这是用来写vscode插件时的项目设置)
  6. 一些常用的预定义变量

在网上搜了几篇文章,都写的不是很详细,而且好多都过时了。尤其很多是task0.1.0的版本,vscode自动会屏蔽掉。

根据我的理解,tasks用于在launch前执行任务,launch用于读取执行文件。

这两者经常组合起来用于需要编译语言的自动编译+自动执行,下面以C++、Rust和Typescript为例。

一、 C++(方法1:不使用VSCode插件,较繁琐)

在当前文件是C++的情况下,tasks可以被用来做编译,而launch用来执行编译好的文件。

// tasks.json
{
    // https://code.visualstudio.com/docs/editor/tasks
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",  // 任务的名字叫Build,注意是大小写区分的,等会在launch中调用这个名字
            "type": "shell",  // 任务执行的是shell命令,也可以是
            "command": "g++", // 命令是g++
            "args": [
                "'-Wall'",
                "'-std=c++17'",  //使用c++17标准编译
                "'${file}'", //当前文件名
                "-o", //对象名,不进行编译优化
                "'${fileBasenameNoExtension}.exe'",  //当前文件名(去掉扩展名)
            ],
          // 所以以上部分,就是在shell中执行(假设文件名为filename.cpp)
          // g++ filename.cpp -o filename.exe
            "group": { 
                "kind": "build",
                "isDefault": true   
                // 任务分组,因为是tasks而不是task,意味着可以连着执行很多任务
                // 在build组的任务们,可以通过在Command Palette(F1) 输入run build task来运行
                // 当然,如果任务分组是test,你就可以用run test task来运行 
            },
            "problemMatcher": [
                "$gcc" // 使用gcc捕获错误
            ],
        }
    ]
}

下面是launch.json文件
里面主要是编译器的参数,可以使用ctrl+space来查看有哪些可用参数
也可以在configurations中存在鼠标光标的情况下,点击右下自动出现的Add Configurations按钮
为什么选gdb不选 windows?因为这个不会执行预任务,也就没法编译文件了
为什么选 launch不选attach,是因为attach用来给正在执行的文件用的,比如网页中的组件,而launch是执行新文件

// launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", //这个应该是F1中出现的名字
            "preLaunchTask": "Build",  //在launch之前运行的任务名,这个名字一定要跟tasks.json中的任务名字大小写一致
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", //需要运行的是当前打开文件的目录中,名字和当前文件相同,但扩展名为exe的程序
            "args": [],
            "stopAtEntry": false, // 选为true则会在打开控制台后停滞,暂时不执行程序
            "cwd": "${workspaceFolder}", // 当前工作路径:当前文件所在的工作空间
            "environment": [],
            "externalConsole": true,  // 是否使用外部控制台,选false的话,我的vscode会出现错误
            "MIMode": "gdb",
            "miDebuggerPath": "c:/MinGW/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

二、C++(方法2:使用Native Debug插件)

VSCode里有一个Native Debug插件能大大简化上述配置,请点击下载。

下载安装之后,我们再来试试。

使用Native Debug插件自动生成https://www.zhihu.com/video/1180786852859256832

三、C++(方法3:使用C/C++ Compile Run插件)

VSCode的这个插件更简单,但只能用于单文件。

这下根本无需配置task.json和launch.json,保存后直接按F6自动编译运行,其他功能见插件下载页的how to use。

Native Debug插件和C/C++ Compile Run插件的对比:
前者更通用(可用于多种编译语言),后者更简单。

四、Rust(使用Native Debug插件)

适用于cargo new建立的项目。

具体操作https://www.zhihu.com/video/1180787461290168320
//tasks.json
{
 // See https://go.microsoft.com/fwlink/?LinkId=733558
 // for the documentation about the tasks.json format
 "version": "2.0.0",
 "tasks": [
        {
 "label": "Build",
 "type": "shell",
 "command": "cargo build"
        }
    ]
}


// launch.json
{
 // Use IntelliSense to learn about possible attributes.
 // Hover to view descriptions of existing attributes.
 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
 "version": "0.2.0",
 "configurations": [
        {
 "name": "Debug",
 "type": "gdb",  // 使用Native Debug插件提供的gdb类型
 "request": "launch",
 "target": "./target/debug/${workspaceFolderBasename}.exe", // 注意这里使用了第五部分讲到的配置变量
 "preLaunchTask": "Build",
 "cwd": "${workspaceRoot}",
 "valuesFormatting": "parseText"
        }
    ]
}

五、Typescript(仅做参考,这是用来写vscode插件时的项目设置)

tasks.json

// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "npm",
			"script": "watch",
			"problemMatcher": "$tsc-watch",
			"isBackground": true,
			"presentation": {
				"reveal": "never"
			},
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}

launch.json

{
	"version": "0.2.0",
	"configurations": [
		{
			"name": "Run Extension",
			"type": "extensionHost",
			"request": "launch",
			"runtimeExecutable": "${execPath}",
			"args": [
				"--extensionDevelopmentPath=${workspaceFolder}"
			],
			"outFiles": [
				"${workspaceFolder}/out/**/*.js"
			],
			"preLaunchTask": "${defaultBuildTask}"
		},
		{
			"name": "Extension Tests",
			"type": "extensionHost",
			"request": "launch",
			"runtimeExecutable": "${execPath}",
			"args": [
				"--extensionDevelopmentPath=${workspaceFolder}",
				"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
			],
			"outFiles": [
				"${workspaceFolder}/out/test/**/*.js"
			],
			"preLaunchTask": "${defaultBuildTask}"
		}
	]
}

六、一些常用的预定义变量

预定义变量#

支持下面的预定义变量:

  • ${workspaceFolder} - 当前工作目录(根目录)
  • ${workspaceFolderBasename} - 当前文件的父目录
  • ${file} - 当前打开的文件名(完整路径)
  • ${relativeFile} - 当前根目录到当前打开文件的相对路径(包括文件名)
  • ${relativeFileDirname} - 当前根目录到当前打开文件的相对路径(不包括文件名)
  • ${fileBasename} - 当前打开的文件名(包括扩展名)
  • ${fileBasenameNoExtension} - 当前打开的文件名(不包括扩展名)
  • ${fileDirname} - 当前打开文件的目录
  • ${fileExtname} - 当前打开文件的扩展名
  • ${cwd} - 启动时task工作的目录
  • ${lineNumber} - 当前激活文件所选行
  • ${selectedText} - 当前激活文件中所选择的文本
  • ${execPath} - vscode执行文件所在的目录
  • ${defaultBuildTask} - 默认编译任务(build task)的名字

预定义变量示例:

假设你满足以下的条件

  1. 一个文件 /home/your-username/your-project/folder/file.ext 在你的编辑器中打开;
  2. 一个目录 /home/your-username/your-project 作为你的根目录.

下面的预定义变量则代表:

  • ${workspaceFolder} - /home/your-username/your-project
  • ${workspaceFolderBasename} - your-project
  • ${file} - /home/your-username/your-project/folder/file.ext
  • ${relativeFile} - folder/file.ext
  • ${relativeFileDirname} - folder
  • ${fileBasename} - file.ext
  • ${fileBasenameNoExtension} - file
  • ${fileDirname} - /home/your-username/your-project/folder
  • ${fileExtname} - .ext
  • ${lineNumber} - 光标所在行
  • ${selectedText} - 编辑器中所选择的文本
  • ${execPath} - Code.exe的位置
Tip: vscode的智能提示会在tasks.jsonlaunch.json 提示所有支持的预定义变量.
编辑于 2022-11-01 09:08