blob: 2cbf572f593b8002532314ac3eea1ee9437fd6d2 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Building windows go programs on linux
2
3I use linux/386, but, I suspect, this procedure will apply to other host platforms as well.
4
5First step is to build host version of go:
6
whit3bde6ed52015-02-08 11:14:41 +01007```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11008$ cd $GOROOT/src
9$ ./make.bash
10```
11
12Next you need to build the rest of go compilers and linkers. I have small program to do that:
13
whit3bde6ed52015-02-08 11:14:41 +010014```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110015$ cat ~/bin/buildcmd
16#!/bin/sh
17set -e
18for 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
22done
23exit 0
24```
25
26Last step is to build windows versions of standard commands and libraries. I have small script for that too:
27
whit3bde6ed52015-02-08 11:14:41 +010028```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110029$ cat ~/bin/buildpkg
30#!/bin/sh
31if [ -z "$1" ]; then
32 echo 'GOOS is not specified' 1>&2
33 exit 2
34else
35 export GOOS=$1
36 if [ "$GOOS" = "windows" ]; then
37 export CGO_ENABLED=0
38 fi
39fi
40shift
41if [ -n "$1" ]; then
42 export GOARCH=$1
43fi
44cd $GOROOT/src
45go tool dist install -v pkg/runtime
46go install -v -a std
47```
48
49I run it like that:
50
whit3bde6ed52015-02-08 11:14:41 +010051```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110052$ ~/bin/buildpkg windows 386
53```
54
55to 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
57Now we're ready to build our windows executable:
58
whit3bde6ed52015-02-08 11:14:41 +010059```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110060$ cat hello.go
61package main
62
63import "fmt"
64
65func main() {
66 fmt.Printf("Hello\n")
67}
68$ GOOS=windows GOARCH=386 go build -o hello.exe hello.go
69```
70
71We just need to find Windows computer to run our hello.exe.