Search code examples
c++windowsvisual-studio-codemingw-w64

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



Solution

  • Seems like compiler isn't able to locate the source files , update the tasks.json to compile programs with complete path ,

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

    here ${file} gives complete path of the file with extension(.cpp) , ${workspaceFolder} and ${fileBasenameNoExtension} are pretty much self-explanatory too .