如何建立和Visual Studio代码运行C ++代码?

问题描述 投票:3回答:3

我目前编译代码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
3个回答
7
投票

您可以配置在Visual Studio代码多项任务,其中之一将允许你建立你的可执行文件,和其他将运行可执行文件。

另外,您还可以看看到Visual Studio代码的“运行模式”(见here)。如果使用“运行模式”,你应该能够配置Visual Studio代码来构建你的可执行文件,然后启动它。

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


Disclaimer: Visual Studio Code does not support tasks that use different shell commands (see here).

那就对了。在目前的状态下,Visual Studio代码没有定义使用不同的shell命令的任务“原生”的支持。

Disclaimer: Visual Studio Code's task-output pane will not allow you to pass input to your program interactively.

如果你的程序依赖于用户输入(例如,从标准输入),你可能不关闭使用Visual Studio代码运行可执行更好。


基本上,我们需要做的,就是定义两个任务,其中之一将是一个构建任务,其他的将是我们的发射任务。

眼见为Visual Studio代码没有定义,每个使用不同的shell命令多任务的大力支持,我们需要改变我们的tasks.json"command"属性cmd(或sh,如果在Linux / Mac系统)。我们也将(如果在Linux / MacOS的"args")需要设置[/C]属性[-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你需要定义不同的键盘快捷键的时间之前。


1
投票

如果任何人遇到这种搜索像我一样的时候,你现在可以设置属性preLaunchTasklaunch.json到您的构建任务的name财产,它会在您启动之前运行。

例如

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

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

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


0
投票

您可以为构建创建任务,并为它的参数,你可以通过命令来运行。作为一个例子我使用编译和运行的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

© www.soinside.com 2019 - 2024. All rights reserved.