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