How do I cross compile a Go project on Linux to generate an executable for running on Windows?
From the command line in the hello directory, run the go build command to compile the code into an executable. From the command line in the hello directory, run the new hello executable to confirm that the code works.
To build from Linux to Windows, you need to set the environment variables GOOS
to Windows
and GOARCH
to amd64
.
On Bash or ZSH:
% GOOS=windows GOARCH=amd64 go build
If your package requires CGO
then you need to use the mingw-w64 compiler:
sudo apt-get install gcc-multilib sudo apt-get install gcc-mingw-w64 GOOS=windows GOARCH=386 \ CGO_ENABLED=1 CXX=i686-w64-mingw32-g++ CC=i686-w64-mingw32-gcc \ go build
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With