Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 1 | # Building windows go programs on linux |
| 2 | |
Jesse Jarzynka | 3a8b54f | 2016-12-13 12:30:12 -0500 | [diff] [blame] | 3 | See [here](https://golang.org/doc/install/source#environment) for available `GOOS` and `GOARCH` values. |
| 4 | |
unapiedra | 77cfe1d | 2016-04-01 00:27:26 +0200 | [diff] [blame] | 5 | ## Go version >= 1.5 |
| 6 | |
| 7 | Since Go version 1.5 cross-compiling has become very easy. Try it out with the code below. More can be found at this blog post by [Dave Cheney][1]. |
| 8 | |
| 9 | [1]: http://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5 |
| 10 | |
| 11 | ```go |
| 12 | $ cat hello.go |
| 13 | package main |
| 14 | |
| 15 | import "fmt" |
| 16 | |
| 17 | func main() { |
| 18 | fmt.Printf("Hello\n") |
| 19 | } |
| 20 | $ GOOS=windows GOARCH=386 go build -o hello.exe hello.go |
| 21 | ``` |
| 22 | |
Alberto Donizetti | bc7e7aa | 2017-01-11 14:23:40 +0100 | [diff] [blame] | 23 | You can now run `hello.exe` on a Windows machine near you. |
unapiedra | 77cfe1d | 2016-04-01 00:27:26 +0200 | [diff] [blame] | 24 | |
Alberto Donizetti | bc7e7aa | 2017-01-11 14:23:40 +0100 | [diff] [blame] | 25 | Note that the command above will silently rebuild most of standard library, and for this reason will be quite slow. To speed-up the process, you can install all the windows-amd64 standard packages on your system with |
| 26 | |
| 27 | ``` |
| 28 | GOOS=windows GOARCH=amd64 go install |
| 29 | ``` |
unapiedra | 77cfe1d | 2016-04-01 00:27:26 +0200 | [diff] [blame] | 30 | |
| 31 | ## Older Go version (<1.5) |
| 32 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 33 | I use linux/386, but, I suspect, this procedure will apply to other host platforms as well. |
| 34 | |
Dmitry P | 538454c | 2015-09-09 12:24:39 +0800 | [diff] [blame] | 35 | Preparation (if needed): |
| 36 | ```sh |
| 37 | sudo apt-get install gcc |
| 38 | export go env GOROOT |
| 39 | ``` |
| 40 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 41 | First step is to build host version of go: |
| 42 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 43 | ```sh |
Dmitry P | 9da025e | 2015-09-09 15:07:27 +0800 | [diff] [blame] | 44 | cd $GOROOT/src |
| 45 | sudo -E GOOS=windows GOARCH=386 PATH=$PATH ./make.bash |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 46 | ``` |
| 47 | |
| 48 | Next you need to build the rest of go compilers and linkers. I have small program to do that: |
| 49 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 50 | ```sh |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 51 | $ cat ~/bin/buildcmd |
| 52 | #!/bin/sh |
| 53 | set -e |
| 54 | for arch in 8 6; do |
| 55 | for cmd in a c g l; do |
| 56 | go tool dist install -v cmd/$arch$cmd |
| 57 | done |
| 58 | done |
| 59 | exit 0 |
| 60 | ``` |
| 61 | |
| 62 | Last step is to build windows versions of standard commands and libraries. I have small script for that too: |
| 63 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 64 | ```sh |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 65 | $ cat ~/bin/buildpkg |
| 66 | #!/bin/sh |
| 67 | if [ -z "$1" ]; then |
| 68 | echo 'GOOS is not specified' 1>&2 |
| 69 | exit 2 |
| 70 | else |
| 71 | export GOOS=$1 |
| 72 | if [ "$GOOS" = "windows" ]; then |
| 73 | export CGO_ENABLED=0 |
| 74 | fi |
| 75 | fi |
| 76 | shift |
| 77 | if [ -n "$1" ]; then |
| 78 | export GOARCH=$1 |
| 79 | fi |
| 80 | cd $GOROOT/src |
| 81 | go tool dist install -v pkg/runtime |
| 82 | go install -v -a std |
| 83 | ``` |
| 84 | |
| 85 | I run it like that: |
| 86 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 87 | ```sh |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 88 | $ ~/bin/buildpkg windows 386 |
| 89 | ``` |
| 90 | |
| 91 | to build windows/386 version of Go commands and packages. You can, probably, see it from my script, I exclude building of any cgo related parts - these will not work for me, since I do not have correspondent gcc cross-compiling tools installed. So I just skip those. |
| 92 | |
| 93 | Now we're ready to build our windows executable: |
| 94 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 95 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 96 | $ cat hello.go |
| 97 | package main |
| 98 | |
| 99 | import "fmt" |
| 100 | |
| 101 | func main() { |
| 102 | fmt.Printf("Hello\n") |
| 103 | } |
| 104 | $ GOOS=windows GOARCH=386 go build -o hello.exe hello.go |
| 105 | ``` |
| 106 | |
| 107 | We just need to find Windows computer to run our hello.exe. |