VSCODE(七)创建一个任务

本文首先翻译了VSCODE官方文档对Task描述,然后通过C/C++的例子来说明自动化工具在VSCODE中的体现,最后介绍了如何配置Task,完成C/C++的编译任务。

一、官方文档关于Task的描述

自动化任务工具在linting(代码校验)、building(编译)、packaging(打包)、testing(测试)或者部署(部署)广泛的存在于工程实践中,如:TypeScript(编译器),ESLint、TSLint(代码校验)、Make CMake Gulp Jake Rake(编译系统)。

这些工具大多数在命令行中运行且可以在软件开发循环(编辑,编译,测试和调试)内部或外部。考虑到这些工具在软件开发周期的重要性,在VSCODE中能运行这些工具和分析结果将会非常有用。在VSCODE任务能够运行脚本和启动进程,因此很多工具能够直接在VSCODE中使用而不需要键入命令行命令、和重新修改(工具)代码。工作区或文件夹要完成的任务由位于工作文件目录下.vscode文件夹中的tasks.json文件配置。

一些扩展能够通过任务提供器来完成部分任务,这些新增任务配置将会被添加到指定工作区文件tasks.json文件夹中。

注意:任务只支持工作在工作区的文件,当你编辑单个文件是不可用的。

个人的看法:VSCODE基于文件夹的任务执行时,VSCODE会启动一个终端并执行tasks.json中给定的任务内容,这个内容必须是在shell中可执行的。这也就意味着,VSCODE通过这一机制拥有了强大的终端功能,编译这种“小”的任务不在话下,游刃有余。此外,一些扩展可能会协助你完成常用任务的书写,即提供一个简单的模板。(如C/C++提供编译的模板)

二、C/C++与自动化工具

由官方的表述中,我们知道了:

  • 自动化工具存在于软件的环节中
  • VSCODE提供了直接运这些工具的手段:Tasks.json
  • tasks.json可运行脚本和程序
  • Tasks.json是工作区相关的

相信大家在使用C/C++开发时都会下载C/C++插件,他完成的作用正如其简介中所述:智能感知、调试和代码浏览。代码浏览功能能够添加若干C/C++浏览附加功能,如Go to definition、Find All Reference等功能。这个插件帮助我们完成编辑、编译、调试功能。回想自动化工具的种类:

  • linting(代码校验)
  • building(编译);
  • packaging(打包);
  • testing(测试);
  • 部署(部署)。

C/C++工具提供了代码校验和编辑上的体验升级。但是他们没有办法完成后三个任务,对于C/C++而言,编译由g++gdb完成,编译打包测试部署由Makefile和CMake等自动化编译工具完成,VSCODE的task.json只是借助终端执行这些工具而已。这样做的好处有:

  • 高效利用了现有自动化工具
  • 自动化工具可复用

对于C/C++而言,task.json执行时,VSCODE将会创建一个终端并给定任务指令,VSCODE具备:

  • 操作系统的文件操作
  • 执行shell、python脚本
  • 运行自动化工具cmake、make
  • 运行指定程序

讲到这里,你应该明白了tasks.json自动化工具的关系。

三、配置一个C/C++编译任务

3.1 VSCODE中的JSON

VSCODE则是通过的配置文件格式是JSON,因此要编写一个让VSCODE识别的配置文件得先了解其语法格式。JSON(JavaScript Object Notation)JavaScript中描述对象的一种轻量化数据交换格式,相较于XMLJSON的结构更容易映射至一般语言的数据结构[2]。JSON的语法规则:

  • 数据在名称/值对中
  • 数据由逗号分隔
  • 花括号保存对象
  • 方括号保存数组

JSON的数据都是用"name":value形式书写,不同值之间使用逗号隔开。名称使用双引号括住,值可以是:

  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true 或 false)
  • 数组(在方括号中)
  • 对象(在花括号中)
  • null

对象是名称值的的容器,数组则是对象的容器。随便打开VSCODE中的一个JSON文件,如:settings.json:

{
    
    
    "window.restoreWindows": "all",
    "extensions.ignoreRecommendations": true,
    "latex-workshop.view.pdf.viewer": "tab",
    "editor.formatOnType": true,
    "editor.find.cursorMoveOnType": true,
    "workbench.colorTheme": "Visual Studio Dark",
    "workbench.editorAssociations": [
        {
    
    
            "viewType": "jupyter.notebook.ipynb",
            "filenamePattern": "*.ipynb"
        }
    ],
    "explorer.confirmDelete": false,
}

表示了一个对象,里边有8个名称/值对。在VSCODE中,文件名就代表某一类设置,我们要设置的task任务,也应该是一个诸如xxx.json的目录。

3.2 编写VSCODE的C/C++编译任务

tasks.json位于工作区中的.vscode。我们可以根据JSON语法直接在vscode编写任务内容,亦可以通过任务模板生成任务内容。带编译的程序如下:

//hello.cpp
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    
    
    cout<<"hello world"<<endl;
    vector<double> dvec(10,4396);
}

C/C++插件内置简单编译任务,在这里基础稍微修改即可。如何打开使用这个简单模板?

  • 搜索Tasks: Configure Tasks
  • 弹出对话中选择编译器C/C++:g++ build active file注意编译器要对应
    在这里插入图片描述
    因为编译的是C/C++文件,所以请我选了/usr/bin/g++。OK!模板生成了,让我们来看看这个模板都做了什么事情:
{
    
    
	"version": "2.0.0",
	"tasks": [
		{
    
    
			"type": "cppbuild",
			"label": "C/C++: gcc build active file",
			"command": "/usr/bin/g++",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
    
    
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "compiler: /usr/bin/gcc"
		}
	]
}

各键值对的具体的含义可以看看见附录(一)。我们关心的只有三个地方:

  • type
  • command
  • args

type选项可以是shellprocess,C/C++插件将shell取了别名cppbuild,表意性更强,不做修改;command可以是任何命令、脚本,因为是编译C/C++所以应该为编译器名字/usr/bin/g++,无需更改;args命令附加参数(即选项),与g++对应的自然为编译选项,可以看到,模板为我们默认开启调试-g、生成目标文件-o,保持不变,选项中还出现了一些变量名字,叫做预定义变量(Predefined variables),用于辅助描述编译所需的:

  • ${file} 当前正在编辑的文档名(cpp名)
  • ${fileDirname} 当前编辑的文档名对应目录
  • ${fileBasenameNoExtension} 无后缀当前编辑文档名

详细说明可以见附录(二)。那么上述task执行的命令等价于:g++ -g hello.cpp -o /home/junwu/Desktop/helloworld,因此默认的模板就是在当前目录下正在编辑的cpp生成与无后缀源文件的可执行程序并附加调试信息。label的含义验证了我们的说法:C/C++: gcc build active file

Tips:当前正在编辑文件名(Current Active File)不等于已经打开的编辑器(Open Editor)。

来看看最终的执行结果:
在这里插入图片描述
[1] https://code.visualstudio.com/docs/editor/tasks
[2] https://www.zhihu.com/question/25636060
[3] https://www.w3school.com.cn/json/json_syntax.asp
[4] https://code.visualstudio.com/docs/editor/variables-reference

附录1:

名字
label The task’s label used in the user interface.
type The task’s type. For a custom task, this can either be shell or process. If shell is specified, the command is interpreted as a shell command (for example
command The actual command to execute.
windows Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
group Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
presentation Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run.
options Override the defaults for cwd (current working directory), env (environment variables), or shell (default shell). Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attributes.
runOptions Defines when and how a task is run.

附录(二)

名字
${workspaceFolder} the path of the folder opened in VS Code
${workspaceFolderBasename} the name of the folder opened in VS Code without any slashes (/)
${file} the current opened file
${fileWorkspaceFolder} the current opened file’s workspace folder
${relativeFile} the current opened file relative to workspaceFolder
${relativeFileDirname} the current opened file’s dirname relative to workspaceFolder
${fileBasename} the current opened file’s basename
${fileBasenameNoExtension} the current opened file’s basename with no file extension
${fileDirname} the current opened file’s dirname
${fileExtname} the current opened file’s extension
${cwd} the task runner’s current working directory on startup
${lineNumber} the current selected line number in the active file
${selectedText} the current selected text in the active file
${execPath} the path to the running VS Code executable
${defaultBuildTask} the name of the default build task
${pathSeparator} the character used by the operating system to separate components in file paths

猜你喜欢

转载自blog.csdn.net/weixin_39258979/article/details/111163768