Tyler Naes
Tyler Naes

Reputation: 31

How to add g++ from MinGW to PATH

I am using Visual Studio Code on Windows 10 and I am trying to build a program (called Bus from the source file Bus.cpp) with g++ from MinGW. I modified the tasks.json file using various methods that I have read from previous threads. I am providing screenshots of the tasks.json file and the results after building (ctrl + shift + b). I am also providing my questions below.

Method 1

Method 2

Method 3

Method 4

Question 1) I want to be able to build and create a program called "bus" using method 1. However, I have to replace "g++" with the directory path of the MinGW's g++ compiler (as shown in method 2). What do I have to do in order to just put "g++" instead of the path to MinGW's g++.exe?

Question 2) It looks like it compiled using methods 2 and 4 which provide the directory path to MinGW's g++ compiler but I do not see the built program called "bus" even though I used "-o bus" in the command. Why do I not see it?

EDIT TO SHOW CODES:

First Method tasks.json file:

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

First Method resulting output:

> Executing task: g++ -g bus.cpp -o bus <

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.
At line:1 char:1
+ g++ -g bus.cpp -o bus
+ ~~~
    + CategoryInfo          : ObjectNotFound: (g++:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

Second Method tasks.json file:

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

Second Method resulting output:

> Executing task: C:/MinGW/bin/g++.exe -g bus.cpp -o bus <

The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

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

Third Method resulting output:

    > Executing task: g++ -g bus.cpp -o bus <

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.
At line:1 char:1
+ g++ -g bus.cpp -o bus
+ ~~~
    + CategoryInfo          : ObjectNotFound: (g++:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

Fourth Method tasks.json file:

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

Fourth Method resulting output:

    > Executing task: C:/MinGW/bin/g++ -g bus.cpp <

The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

Upvotes: 2

Views: 18648

Answers (1)

maxap
maxap

Reputation: 100

To summarize, you want to add the mingW-gcc to your path.

Easiest way:

Start Menu -> Computer -> Right Click -> Properties -> Advanced System Settings (somewhere left) -> Environment Variables

Then add your directory to the Path variable after adding a semi colon to the end.

Upvotes: 2

Related Questions