![](https://img-blog.csdnimg.cn/20210412224055543.png)
☞ ░ 前往老猿Python博客 ░ https://blog.csdn.net/LaoYuanPython
一、引言
在《信创之国产浪潮电脑+统信UOS操作系统体验2:安装visual studio code和cmake搭建C++开发环镜》介绍了在国产浪潮电脑+统信UOS操作系统中安装visual studio code和cmake搭建C++开发环镜的过程及案例,但上述过程仅限于编译,无法执行调试,且编译需要在控制台输入cmake和make指令才能执行。
实际上vscode是支持通过配置可以实现类似Visual C++等IDE开发工具使用菜单和快捷键直接进行程序编译构建的,这样构建的任务可以结合后续的调试配置进行IDE环境的程序调试,不过在之前必须先在任务配置文件tasks.json配置编译构建任务。
熟练掌握tasks.json的配置方法有助于灵活配置编译构建任务,本文将详细介绍tasks.json包含的各任务配置要素,并举简例说明,后续博文再介绍C++编译任务的构建。
二、tasks.json文件功能简述
tasks.json文件是vscode用于进行任务处理的配置文件,支持如下任务配置及其功能:
- 编译C++代码:可以使用g++编译器编译C++代码,并生成可执行文件
- 运行Python代码:可以使用Python解释器运行Python代码,并输出结果
- 调试Node.js代码:可以使用Node.js调试器调试Node.js代码,并查看变量值、调用栈等信息
- 执行Shell命令:可以执行Shell命令,并输出结果。
在tasks.json配置文件中,可以定义多个任务,每个任务都包含了一些属性,例如任务名称、任务类型、执行命令、命令参数、工作目录、输出等,这些任务还可以指定执行依赖关系。通过配置tasks.json文件,可以方便地在Visual Studio Code中执行这些任务,提高开发效率。
三、tasks.json文件官方定义
tasks.json文件的配置项官方定义请见vscode的官方文档,下面是相关定义及注释:
interface TaskConfiguration extends BaseTaskConfiguration {
/**
* The configuration's version number
*/
version: '2.0.0';
/**
* Windows specific task configuration
*/
windows?: BaseTaskConfiguration;
/**
* macOS specific task configuration
*/
osx?: BaseTaskConfiguration;
/**
* Linux specific task configuration
*/
linux?: BaseTaskConfiguration;
}
interface BaseTaskConfiguration {
/**
* The type of a custom task. Tasks of type "shell" are executed
* inside a shell (e.g. bash, cmd, powershell, ...)
*/
type: 'shell' | 'process';
/**
* The command to be executed. Can be an external program or a shell
* command.
*/
command: string;
/**
* Specifies whether a global command is a background task.
*/
isBackground?: boolean;
/**
* The command options used when the command is executed. Can be omitted.
*/
options?: CommandOptions;
/**
* The arguments passed to the command. Can be omitted.
*/
args?: string[];
/**
* The presentation options.
*/
presentation?: PresentationOptions;
/**
* The problem matcher to be used if a global command is executed (e.g. no tasks
* are defined). A tasks.json file can either contain a global problemMatcher
* property or a tasks property but not both.
*/
problemMatcher?: string | ProblemMatcher | (string | ProblemMatcher)[];
/**
* The configuration of the available tasks. A tasks.json file can either
* contain a global problemMatcher property or a tasks property but not both.
*/
tasks?: TaskDescription[];
}
/**
* Options to be passed to the external program or shell
*/
export interface CommandOptions {
/**
* The current working directory of the executed program or shell.
* If omitted the current workspace's root is used.
*/
cwd?: string;
/**
* The environment of the executed program or shell. If omitted
* the parent process' environment is used.
*/
env?: { [key: string]: string };
/**
* Configuration of the shell when task type is `shell`
*/
shell: {
/**
* The shell to use.
*/
executable: string;
/**
* The arguments to be passed to the shell executable to run in command mode
* (e.g ['-c'] for bash or ['/S', '/C'] for cmd.exe).
*/
args?: string[];
};
}
从上述资料可以看出,代表任务配置的真正核心为BaseTaskConfiguration,实际上BaseTaskConfiguration的最常用配置项是tasks,BaseTaskConfiguration的部分配置项与其子配置项tasks下的子配置项相同,关于tasks请参考老猿在CSDN博文《Visual Studio Code中的任务配置文件tasks.json中的可选任务组tasks详解》的详细介绍。
四、tasks.json文件格式及内容
4.1、任务类型
从官方文档可见,一个典型的task.json文件包含多种属性,格式复杂,任务可以分为全局任务和局部可选任务,在tasks.json一级配置的 任务为全局任务,在二级tasks中配置的任务老猿称为局部可选任务,这个名称是老猿自定义。
4.2、全局任务和局部可选任务对比
经老猿全局任务和局部可选任务的对比:
- 全局任务和局部可选任务具有很多相同的配置元素,包括ype、command、isBackground、args、presentation、problemMatcher
- 全局任务比局部可选任务多了version、options和tasks这三个局部可选任务没有的配置项,而tasks就是局部可选任务的配置项
- 全局任务比局部可选任务少了label、group、runOptions、detail四个子配置项
- 全局任务的type只能是“process”和“shell”,局部可选任务在安装了C++组件的情况下,多了’“cppbuild”这个类型,因此全局任务中配置的任务在菜单“终端->运行生成任务…”中是看不到的,只能在“运行任务…”中看到该任务,如果安装其他语言相关组件估计还有其他类型
- 全局任务缺了label选项,因此选择任务时显示的command选项的内容,而局部可选任务显示的label对应的内容
- 在vscode的官方文档注释中,关于全局任务的problemMatcher有个注释说明:如果执行全局命令(例如,tasks没有定义局部可选任务)将使用全局任务配置的问题匹配器,一个tasks.json文件只能在全局问题匹配器属性或tasks属性中配置最多一个,不能同时包含两者。但经老猿测试这个说法不成立。
4.3、配置选项
全局任务和局部可选任务具有很多相同的配置元素如,包括type、command、isBackground、args、presentation、problemMatcher,这些选项的含义请参考老猿在CSDN的博文《Visual Studio Code中的任务配置文件tasks.json中的可选任务组tasks详解》的介绍.。
全局任务比局部可选任务多了version、options和tasks这三个局部可选任务没有的配置项,而tasks就是局部可选任务的配置项,因此下面介绍version和options这2个配置项:
- version指定task.json文件的版本号,目前为2.0.0;
- options:options的类型为CommandOptions,具体介绍请参考老猿在CSDN的博文《Visual Studio Code中tasks.json全局任务命令选项CommandOptions配置介绍》的介绍。
五、任务配置案例
下面是一个实际案例的具体配置内容:
{
"version": "2.0.0",
"type": "shell",
"command": "echo \"vscode当前相关变量值如下:\r\n workspaceFolder=${workspaceFolder}\r\n workspaceFolderBasename=${workspaceFolderBasename}\r\n file=${file}\r\n relativeFile=${relativeFile} \r\n fileBasename=${fileBasename} \r\n fileBasenameNoExtension=${fileBasenameNoExtension}\r\n fileDirname=${fileDirname} \r\n fileExtname=${fileExtname} \r\n cwd=${cwd}\r\n 相关变量内容如上。\r\n\" ",
"options":
{
"shell": {
"executable":"bash",
"args":["-c"]
}
},
"problemMatcher":[],
"tasks":
[
{
"type": "cppbuild",
"label": "buildHello1",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-Wall",
"-Wextra",
"-O0",
"${workspaceFolder}/hello.cpp",
"-o",
"${fileDirname}/$hello1"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译hello.cpp"
},
{
"type": "cppbuild",
"label": "buildHello2",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-Wall",
"-Wextra",
"-O0",
"${workspaceFolder}/hello.cpp",
"-o",
"${fileDirname}/$hello2"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译hello.cpp"
}
]
}
上面这个任务包含一个全局任务,该全局任务用来显示vscode预定义的一些系统变量的值,两个局部可选任务,都是编译hello.cpp,不过任务名和编译的目标文件名有区别。
通过这个案例,大家可以对全局任务和局部可选任务的配置有所感性认识,结合上面介绍的知识,就能掌握任务配置的方法。
六、小结
本文介绍了Visual Studio Code中的任务文件tasks.json的任务(包括全局任务和局部可选任务)配置各要素的配置含义和用途,并举例说明了具体任务配置的应用,有助于大家深入理解vscode的任务配置方法。
参考资料
- vscode用法;
- vscode中关于tasks.json的官方文档地址:https://code.visualstudio.com/docs/editor/tasks-appendix
- Variables Reference–Visual Studio Code supports variable
- Integrate with External Tools via Tasks
写博不易,敬请支持:
如果阅读本文于您有所获,敬请点赞、评论、收藏,谢谢大家的支持!
更多关于信创之国产浪潮电脑+统信UOS操作系统体验及VSCODE介绍的内容请参考专栏《国产信创之光》的其他文章。
关于老猿的付费专栏
- 付费专栏《https://blog.csdn.net/laoyuanpython/category_9607725.html 使用PyQt开发图形界面Python应用》专门介绍基于Python的PyQt图形界面开发基础教程,对应文章目录为《 https://blog.csdn.net/LaoYuanPython/article/details/107580932 使用PyQt开发图形界面Python应用专栏目录》;
- 付费专栏《https://blog.csdn.net/laoyuanpython/category_10232926.html moviepy音视频开发专栏 )详细介绍moviepy音视频剪辑合成处理的类相关方法及使用相关方法进行相关剪辑合成场景的处理,对应文章目录为《https://blog.csdn.net/LaoYuanPython/article/details/107574583 moviepy音视频开发专栏文章目录》;
- 付费专栏《https://blog.csdn.net/laoyuanpython/category_10581071.html OpenCV-Python初学者疑难问题集》为《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python图形图像处理 》的伴生专栏,是笔者对OpenCV-Python图形图像处理学习中遇到的一些问题个人感悟的整合,相关资料基本上都是老猿反复研究的成果,有助于OpenCV-Python初学者比较深入地理解OpenCV,对应文章目录为《https://blog.csdn.net/LaoYuanPython/article/details/109713407 OpenCV-Python初学者疑难问题集专栏目录 》
- 付费专栏《https://blog.csdn.net/laoyuanpython/category_10762553.html Python爬虫入门 》站在一个互联网前端开发小白的角度介绍爬虫开发应知应会内容,包括爬虫入门的基础知识,以及爬取CSDN文章信息、博主信息、给文章点赞、评论等实战内容。
前两个专栏都适合有一定Python基础但无相关知识的小白读者学习,第三个专栏请大家结合《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python图形图像处理 》的学习使用。
对于缺乏Python基础的同仁,可以通过老猿的免费专栏《https://blog.csdn.net/laoyuanpython/category_9831699.html 专栏:Python基础教程目录)从零开始学习Python。
如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。