简体   繁体   English

在 Visual Studio Code 中构建 C++ 程序

[英]Building a C++ Program in Visual Studio Code

To my knowledge, I have followed all of the steps to build a C++ program in Visual Studio Code on Windows 10. I have gcc-7.1.0-64 installed under C:/MinGW , the C/C++ extension installed in VS Code, and have configured a build task for my HelloWorld.cpp .据我所知,我已按照所有步骤在 Windows 10 上的 Visual Studio Code 中构建 C++ 程序。我在C:/MinGW下安装了gcc-7.1.0-64 ,VS Code 中安装了C/C++扩展,并为我的HelloWorld.cpp配置了一个构建任务。

The issue:问题:

When I try to build the program by opening the Command Palette and then typing Tasks: Run Build Task , It displays the error:当我尝试通过打开命令面板然后键入Tasks: Run Build Task来构建程序时,它显示错误:

No Build Task found.未找到构建任务。 Press 'Configure Build Task' to define one.按“配置构建任务”来定义一个。

despite the fact that I have already done exactly that.尽管我已经做到了这一点。 I am probably missing something simple, but no tutorial or documentation I could find anywhere explains how to make this work.我可能遗漏了一些简单的东西,但我在任何地方都找不到任何教程或文档来解释如何进行这项工作。 Any help is appreciated.任何帮助表示赞赏。

HelloWorld.cpp HelloWorld.cpp

#include <iostream>

using namespace std;

int main() {
  std::cout << "Hello world\n";
}

c_cpp_properties.json (irrelevant Mac/Linux setup omitted from snippet) c_cpp_properties.json (片段中省略了不相关的 Mac/Linux 设置)

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/MinGW/include/c++/7.1.0/*"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "C:/MinGW/include/c++/7.1.0/*"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 2
}

tasks.json任务文件

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "taskName": "HelloWorld.cpp",
      "command": "c++",
      "args": ["-g", "HelloWorld.cpp"],
      "type": "shell"
    }
  ]
}

Thanks to @RobLourens's comment for the answer.感谢@RobLourens 对答案的评论。

The easiest way for tasks 2.0 is, run "Configure default build task", which will let you pick the build command, and set the group property for you.任务 2.0 最简单的方法是运行“配置默认构建任务”,这将让您选择构建命令,并为您设置组属性。

I soon realized that it adds a line under the "group" section of the specified build task in tasks.json : ( may be added manually to circumvent the formal method )我很快意识到它在tasks.json指定构建任务的"group"部分下添加了一行:(可能会手动添加以规避形式方法

"isDefault": true,

只需在您的任务中添加"isBuildCommand": true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM