【问题标题】:How to invoke cmd.exe /c cls from within VS Code tasks.json?如何从 VS Code tasks.json 中调用 cmd.exe /c cls?
【发布时间】:2021-10-05 16:00:20
【问题描述】:

我有一个简单的HelloWorld.cpp 文件,我想按照以下步骤运行它,每个步骤一个接一个地运行,如下所示。

  • 编译
  • 清除集成终端
  • 运行生成的可执行文件。

很遗憾,我未能设置第二步(清除控制台窗口)。正确的设置是什么?

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "COMPILE",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe:",
                "${fileBasenameNoExtension}.exe",
                "${file}",
                "/std:c++latest"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ]
        },
        {
            "label": "CLEAN",
            "command": "cmd.exe /c cls",
            "dependsOn": "COMPILE"
        },
        {
            "label": "RUN",
            "command": "${fileBasenameNoExtension}.exe",
            "dependsOn": "CLEAN",
            "options": {
                "cwd": "${fileDirname}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

【问题讨论】:

    标签: c++ visual-studio-code


    【解决方案1】:

    有一个带有workbench.action.terminal.clear的vscode设置,你会在那里找到相关设置, 或在文件 > 首选项 > 键盘快捷键中。搜索终端:清除并分配密钥

    【讨论】:

    • 我想扩展我的tasks.json 以仅使用一个快捷方式CTRL+SHIFT+B 调用这3 个步骤。
    【解决方案2】:

    啊哈。我找到了解决方案:将"type": "shell" 添加到CLEAN 步骤中,如下所示。

    {
        "type": "shell",
        "label": "CLEAN",
        "command": "cls",
        "dependsOn": "COMPILE"
    }
    

    完整的tasks.json

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "COMPILE",
                "command": "cl.exe",
                "args": [
                    "/Zi",
                    "/EHsc",
                    "/nologo",
                    "/Fe:",
                    "${fileBasenameNoExtension}.exe",
                    "${file}",
                    "/std:c++latest"
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$msCompile"
                ]
            },
            {
                "type": "shell",
                "label": "CLEAN",
                "command": "cls",
                "dependsOn": "COMPILE"
            },
            {
                "label": "RUN",
                "command": "${fileBasenameNoExtension}.exe",
                "dependsOn": "CLEAN",
                "options": {
                    "cwd": "${fileDirname}"
                },
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-10
      • 2019-10-17
      • 1970-01-01
      • 2018-12-22
      • 2021-11-25
      • 2020-08-25
      • 2018-09-09
      • 2016-05-22
      相关资源
      最近更新 更多