VScode配置task和launch支持C++11

2023-05-16


title: VScode配置task和launch
date: 2022-12-12 20:57:26
cover:
categories: VisualCode
tags:

  • debug

VScode配置task和launch支持C++11

刚开始使用VScode一般都是使用默认的task和lunch配置去执行代码或者debug,一旦修改了相关目录或者改动一些参数,就会不停的提醒XXX文件不存在或者一些奇怪错误。今天就专门花时间研究了一下task和lunch怎么用

因为task和launch都是使用json编写,并且是用来启动编译器,所以需要一些 预备知识

  • 编译器相关参数
  • json语法

Task的配置

task就是你当前要执行的任务,如果不手动配置,vs会自动配置当前环境的编译任务

初次创建文件或者目录后,按下 F1键盘 会自动生成launch和task文件

默认task如下

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

以下是VS官方文档参数说明

  • 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: bash, cmd, or PowerShell). If process is specified, the command is interpreted as a process to execute.
  • 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.

在配置文件下使用 CTRL+空格键 触发候选项。

我们修改的主要由这几项

  • label
  • command
  • args

label

在这里插入图片描述

图中显示的名字就是定义的 label

command

command为执行任务所使用的命令,在linux下如果用gcc或者clang可以使用

which gcc
which clang

查看当前使用编译器的文件路径。比如我要使用brew安装的clang,可以修改为

"command": "/opt/homebrew/opt/llvm/bin/clang++"

更换为你要使用的编译器即可

args

args为当前编译器的参数,配合vs相关变量使用,这里列出一些常用的变量

  • ${userHome} - the path of the user’s home folder
  • ${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 upon the startup of VS Code
  • ${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

编译器的参数可以参考https://www.runoob.com/w3cnote/gcc-parameter-detail.html

"args": [
	"-g",
	"${file}",
	"-o",
   "${fileDirname}/temp/${fileBasenameNoExtension}", //生成的文件放在temp目录下
	"-std=c++17"	//支持c++17
],

如果要使用c++11特性,可以在这里添加相关参数

其它参数

其他参数参照VS官方文档按需配置,基本C/C++的配置太不需要改动其他操作。

Launch

根据上文自动生成的task.json launch文件已经生成了,如果没有生成的话在debug界面点击生成launch

默认生成的文件如下

{
    "configurations": [
        {
            "name": "C/C++: g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: g++ build active file"
        }
    ],
    "version": "2.0.0"
}

VS官方参考文档:https://code.visualstudio.com/docs/editor/debugging

这里我们要注意几个参数

  • program
  • MIMode
  • preLaunchTask

program

program参数表示可执行文件的位置,如果找不刀可执行文件,debug会报错。

这里一定注意 要和task中build任务生成目录一致

因为我的task中,输出的二进制文件在temp文件下,所以要修改program的位置

"program": "${fileDirname}/temp/${fileBasenameNoExtension}"

MIMode

这里指定要使用的调试程序

  • gdb
  • lldb

我使用的是llvm所以直接就写lldb即可

如果是gcc的话就是用gdb

preLaunchTask

这里指定的是launch前置任务,一般用于生成可调式的二进制文件

因为我们已经生成了一个task,我们直接调用前一个task即可。preLaunchTask的值与task的label的值相匹配

"preLaunchTask": "C/C++: g++ build active file"

现在开始调试你的程序吧

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VScode配置task和launch支持C++11 的相关文章

  • 有没有正确的方法来取消异步任务?

    我遇到了如何正确取消异步任务的问题 这是一些草稿 我的入口点运行两个异步任务 第一个任务做了一些 长时间 的工作 第二个任务取消了它 入口点 private static void Main var ctc new Cancellation
  • 从“最近的应用程序”启动应用程序和点击应用程序图标有什么区别

    我正在开发大型项目 因此有一些逻辑可以保存应用程序状态 然后在来自后台时打开正确的活动 片段 但无论如何 我发现如果用户浏览我的应用程序然后最小化它 android 在以下情况下会以不同的方式从后台打开它 用户点击应用程序图标 行为 当应用
  • 并发任务更新复杂对象 JavaFX - swingworker 等效项?

    我想在后台运行一个任务 更新视图中的中间结果 我正在尝试实现 MVC JavaFX 应用程序 任务在模型中定义 我想将部分结果发送给主要威胁 以便在视图中显示它们 我使用 updateValue 来执行此操作 另外 我在控制器中定义了对象属
  • 使用任务时出现意外的线程中止异常。为什么?

    我有一个在应用程序域中运行的控制台应用程序 应用程序域由自定义 Windows 服务启动 该应用程序使用父任务来启动多个有效的子任务 当计时器寻找新工作时 在任何给定时间都可能有许多父任务和子任务一起运行 所有父任务的句柄位于任务列表中 s
  • Gradle 插件从插件 jar 复制文件

    我正在创建我的第一个 gradle 插件 我正在尝试将文件从分发 jar 复制到我在项目中创建的目录中 尽管该文件存在于 jar 内 但我无法将其复制到目录中 这是我的任务代码 import org gradle api DefaultTa
  • 异步 ServiceController.WaitForStatus 如何执行?

    So ServiceController WaitForStatus https msdn microsoft com en us library system serviceprocess servicecontroller waitfo
  • 将参数传递到 Task.Factory.StartNew

    给出以下代码 string injectedString Read string out of HttpContext Task Factory StartNew gt MyClass myClass new MyClass myClass
  • Android 10.0 应用程序在 BOOT 上启动

    我们有一个 Android 应用程序 我们打算在手机启动期间启动 启动 通过在 Android 10 中尝试一些代码 我们意识到在 Android 8 0 之后无法在启动时启动应用程序 以前在 Android 6 中 这是可能的 即使在物理
  • 为什么 C# Parallel.Invoke 很慢?

    我正在这样做 private static void Main string args var dict1 new Dictionary
  • 如何处理Task.Run异常

    我在捕获异常时遇到问题Task Run这是通过更改代码解决的 如下所示 我想知道这两种方式处理异常之间的区别 In the Outside方法我无法捕获异常 但是在Inside方法我可以 void Outside try Task Run
  • eclipse 烦恼:调试和启动工具栏不可用

    我正在运行 Windows XP 和 Eclipse 4 2 2 Build id M20130204 1200 并且我丢失了调试和启动工具栏 我尝试过 Windows gt 重置透视 原始值 和窗口 gt 自定义透视 工具栏可见性和命令组
  • 基于任务的编程:#pragma omp task 与 #pragma omp parallel for

    考虑到 void saxpy worksharing float x float y float a int N pragma omp parallel for for int i 0 i lt N i y i y i a x i And
  • 在 UI 线程上创建并启动任务

    当在工作线程上调用的方法需要在 UI 线程上运行代码并等待其完成后再执行其他操作时 可以这样做 public int RunOnUi Func
  • 如何取消等待中的任务?

    我正在处理这些 Windows 8 WinRT 任务 并且尝试使用下面的方法取消任务 并且它在某种程度上有效 CancelNotification 方法确实被调用 这使您认为任务已被取消 但在后台任务仍在运行 然后在完成后 任务的状态始终为
  • Gradle创建多项目Jar

    因此 从 Gradle 和 Android Studio 诞生之初起 我就一直在使用它们 然而 我发现自己用头撞墙的次数有时远远超过了它的价值 我花了一天半的时间试图解决我目前的困境 在我工作的地方 我们使用很多共享库项目 这意味着与 Gr
  • 在午夜更新应用程序徽章,并提供以下选项:应用程序未启动或在后台,徽章数量可能会减少

    我正在阅读许多有关本地通知的内容以及它们如何帮助更新应用程序徽章编号 我想在午夜更新此徽章 并将其值设置为我在午夜之前无法知道的数字 因此 如果可能的话 我想在午夜启动一个功能来更新 加载一些数据 检查要显示的数字 并将其显示在徽章上 当然
  • 如何为新任务类型扩展 Gradle 任务的行为?

    我想为一些测试任务设置一些东西 更具体地说 我想添加一些环境变量和一些系统属性 也许还有一些其他内容 例如 依赖项 或 workingDir 与常规的Test任务我可以做到这一点 task test1 type Test dependsOn
  • 任务取消最佳实践

    假设我有一个处理器 其工作是将文件保留回磁盘 这是作为Task当观察一个BlockingCollection
  • 等待 AsyncMethod() 与等待等待 Task.Factory.StartNew(AsyncMethod)

    给出以下方法 public async Task
  • 如何启动异步任务对象

    我想开始收集Task同时处理对象并等待所有对象完成 下面的代码显示了我想要的行为 public class Program class TaskTest private Task createPauseTask int ms works w

随机推荐

  • 图像处理 - GLCM灰度共生矩阵如何计算

    因为最近要在OpenCV上试试各种图像分割算法的效果 xff0c 其中灰度共生算法没有办法直接调用库函数实现 xff0c 看了很多文章 xff0c 其中有一篇博文讲的很清楚 博主有提供原理和源码 xff0c 大家可以直接看原博 原文地址 x
  • SUSE12 Remote side unexpectedly closed network connection ,Connection reset by peer原因之一

    用工具新开ssh远程报错如下 xff1a 已连接的ssh中 xff0c telnet报错如下 同网段机器ssh命令报错如下 xff1a 在对比了同操作系统同文件的配置后 xff0c 排除了 etc hosts deny跟 etc hosts
  • xterm连接虚拟机里的ubuntu

    mobaxterm连接在虚拟机的abuntu 1 在Ubuntu上打开SSH服务 安装 openssh client 和 openssh server sudo apt get install openssh client sudo apt
  • 图解Linux命令之--fdisk命令

    fdisk命令 gt 磁盘分区管理工具 添加分区的流程 fdisk dev sda 选择要分区的硬盘 p 列出当前分区表 n 添加新分区 回车 选择开始的块地址 直接回车默认就可以了 43 2G 输入要添加分区的大小 43 200M xff
  • 安装docker-compose报ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we c

    在CentOS7中 xff0c 如果python版本为3 x xff0c 在安装docker compose时会报错 xff1a ERROR Cannot uninstall PyYAML It is a distutils install
  • PostgreSql | 数据库 |自定义函数的设计和实现

    前言 xff1a 数据库中的函数概念 和开发语言 xff0c Java xff0c PHP xff0c Python等等类似 xff0c 关系型数据库也是有函数的 xff0c 函数指的是动态的封装一部分特定功能的集合 例如 xff0c 查询
  • 元学习系列(一):Siamese Network(孪生网络)

    目前有一种说法认为 xff0c 深度学习模型在数据量较大的情况下才能取得较好的效果 xff0c 当数据量较少 xff0c 更偏向于使用传统的机器学习模型 想办法从深度学习的方向构建模型 xff0c 使得模型在数据量较小的情况下也能取得较好效
  • SpringBoot整合MybatisPlus时“注入失败”的问题记录

    问题情景 xff1a 最近将几个小的Demo整合在一起 xff0c 其中项目A使用Mybatis项目B使用Mybatis plus 在正常的修改完application yml xff0c pom文件后尝试启动项目 xff0c 启动失败 报
  • VasSonic之流式拦截

    VasSonic之流式拦截 VasSonic框架用到了流式拦截和增量更新技术 xff0c 下面只简单介绍流式拦截 xff0c 详细参考 xff1a https github com Tencent VasSonic wiki 一 xff09
  • 全文搜索引擎 Elasticsearch 入门

    全文搜索属于最常见的需求 xff0c 开源的 Elasticsearch xff08 以下简称 Elastic xff09 是目前全文搜索引擎的首选 它可以快速地储存 搜索和分析海量数据 维基百科 Stack Overflow Github
  • Ubuntu16.04 安装,更新与卸载Docker CE

    Ubuntu16 04 安装 xff0c 更新与卸载Docker CE 污污老师 关注 2017 11 14 23 40 字数 1019 阅读 3079评论 0喜欢 1 Docker CE 17 09 操作系统要求 xff1a 一个64位的
  • 零基础学习OpenGL(八)--立方体贴图、天空盒、环境映射

    立方体贴图 将多个纹理组合起来映射到一张纹理上的一种纹理类型 xff1a 立方体贴图 Cube Map 立方体贴图 xff1a 一个包含了6个2D纹理的纹理 xff0c 每个2D纹理都组成了立方体的一个面 xff1a 一个有纹理的立方体 之
  • Centos7下VNC离线安装(个人纪录)

    Centos7下VNC离线安装 个人纪录 1 官网下载rpm包 下载地址 2 执行安装命令 xff1a rpm Uvh tigervnc server 1 8 0 17 el7 x86 64 rpm 3 检查安装情况 xff1a rpm q
  • thinclient_drives

    ubuntu上安装xrdp搭建远程桌面 xff0c 后面远程桌面是可以了 xff0c 但是用户目录下生出了一个thinclient drives文件夹 xff0c 无论是不是root都不能删除 xff0c 如果你有强迫症 xff0c 你就感
  • 解决虚拟机下的ubuntu不能上网的问题

    解决虚拟机下的ubuntu不能上网的问题 2017年09月25日 19 31 39 ray7777777777 阅读数 xff1a 3676 1 打开虚拟机VM 2 右键ubuntu 设置 网络适配器 选中NAT模式 xff1a 用于共享主
  • 云原生|kubernetes|rancher-2.6.4安装部署简明手册

    前言 rancher是一个比较特殊的开源的kubernetes管理工具 xff0c 特殊在它是一个名称为k3s的简单kubernetes集群 xff0c 而该集群是在kubernetes集群内的 rancher还可以在一个裸的仅具有dock
  • Arch安装TIM并解决无法输入中文的常见问题

    花了两个小时的时间解决linux tim无法输入中文的问题 xff0c 最后直接重装系统对比前后配置解决问题 我觉得这种事情应该是十分钟就能搞定的 xff0c 突然想起高中政治老师之前说过的一个问题 一个人的汽车坏了 xff0c 他去修车
  • Arch使用vs code编译调试C/C++

    Arch使用vs code编译调试C C 43 43 Windows平台下无脑下一步编译器就安装好了 xff0c 转到linux平台下基本没有无脑下一步这种操作 xff0c 这个时候就需要手动配置相关程序IDE 43 GCC xff0c 去
  • vim使用coc 补全代码

    coc项目地址 https github com neoclide coc nvim coc安装 使用Vim Plugin安装coc vim 在vimrc中添加插件 34 Use release branch recommend 推荐使用
  • VScode配置task和launch支持C++11

    title VScode配置task和launch date 2022 12 12 20 57 26 cover categories VisualCode tags debug VScode配置task和launch支持C 43 43 1