blob: 1fc6dbd8a2e4d923645f9a54d2bd5ec823dc7345 [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
23You can now run `hello.exe` on a Windows machine near you.
24
25
26## Older Go version (<1.5)
27
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110028I use linux/386, but, I suspect, this procedure will apply to other host platforms as well.
29
Dmitry P538454c2015-09-09 12:24:39 +080030Preparation (if needed):
31```sh
32sudo apt-get install gcc
33export go env GOROOT
34```
35
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110036First step is to build host version of go:
37
whit3bde6ed52015-02-08 11:14:41 +010038```sh
Dmitry P9da025e2015-09-09 15:07:27 +080039cd $GOROOT/src
40sudo -E GOOS=windows GOARCH=386 PATH=$PATH ./make.bash
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110041```
42
43Next you need to build the rest of go compilers and linkers. I have small program to do that:
44
whit3bde6ed52015-02-08 11:14:41 +010045```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110046$ cat ~/bin/buildcmd
47#!/bin/sh
48set -e
49for 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
53done
54exit 0
55```
56
57Last step is to build windows versions of standard commands and libraries. I have small script for that too:
58
whit3bde6ed52015-02-08 11:14:41 +010059```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110060$ cat ~/bin/buildpkg
61#!/bin/sh
62if [ -z "$1" ]; then
63 echo 'GOOS is not specified' 1>&2
64 exit 2
65else
66 export GOOS=$1
67 if [ "$GOOS" = "windows" ]; then
68 export CGO_ENABLED=0
69 fi
70fi
71shift
72if [ -n "$1" ]; then
73 export GOARCH=$1
74fi
75cd $GOROOT/src
76go tool dist install -v pkg/runtime
77go install -v -a std
78```
79
80I run it like that:
81
whit3bde6ed52015-02-08 11:14:41 +010082```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110083$ ~/bin/buildpkg windows 386
84```
85
86to 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
88Now we're ready to build our windows executable:
89
whit3bde6ed52015-02-08 11:14:41 +010090```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110091$ cat hello.go
92package main
93
94import "fmt"
95
96func main() {
97 fmt.Printf("Hello\n")
98}
99$ GOOS=windows GOARCH=386 go build -o hello.exe hello.go
100```
101
102We just need to find Windows computer to run our hello.exe.