如何在Visual Studio Code中构建和运行C ++代码? [英] How to build and run C++ code in Visual Studio Code?

查看:53
本文介绍了如何在Visual Studio Code中构建和运行C ++代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个task.json脚本,该脚本当前正在编译代码

I have a tasks.json script that currently compiles the code

{
    "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.是否也可以在任务中执行此操作?因此,如果成功完成构建,那么它将运行其他任务?

This works fine, but when I want to run the file I have to run the exe from command line. Is it possible to do this in the task as well? So if it finishes building succesfully it then runs a different task?

推荐答案

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

You can configure multiple tasks in Visual Studio Code, one of which will allow you to build your executable, and the other will run your executable.

(可选)您还可以查看Visual Studio代码的运行模式"(请参见这里).如果使用运行模式",则应该能够配置Visual Studio Code来生成可执行文件,然后启动它.

Optionally, you could also look into Visual Studio Code's "Run Mode" (see here). If you use "Run Mode", you should be able to configure Visual Studio Code to build your executable, and then launch it.

我对运行模式"不是很熟悉,因此我将详细介绍如何定义多个任务以达到相似的结果.

I'm not extremely familiar with "Run Mode", thus I will detail how to define multiple tasks to achieve a similar result.

是的.在当前状态下,Visual Studio Code不支持使用不同的Shell命令定义任务的本机"支持.

That's right. At its current state, Visual Studio Code doesn't have "native" support for defining tasks that use different shell commands.

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

If your program relies on user-input (for example, from stdin), you're probably better off not using Visual Studio Code to run your executable.

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

Basically, what we'll need to do, is define two tasks, one of which will be a build task, the other will be our launch task.

由于Visual Studio Code对定义多个任务(每个任务使用不同的Shell命令)没有强大的支持,因此我们需要更改 tasks.json "command"属性设置为 cmd (如果是Linux/macOS,则为 sh ).我们还需要将"args" 属性设置为 [/C] (如果在Linux/macOS上为 [-c] ).

Seeing as Visual Studio Code doesn't have great support for defining multiple tasks that each use different shell commands, we'll need to change our tasks.json's "command" property to cmd (or sh, if on Linux/macOS). We'll also need to set the "args" property to [/C] ([-c] if on Linux/macOS).

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

The reason behind us doing this, is because we want each of the tasks we're about to define, to be passed as arguments to a new shell instance.

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

The next step, is to define our build and launch tasks. When we do so, we'll need to make sure we place the command we want to run, as a task argument. For example:

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

最后,我们要做的是将"isBuildCommand" 属性添加到我们的构建任务中(并确保其为 true ),并添加<我们的启动任务的code>"isTestCommand" 属性(并再次确保它为 true ).

Finally, what we'll do, is add the "isBuildCommand" property to our build task (and make sure it's true), as well as add the "isTestCommand" property to our launch task (and, again, make sure it's true).

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

After all of that, our tasks.json file could look something like this:

{
    "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 数组中.示例:

Note: If placing each task argument in their own string within the args array doesn't work, you can also try placing all of the arguments in a single string within the args array. Example:

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

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

Note: If you would like to be able to invoke your task(s) via keyboard shortcuts, you have the "workbench.action.tasks.build" and "workbench.action.tasks.test" editor commands at your disposal.

如果您需要将键绑定到这些命令的示例,以下是我如何在我的

If you need an example of binding keys to those commands, here's an example of how I have them mapped in my keybindings.json file:

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

编辑:您可能只需要为测试任务定义一个键盘快捷键,因为构建任务可能已经定义了一个快捷键.在花费时间定义其他键盘快捷键之前,请在此处中进行检查.

Edit: You probably only need to define a keyboard shortcut for the test task, as the build task probably already has one defined. Check here before you take the time to define a different keyboard shortcut.

这篇关于如何在Visual Studio Code中构建和运行C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆