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