g++ with MSYS & VS Code

“Building technical systems involves a lot of hard work and specialized knowledge: languages and protocols, coding and debugging, testing and refactoring.” - Jesse James Garrett

Photo by Markus Spiske on Unsplash

This took me longer than I care to admit to figure out. Hence I wrote this tutorial in the hope of saving someone out there some hassle. This was done on Windows 11 Home Edition.

If you’ve been reading for a while you know I love Linux and Open Source code. However, with the new-ish CEO of Microsoft, Satya Nadella, embracing more and more Open Source technologies -- coupled with the surprising acceptance of ArduPilot by non-techincal users, I have decided to give Microsoft another chance and dip my toes into the Windows world once again. This required a bit of mental gymnastics: my solution involves VS Code, MSYS2, and MinGW.

MSYS2 For Compiling with MinGW

Much of the following is from this tutorial.

Begin by installing Visual Studio Code and the C/C++ Extension for it. Then install MSYS2 by following these instructions. To be on the safe side I installed MSYS2 as an admin, though I’m 90% sure I didn’t need to. What is needed is the MSYS package manager. In the MSYS2 prompt run:

pacman -Syu

I had to run this command multiple times until pacman was fully updated and I had to restart my MSYS2 prompt as part of the update process. The instructions say to run:

pacman -Su

but I don’t think that is strictly necessary. I did it just in case. Finally run:

pacman -S --needed base-devel mingw-w64-x86_64-toolchain

You can read more about MSYS2’s pacman here.

Change Windows PATH

Next you need to point the Windows PATH environment variable to the MinGW compiler that was just installed. For the uninitiated, environment variables can be daunting at first, but once you get the hang of them they are incredibly useful.

Begin by pulling up the “Settings” link from the Windows 11 Start Menu:

Windows 11 Start Menu: Settings circled.

Once there, search for “Edit environment variables for your account.”

Once inside Windows 11 “Settings” search for “Edit environment variables for your account.”

This should pull up a dialog that will allow you to edit the environment variables. We want to append to the Path USER environment variable (which is DIFFERENT from the Path SYSTEM environment variable). See: https://stackoverflow.com/questions/4477660/what-is-the-difference-between-user-variables-and-system-variables. Add C:\msys64\mingw64\bin to the PATH list.

Add C:\msys64\mingw64\bin to the PATH list.

Click OK (not Cancel) to save the updated PATH.

Verify the change was successful by checking at a PowerShell or other prompt (the Windows Terminal works too). Note that this requires a restart of PowerShell if it was already open before you started installing MSYS2. The desired commands are:

g++ --version

and

gdb --version

Post Publication Addendum:

Running the Build

We are almost there -- to get MinGW to compile your code takes one more step. Setting up a tasks.json file is left as an exercise for the reader, but this example should help:

{

"tasks": [

{

"type": "cppbuild",

"label": "C/C++: g++.exe build active file",

"command": "C:/msys64/mingw64/bin/g++.exe",

"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],

"options": {

"cwd": "${fileDirname}"

},

"problemMatcher": ["$gcc"],

"group": {

"kind": "build",

"isDefault": true

},

"detail": "compiler: C:/msys64/mingw64/bin/g++.exe"

}

],

"version": "2.0.0"

}

The following text is taken directly from https://code.visualstudio.com/docs/cpp/config-mingw#_running-the-build:

To run the build task defined in tasks.json, press Ctrl+Shift+B or from the Terminal main menu choose Run Build Task.

What my screen showed was this:

How to convince VS Code to use a non-Visual Studio compiler

When the task starts, you should see the Integrated Terminal panel appear below the source code editor. After the task completes, the terminal shows output from the compiler that indicates whether the build succeeded or failed.

Create a new terminal using the + button and you'll have a new terminal with the helloworld folder as the working directory. Run dir and you should now see the executable helloworld.exe.

You can run helloworld in the terminal by typing helloworld.exe (or .\helloworld.exe if you use a PowerShell terminal).

Note: You might need to press Enter a couple of times initially to see the PowerShell prompt in the terminal. This issue should be fixed in a future release of Windows.

Pro Tip: be sure you have the .cpp file you want selected in the VS Code window, NOT the tasks.json file!

Next Steps

If you don’t see any errors then congratulations! You have successfully installed the MinGW compiler on Windows! You can now use this bookmark to walk you through:

  • Setting up a Hello World program

  • How the gdb works with VS Code

  • And more!

Credits

Stackoverflow: https://stackoverflow.com/questions/47872250/cant-compile-code-launch-program-program-path-does-not-exist

MinGW-64: https://www.mingw-w64.org/

Powershell Doc: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.2

Random repo on GitHub that was a big help: https://github.com/manoharreddyporeddy/programming-language-notes/tree/master/vscode-c%2B%2B

Previous
Previous

CMake Introduction

Next
Next

Matplotlib