首页 > 解决方案 > My C++ program won't compile on Visual Studio code

问题描述

I'm attempting to use Visual Studio Code for the first time and my C++ won't compile.

I have already added mingw's bin and bash.exe from MSYS2 to my PATH. All of my code is in the same directory and straight from microsoft's guide https://code.visualstudio.com/docs/cpp/config-mingw (I did change the paths to mine). All of my files are also in the same directory.

I've included the file

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

The file wouldn't build and I am continuously the same error message:

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


标签: c++windowsvisual-studio-codemingw-w64

解决方案


似乎编译器无法找到源文件,更新 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}几乎是不言自明的。


推荐阅读