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 | |
| 23 | You can now run `hello.exe` on a Windows machine near you. |
| 24 | |
| 25 | |
| 26 | ## Older Go version (<1.5) |
| 27 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 28 | I use linux/386, but, I suspect, this procedure will apply to other host platforms as well. |
| 29 | |
Dmitry P | 538454c | 2015-09-09 12:24:39 +0800 | [diff] [blame] | 30 | Preparation (if needed): |
| 31 | ```sh |
| 32 | sudo apt-get install gcc |
| 33 | export go env GOROOT |
| 34 | ``` |
| 35 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 36 | First step is to build host version of go: |
| 37 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 38 | ```sh |
Dmitry P | 9da025e | 2015-09-09 15:07:27 +0800 | [diff] [blame] | 39 | cd $GOROOT/src |
| 40 | sudo -E GOOS=windows GOARCH=386 PATH=$PATH ./make.bash |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 41 | ``` |
| 42 | |
| 43 | Next you need to build the rest of go compilers and linkers. I have small program to do that: |
| 44 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 45 | ```sh |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 46 | $ cat ~/bin/buildcmd |
| 47 | #!/bin/sh |
| 48 | set -e |
| 49 | for arch in 8 6; do |
| 50 | for cmd in a c g l; do |
| 51 | go tool dist install -v cmd/$arch$cmd |
| 52 | done |
| 53 | done |
| 54 | exit 0 |
| 55 | ``` |
| 56 | |
| 57 | Last step is to build windows versions of standard commands and libraries. I have small script for that too: |
| 58 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 59 | ```sh |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 60 | $ cat ~/bin/buildpkg |
| 61 | #!/bin/sh |
| 62 | if [ -z "$1" ]; then |
| 63 | echo 'GOOS is not specified' 1>&2 |
| 64 | exit 2 |
| 65 | else |
| 66 | export GOOS=$1 |
| 67 | if [ "$GOOS" = "windows" ]; then |
| 68 | export CGO_ENABLED=0 |
| 69 | fi |
| 70 | fi |
| 71 | shift |
| 72 | if [ -n "$1" ]; then |
| 73 | export GOARCH=$1 |
| 74 | fi |
| 75 | cd $GOROOT/src |
| 76 | go tool dist install -v pkg/runtime |
| 77 | go install -v -a std |
| 78 | ``` |
| 79 | |
| 80 | I run it like that: |
| 81 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 82 | ```sh |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 83 | $ ~/bin/buildpkg windows 386 |
| 84 | ``` |
| 85 | |
| 86 | 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. |
| 87 | |
| 88 | Now we're ready to build our windows executable: |
| 89 | |
whit3 | bde6ed5 | 2015-02-08 11:14:41 +0100 | [diff] [blame] | 90 | ```go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 91 | $ cat hello.go |
| 92 | package main |
| 93 | |
| 94 | import "fmt" |
| 95 | |
| 96 | func main() { |
| 97 | fmt.Printf("Hello\n") |
| 98 | } |
| 99 | $ GOOS=windows GOARCH=386 go build -o hello.exe hello.go |
| 100 | ``` |
| 101 | |
| 102 | We just need to find Windows computer to run our hello.exe. |