Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: How to cross compile on Linux for Windows

Tags:

How do I cross compile a Go project on Linux to generate an executable for running on Windows?

like image 596
shagul Avatar asked Jan 10 '17 10:01

shagul


People also ask

Can Go be compiled to EXE?

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.


1 Answers

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 
  • For more details see: https://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5
  • A description of possible values for GOOS and GOARCH is available here: https://golang.org/doc/install/source#environment

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 
like image 138
Michael Miller Avatar answered Sep 26 '22 23:09

Michael Miller