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

时间:2019-07-13 13:25:17

标签: c++ windows visual-studio-code mingw-w64

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

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

我已包含文件

helloworld.cpp:

#include <iostream>


using namespace std;

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


task.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:错误:helloworld.cpp:没有这样的文件或目录g ++。exe:   致命错误:没有输入文件编译终止。终点站   进程终止,退出代码:1


2 个答案:

答案 0 :(得分:0)

似乎编译器无法找到源文件,更新task.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}也很容易解释。

答案 1 :(得分:-1)

 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
        }
    }
]
}