VSCODE(七)创建C++编译任务

任务是与工作区相关的,可用于运行程序和脚本,这次我们来看看C++编译任务是如何编写的。

一、任务是如何帮助我们完成C/C++开发的?

C/C++开发需要:

  • 一些辅助编辑、校验的功能
  • 代码的编译
  • 打包
  • 测试
  • 部署

对于第一项,C/C++开发时都会下载C/C++插件,帮助我们完成编辑、编译、调试功能。智能感知、调试和代码浏览(Go to definition、Find All Reference)。我们知道task借助终端可执行一切终端任务,我们程序进行“编译、打包、测试和部署”也是通过终端完成,所以vscode完全可以进行C++开发。对于C/C++而言,编译由g++、gdb完成,编译打包测试部署由Makefile和CMake等自动化编译工具完成。

二、配置默认编译任务?

假设我们要编译的源文件如下:

#include <iostream>
int main()
{
    std::cout<<"Hello world"<<std::endl;

    return 0;
}

按F1选择Tasks:Configure Default Build Task,字面意思是任务:配置一个默认编译任务。
在这里插入图片描述接着会继续提示你选择默认编译任务,根据你编译的源文件类型选择合适的编译器,由于我是C++程序,因此我们选择compiler:/usr/bin/g++这一栏:
在这里插入图片描述接着VSCODE自动帮你在.vocode下新建一个tasks.json(注意这里task用了复数,也就是这个文件可以执行多个任务),其内容如下:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++ build active file",
			"command": "/bin/g++",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: /bin/g++"
		}
	]
}

至此,任务配置已经完成,点击你要编译的源文件为,按下ctrl+shift+b即可运行编译任务,这个默认任务是编译一个当前正在编辑的源文件,目标程序名称是源文件去掉后缀。

看懂这些JSON文件并不困难,这里说一下一些比较重要的配置键值对:

  • "version" : "2.0.0" 这个tasks.json版本号是2.0.0
  • tasks 内容(中括号里面的,数据类型:数组)

tasks内容任务数组,不同任务使用不同逗号隔开。如下:

tasks:[
	{
		任务一
	}{
		任务二
	}
]

这里只用了只有一个任务:

  • type 一般是shell或者process。对于C++程序cppbuild等于shell的别名,为了更加可读?
  • label 任务标签,用于识别不同的任务
  • command 终端中执行的命令,这里是g++编译器
  • args 数组 表示g++选项,如果忘记了回去复习一下g++内容
  • options 额外选项。这里的选项是"cwd",当前脚本或者程序执行的地方,如果不给定默认是工作空间的根目录。
  • problemMatcher 不清楚是干啥的
  • group 说明这个任务归属,有两个组,一个是test组,另一个是build组。还可以配置是否是该组的默认任务
  • detail 详细说明

我们知道在.vscode中的配置是可以移植的,如果你想让你的配置更加通用,最好使用一些VSCODE预定义的一些变量,见附录(二)。

切换到需要编译的源文件并按下ctrl+shift+b,即可编译。
在这里插入图片描述

三、定制化编译任务

编译如下结构的cpp程序,应该如何书写tasks.json?

.
├── include
│   ├── add.h
│   └── minus.h
├── main
├── main.cpp
└── src
    ├── add.cpp
    └── minus.cpp

2 directories, 6 files

工程主要由一个mian.cpp和两个分属不同文件夹的源文件和头文件,显然不能直接套用默认模板。使用以下tasks.json内容即可进行编译:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++ build active file",
			"command": "/bin/g++",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${workspaceFolder}/src/*.cpp",
				"${workspaceFolder}/*cpp",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: /bin/g++"
		}
	]
}

附录1:

名字
labelThe task’s label used in the user interface.
typeThe 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
commandThe actual command to execute.
windowsAny Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
groupDefines 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.
presentationDefines 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.
optionsOverride 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.
runOptionsDefines 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

[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

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VSCode中进行C++多文件编译可以按照以下步骤进行操作: 1. 确保您已安装C++扩展:在VSCode中,点击左侧的扩展图标(四个方块),并搜索并安装由Microsoft提供的"C++"扩展。 2. 在项目文件夹中创建源文件:在VSCode中,打开您的C++项目文件夹,并在其中创建您的C++源文件。可以使用`.cpp`扩展名来保存源文件。 3. 创建一个构建任务:在VSCode中,使用快捷键`Ctrl + Shift + B`(Windows/Linux)或者`Cmd + Shift + B`(Mac)来打开构建任务菜单。如果是第一次使用,VSCode会提示您选择一个构建系统。选择"C++"来生成一个默认的构建任务。 4. 配置构建任务VSCode会在项目文件夹中创建一个`tasks.json`文件,您可以在其中配置构建任务。根据您的项目需要,可以配置多个构建任务。例如,如果您有多个源文件需要编译,您可以在`tasks.json`文件中指定它们的编译命令和依赖关系。 5. 运行构建任务:在VSCode中,使用快捷键`Ctrl + Shift + B`(Windows/Linux)或者`Cmd + Shift + B`(Mac)选择您要运行的构建任务。 6. 查看输出信息:构建任务成功运行后,将在VSCode的终端中显示构建输出的信息。您可以查看编译错误、警告或生成的可执行文件等信息。 请注意,具体的步骤和配置可能因您的项目和开发环境而有所不同。您可能需要根据实际情况自定义构建任务编译选项。 希望这些步骤能帮助您在VSCode中成功进行C++多文件编译。如有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值