我的 C++ 程序无法在 Visual Studio 代码上编译

My C++ program won't compile on Visual Studio code

我是第一次尝试使用 Visual Studio 代码,但我的 C++ 无法编译。

我已经将 mingw 的 bin 和 bash.exe 从 MSYS2 添加到我的 PATH 中。我的所有代码都在同一个目录中,直接来自微软的指南 https://code.visualstudio.com/docs/cpp/config-mingw(我确实更改了我的路径)。我的所有文件也在同一个目录中。

我已经添加了文件

helloworld.cpp:

#include <iostream>


using namespace std;

int main()
{
   cout << "Hello World";
}


tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-o",
                "helloworld",
                "helloworld.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}


c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "compilerPath": "C:\Mingw-w64\mingw32\bin\g++.exe",
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}


launch.json:

{
    "version": "0.2.0",
     "configurations": [
         {
             "name": "(gdb) Launch",
             "type": "cppdbg",
             "request": "launch",
             "program": "${workspaceFolder}/helloworld.exe",
             "args": [],
             "stopAtEntry": true,
             "cwd": "${workspaceFolder}",
             "environment": [],
             "externalConsole": false,
             "MIMode": "gdb",
             "miDebuggerPath": "C:\Mingw-w64\mingw32\bin\gdb.exe",
             "setupCommands": [
                 {
                     "description": "Enable pretty-printing for gdb",
                     "text": "-enable-pretty-printing",
                     "ignoreFailures": true
                 }
             ]
         }
     ]
 }

文件无法构建,我一直收到相同的错误消息:

g++.exe: error: helloworld.cpp: No such file or directory g++.exe: fatal error: no input files compilation terminated. The terminal process terminated with exit code: 1


似乎编译器无法找到源文件,更新 tasks.json 以使用完整路径编译程序,

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",//Just
                "-o",//edit
                "${workspaceFolder}\${fileBasenameNoExtension}"//these
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

此处 ${file} 给出扩展名为 (.cpp) 的文件的完整路径,${workspaceFolder}${fileBasenameNoExtension} 也差不多 self-explanatory。

 That eventually worked after I exited the shell and re-opened it

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "label": "echo",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g", "main.cpp"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]
}