# For C++

## Install MinGW compiler

First you need to install a C++ compiler. For Windows the easiest option is MinGW. Goto [http://www.mingw.org/](http://www.mingw.org/) and select "Downloads".

Wait for SourceForge to load and select the big green button that states "Download Latest Version":

![Getting the Latest Version](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY4vd9uEKXTKAv8g%2Fmingw_downloads.png?generation=1535538771663689\&alt=media)

Install the package and wait for the package selector to load. Now select the "mingw32-base" and "mingw32-gcc-g++" packages and mark them for installation. From the menu select `Installation => Apply Changes` to install the packages. The end result should be:

![Package Selection](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY4xhTWm1ZJ23Vl7%2Fmingw_package_selection.png?generation=1535538771625990\&alt=media)

Open a powershell and make sure that the `g++ --version` command gives a comparable output to the one given below:

```
C:\Users\nicod\> g++ --version
g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

## Visual Studio Code Setup

Open Visual Studio Code and select the Extensions panel. Next install the C/C++ extension.

![C/C++ Extension](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY4ztqWICZWXamUa%2Fc_cpp_extension.png?generation=1535538771683008\&alt=media)

## Creating a C++ project

Launch Visual Studio Code and open a folder (this will be the root project folder). Create a new file called `main.cpp` inside this folder and place the hello world code from below inside. Make sure to save it.

```cpp
#include <iostream>

int main() {
    std::cout << "Hello from VScode" << std::endl;
    return 0;
}
```

The end result should be this:

![New Project](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY50KXCS2_ef-GLy%2Fstart_project.png?generation=1535538773266611\&alt=media)

Next we need to create a task to build and run our application in a single step.

Hit `CTRL - SHIFT - P` to open the command menu and select `Tasks: Configure Task`.

![Configure Task](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY52PsOF1zDozbXr%2Fconfigure_task.png?generation=1535538773208771\&alt=media)

Select `Create tasks.json file from template`.

![Create tasks.json](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY547AkIb39r9lnt%2Fcreate_tasks.png?generation=1535538772675466\&alt=media)

Choose the `Others` option.

![Others Option](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY56IDIXPgvskMFq%2Fothers_option.png?generation=1535538771616782\&alt=media)

Replace the standard json config with the one from below.

```javascript
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build application",
            "type": "shell",
            "command": "g++",
            "args": [
                "-Wall", "--std=c++11",
                "main.cpp",
                "-o", "app.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "run application",
            "type": "shell",
            "command": "${workspaceFolder}/app.exe",
            "args": [],
            "presentation": {
                "reveal": "always"
            },
            "dependsOn": [
                "build application"
            ],
            "problemMatcher": []
        }
    ]
}
```

Now you can select `Tasks => Run Build Tasks` or hit `CTRL - SHIFT - B` to build your application.

You can also select `Tasks => Run Task` and select `run application` to both build and run the application.

![Run Application](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY580raJu9jkhqq-%2Frun_application.png?generation=1535538772786676\&alt=media)

If you wish you can also define a shortcut key to run the task. Goto `File => Preferences => Keyboard Shortcuts` and click the `keybindings.json` tag to open the file.

![Opening keybindings json file](https://981283853-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEK-j-hxx7zCDNXfjra%2F-LL4EVa4libM96oMBXqa%2F-LL4EY5AvoPiepQr2-b8%2Fkeybindings_shortcut.png?generation=1535538772226935\&alt=media)

Add a shortcut entry between the square brackets as shown below. Make sure to edit the file shown on the right-hand side if you get the split view (left is default example):

```javascript
// Place your key bindings in this file to overwrite the defaults
[{
    "key": "f5",
    "command": "workbench.action.tasks.runTask",
    "args": "run application"
}
]
```

You can choose the key yourself and the `args` is the label of the run application task.

## Adding Extra Classes

To compile extra classes you need to add the cpp implementation file to your compilation task. For example for a file called `robot.cpp` you need to add the file to the `args` list of your `build application` task:

```javascript
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build application",
            "type": "shell",
            "command": "g++",
            "args": [
                "-Wall", "--std=c++11",
                "main.cpp", "robot.cpp",
                "-o", "app.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "run application",
            "type": "shell",
            "command": "${workspaceFolder}/app.exe",
            "args": [],
            "presentation": {
                "reveal": "always"
            },
            "dependsOn": [
                "build application"
            ],
            "problemMatcher": []
        }
    ]
}
```
