blob: b90e0da95d81a7d9182c2e47c4d04b945d11706e [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Building windows go programs on linux
2
Jesse Jarzynka3a8b54f2016-12-13 12:30:12 -05003See [here](https://golang.org/doc/install/source#environment) for available `GOOS` and `GOARCH` values.
4
unapiedra77cfe1d2016-04-01 00:27:26 +02005## Go version >= 1.5
6
7Since 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
13package main
14
15import "fmt"
16
17func main() {
18 fmt.Printf("Hello\n")
19}
20$ GOOS=windows GOARCH=386 go build -o hello.exe hello.go
21```
22
Alberto Donizettibc7e7aa2017-01-11 14:23:40 +010023You can now run `hello.exe` on a Windows machine near you.
unapiedra77cfe1d2016-04-01 00:27:26 +020024
Alberto Donizettibc7e7aa2017-01-11 14:23:40 +010025Note 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```
28GOOS=windows GOARCH=amd64 go install
29```
unapiedra77cfe1d2016-04-01 00:27:26 +020030
31## Older Go version (<1.5)
32
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110033I use linux/386, but, I suspect, this procedure will apply to other host platforms as well.
34
Dmitry P538454c2015-09-09 12:24:39 +080035Preparation (if needed):
36```sh
37sudo apt-get install gcc
38export go env GOROOT
39```
40
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110041First step is to build host version of go:
42
whit3bde6ed52015-02-08 11:14:41 +010043```sh
Dmitry P9da025e2015-09-09 15:07:27 +080044cd $GOROOT/src
45sudo -E GOOS=windows GOARCH=386 PATH=$PATH ./make.bash
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110046```
47
48Next you need to build the rest of go compilers and linkers. I have small program to do that:
49
whit3bde6ed52015-02-08 11:14:41 +010050```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110051$ cat ~/bin/buildcmd
52#!/bin/sh
53set -e
54for 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
58done
59exit 0
60```
61
62Last step is to build windows versions of standard commands and libraries. I have small script for that too:
63
whit3bde6ed52015-02-08 11:14:41 +010064```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110065$ cat ~/bin/buildpkg
66#!/bin/sh
67if [ -z "$1" ]; then
68 echo 'GOOS is not specified' 1>&2
69 exit 2
70else
71 export GOOS=$1
72 if [ "$GOOS" = "windows" ]; then
73 export CGO_ENABLED=0
74 fi
75fi
76shift
77if [ -n "$1" ]; then
78 export GOARCH=$1
79fi
80cd $GOROOT/src
81go tool dist install -v pkg/runtime
82go install -v -a std
83```
84
85I run it like that:
86
whit3bde6ed52015-02-08 11:14:41 +010087```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110088$ ~/bin/buildpkg windows 386
89```
90
91to 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
93Now we're ready to build our windows executable:
94
whit3bde6ed52015-02-08 11:14:41 +010095```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110096$ cat hello.go
97package main
98
99import "fmt"
100
101func main() {
102 fmt.Printf("Hello\n")
103}
104$ GOOS=windows GOARCH=386 go build -o hello.exe hello.go
105```
106
107We just need to find Windows computer to run our hello.exe.