# Get the GNU compiler on Windows 

If you're a hacker running Windows, you don't need to resort to proprietary applications to compile code.
With the [Minimalist GNU for Window(http://mingw.org) project, you can download and install ``gcc`` on your Windows computer, along with several other essential GNU components to enable [GNU Autotools](https://opensource.com/article/19/7/introduction-gnu-autotools) process.

## Installing MinGW

The easiest way to install MinGW is through mingw-get, a GUI application that helps you select the components you want to install and keep them up to date.
To run it, download ``mingw-get-setup.exe`` from the project's host [osdn.net/projects/mingw/releases](https://osdn.net/projects/mingw/releases/).
Choose to install it as you would with any EXE file, clicking through the install wizard to completion.

![Installing mingw-get](mingw-install.jpg)

## Installing GCC on Windows

All you've done so far is install an installer, or more accurately a dedicated *package manager* called mingw-get.
Now you must launch mingw-get to select which applications from the MinGW project you want to install on your computer. 

First, select mingw-get from your application menu to launch it.

![Installing GCC](mingw-packages.jpg)

To install the GNU C Compiler, click the listing for GCC and G++.
This marks the GNU C and C++ compiler for installation.
To complete the process, select **Apply Changes** from the **Installation** menu in the top left corner of the mingw-get window.

Once you have GCC installed, you can run it from [Powershell](https://opensource.com/article/19/8/variables-powershell) using the full path of its location:

```
PS> C:\MinGW\bin\gcc.exe --version
gcc.exe (MinGW.org GCC Build-x) x.y.z
Copyright (C) 2019 Free Software Foundation, Inc.
```

## Running Bash on Windows

While it calls itself "minimalist", MinGW optionally provides MSYS, a Bourne Shell command-line interpreter. 
It's an alternative to Microsoft's ``cmd.exe`` and Powershell, and it defaults to Bash.
Aside from being one of the (justifiably) most popular shells, Bash is useful when porting open source applications to the Windows platform because many open source projects assume a [POSIX](https://opensource.com/article/19/7/what-posix-richard-stallman-explains) environment.

You can install MSYS from the mingw-get GUI or from within Powershell:

```
PS> mingw-get install msys
```

To try out Bash, launch it using its full path:

```
PS> C:\MinGW\msys/1.0/bin/bash.exe
bash.exe-$ echo $0
"C:\MinGW\msys/1.0/bin/bash.exe"
```

## Setting the PATH on Windows

You probably don't want to have to type the full path for every command.
To avoid that, add the path of the directory containing your new GNU executables. 
There are two root directories: one for MinGW itself (including GCC and its related toolchain) and another for MSYS (including Bash and many common tools from the GNU and BSD projects).

To modify your environment in Windows, click on the application menu and type ``env``.

![Edit your env](mingw-env.jpg)

In the preferences window that appears, click the **Environment variables** button located near the bottom of the window.

In the **Environment variables** window, double click the **Path** selection from the bottom panel.

In the **Edit Environment variables** window that appears, click the **New** button on the right.
Create a new entry reading **C:\MinCW\msys\1.0\bin** and then click OK.
Create a second new entry the same way, this one reading **C:\MinGW\bin**, and then click OK.

Accept these changes in each Preference window.
You can reboot your computer to ensure the new variable is detected by all applications, or just relaunch your Powershell window.

From now on, you can call any MinGW command without specifying the full path, because the full path is in the ``%PATH%`` environment variable of your Windows system, which is inherited by Powershell.

## Hello world

You're all set up now, so put your new MinGW system to a small test.
If you're a ``vim`` user, launch ``vim`` and enter this obligatory "hello world" code:

```
#include <stdio.h>
#include <iostream>

using namespace std;

int main() {
  cout << "Hello open source." << endl;
  return 0;
}
```

Save the file as ``hello.cpp`` and then compile it with the C++ component of GCC:

```
PS> gcc hello.cpp --output hello
```

And finally run it:

```
PS> .\a.exe
Hello open source.
PS> 
```

There's much more to MinGW than what I can cover here.
After all, MinGW opens a whole world of open source and potential for custom code, so take advantage of it.
For a wider world of open source, you can also [give Linux a try](https://opensource.com/article/19/7/ways-get-started-linux).
You'll be amazed at what's possible when all limits are removed.
But in the mean time, give MivGW a try and enjoy the freedom of the GNU.