Search code examples
c++buildvisual-studio-codemingwmingw-w64

I have a build error with MinGW and VS Code "g++ not recognized as a cmdlet..."


I am trying to set up build and run a c++ file in VS Code 2019. I am having build errors after editing the tasks.json file. The environment variable is set to g++ as it should be. So far, I have been following this tutorial.

I attempted changing "command" to"C:\MinGW\bin\g++.exe" as recommended in a question thread on GitHub. However, because my c++ file is not in this file path, the program was not able to find it when I built the code. This is what the "command" portion of the tasks.json file should look like:

"label": "build calculator adventure",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-o",
                "Calculator-Adventure",
                "Calculator Adventure.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }

The "Calculator-Adventure" part is my filename. The expected output is for the code to build and create a .exe file for my code, as stated in the tutorial and said in the VS Code Docs.

However, it currently outputs the following into the terminal:

> Executing task: ‪‪g++ -g Calculator Adventure.cpp -o Calculator-Adventure <

g++ : The term 'g++' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The terminal process terminated with exit code: 1"

Solution

  • OK, I finally figured it out. What worked for me was adding the file path to the git bash shell (C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git) to the System Environment Variables in the Control Panel (how to do that here). Make sure you also have the file path to the MinGW bin folder added to the Environment Variables as well (32bit installer: C:\MinGW\bin) (64bit installer: C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin) Then, restart VS Code and build (Ctrl+Shift+B) again.

    Here's my final code for the .json files:

    c_cpp_properties.json:

        "configurations": [
            {
                "name": "Win32",
                "includePath": [
                    "${workspaceFolder}/**"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "_UNICODE"
                ],
                "windowsSdkVersion": "10.0.17763.0",
                "compilerPath": "C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
                "cStandard": "c11",
                "cppStandard": "c++17",
                "intelliSenseMode": "gcc-x64",
                "browse": {
                    "path": [
                        "${workspaceFolder}"
                    ],
                    "limitSymbolsToIncludedHeaders": true,
                    "databaseFilename": ""
                }
            }
        ],
        "version": 4
    }
    

    tasks.json:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build calculator adventure",
                "type": "shell",
                "command": "g++",
                "args": [
                    "-g",
                    "-o",
                    "Calculator-Adventure",
                    "Calculator Adventure.cpp"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

    For more information, check out this page. It is a really detailed step-by-step guide for using the MinGW compiler for C++ in VS Code (read it carefully). If you have any other trouble, take a look at this tutorial (same tutorial linked in the question). Hope this helps!

    Note: in the docs page I linked, they use the 64bit version of MinGW. It should still work with the 32bit version though. Thanks, to @drescherjm for posting the VS Code Docs!