Installing gcc on Windows for Go
I see... Cgo packages need gcc
Table of contents
Problem
I am recently learning Go and I stumbled upon an error about exec: "gcc": executable file not found in %PATH%
when using the github.com/mattn/go-sqlite3
package. The package I am installing is a Cgo package which calls C code directly in Go. It requires gcc
to be installed. Indeed, I did not have gcc
installed on my Windows machine.
Solution
Pre-compiled native binaries of gcc
for Windows are not available. We have to rely on an intermediary such as Cygwin and MinGW. Luckily, MSYS2 makes this process easy for us. After installing it, you are presented with a UCRT64 terminal.
Within the terminal, install mingw-w64-x86_64-gcc
using the pacman
package manager, like so:
$ pacman -S mingw-w64-x86_64-gcc
This will install the native Windows executables of gcc
. Afterward, add C:\msys64\mingw64\bin
to your PATH
environment variable so that gcc
can be executed from anywhere on your machine.
My Go program was successfully compiled after doing the steps above. Awesome!
Closing Thoughts
This short article is based on a similar and comprehensive article for C/C++ development.
The issues that I encountered would have been immediately resolved if my main operating system was running on Linux. Even more so, I would not have to rely on an intermediary tool to run gcc
on my machine. I am looking forward to making the complete switch to Linux anytime soon ๐ค.