【问题标题】:How to build and run C++ code in Visual Studio Code?如何在 Visual Studio Code 中构建和运行 C++ 代码?
【发布时间】:2016-10-12 12:20:22
【问题描述】:

我有一个 tasks.json 脚本,目前正在编译代码

{
    "version": "0.1.0",
    "command": "gcc",
    "isShellCommand": true,
    "args": ["-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"],
    "echoCommand": true,
    "showOutput": "always",
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

这很好用,但是当我想运行文件时,我必须从命令行运行 exe。是否也可以在任务中执行此操作?那么,如果它成功完成构建,它会运行不同的任务吗?

【问题讨论】:

    标签: c++ visual-studio-code


    【解决方案1】:

    如果其他人像我一样在搜索时遇到此问题,您现在可以将 launch.json 中的属性 preLaunchTask 设置为构建任务的 name 属性,它将在您启动之前运行。

    举例

    "name": "Debug (gdb) Launch", "preLaunchTask": "Build All",

    在启动您的程序之前,将在您的tasks.json 中运行"name": "Builld All"

    您可以在 Debugging in Visual Code 文档页面上阅读相关信息。

    【讨论】:

    • 找了好几个小时了,这个应该在最上面,谢谢
    【解决方案2】:

    您可以在 Visual Studio Code 中配置多个任务,其中一个允许您构建可执行文件,另一个将运行您的可执行文件。

    您还可以选择查看 Visual Studio Code 的“运行模式”(请参阅​​ here)。如果您使用“运行模式”,您应该能够配置 Visual Studio Code 来构建您的可执行文件,然后启动它。

    我对“运行模式”不是很熟悉,因此我将详细介绍如何定义多个任务以实现类似的结果。


    免责声明:Visual Studio Code 不支持使用不同 shell 命令的任务(请参阅here)。

    没错。在当前状态下,Visual Studio Code 没有“原生”支持来定义使用不同 shell 命令的任务。

    免责声明:Visual Studio Code 的任务输出窗格不允许您以交互方式将输入传递给程序。

    如果您的程序依赖于用户输入(例如,来自标准输入),您最好不要使用 Visual Studio Code运行您的可执行文件


    基本上,我们需要做的是定义两个任务,其中一个是构建任务,另一个是我们的启动任务。

    由于 Visual Studio Code 不能很好地支持定义多个任务,每个任务都使用不同的 shell 命令,我们需要将 tasks.json"command" 属性更改为 cmd(或 sh ,如果在 Linux/macOS 上)。我们还需要将"args" 属性设置为[/C](如果在Linux/macOS 上为[-c])。

    我们这样做的原因是,我们希望将要定义的每个任务作为参数传递给新的 shell 实例。

    下一步,是定义我们的构建和启动任务。当我们这样做时,我们需要确保将要运行的命令作为任务参数放置。例如:

    {
        "taskName": "build",
        "args": ["gcc", "-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"]
    }
    

    最后,我们要做的是将"isBuildCommand" 属性添加到我们的构建任务中(并确保它是true),并将"isTestCommand" 属性添加到我们的启动任务中(同样,确保它是true)。

    在所有这些之后,我们的tasks.json 文件可能看起来像这样:

    {
        "version": "0.1.0",
        "command": "cmd",
        "args": ["/C"],
        "isShellCommand": true,
        "showOutput": "always",
        "suppressTaskName": true,
        "tasks": [
            {
                "taskName": "build",
                "args": ["gcc", "-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"],
                "isBuildCommand": true
            },
            {
                "taskName": "run",
                "args": ["${relativeFile}.exe"],
                "isTestCommand": true
            }
        ]
    }
    

    注意:如果将每个任务参数放在 args 数组中自己的字符串中不起作用,您也可以尝试将所有参数放在 args 中的单个字符串中大批。示例:

    ["gcc -Wall ${relativeFile} -o ${relativeFile}.exe -pedantic"]
    

    注意:如果您希望能够通过键盘快捷键调用您的任务,您可以使用 "workbench.action.tasks.build""workbench.action.tasks.test" 编辑器命令。

    如果您需要将键绑定到这些命令的示例,以下是我如何将它们映射到我的 keybindings.json 文件中的示例:

    [
        {
            "key": "f6",
            "command": "workbench.action.tasks.build"
        },
        {
            "key": "f7",
            "command": "workbench.action.tasks.test"
        }
    }
    

    编辑:您可能只需要为测试任务定义一个键盘快捷键,因为构建任务可能已经定义了一个。在您花时间定义不同的键盘快捷键之前,请检查here

    【讨论】:

      【解决方案3】:

      你可以为构建创建一个任务,作为它的参数你可以传递命令来运行。例如,我用来编译和运行 c++ 的任务如下所示。

      {
      
      "version": "2.0.0",
      "tasks": [
          {
              "label": "g++ build and run",
              "type": "shell",
              "command": "g++",
              "args": [
                  "-g",
                  "-o",
                  "out.exe",
                  "\"${file}\"",
                  "&&",
                  "./out.exe",
                  "<",
                  "input.in",
                  ">",
                  "output.out"
              ],
              "group": {
                  "kind": "build",
                  "isDefault": true
              }
          }
      ]
      }
      

      在上述任务中,我将源文件(文件名可以是此处的任何人,因为使用了${file})编译为out.exe,然后运行out.exe,同时从input.in 获取输入并输出输出到output.out

      【讨论】:

      • 非常感谢!但是 && 抛出一个错误,我不得不使用 ;在PowerShell中。并删除 ${file} 周围的多余引号。
      猜你喜欢
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 2021-05-26
      • 2021-06-01
      • 1970-01-01
      相关资源
      最近更新 更多