# 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/> and select "Downloads".

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

![Getting the Latest Version](/files/-LL4EY4vd9uEKXTKAv8g)

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](/files/-LL4EY4xhTWm1ZJ23Vl7)

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](/files/-LL4EY4ztqWICZWXamUa)

## 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](/files/-LL4EY50KXCS2_ef-GLy)

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](/files/-LL4EY52PsOF1zDozbXr)

Select `Create tasks.json file from template`.

![Create tasks.json](/files/-LL4EY547AkIb39r9lnt)

Choose the `Others` option.

![Others Option](/files/-LL4EY56IDIXPgvskMFq)

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](/files/-LL4EY580raJu9jkhqq-)

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](/files/-LL4EY5AvoPiepQr2-b8)

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": []
        }
    ]
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vives.gitbook.io/software-installation-guide/visual-studio-code/for_cpp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
