blob: 7b957537073957e869d3651b088dd3143e629fd5 [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
Dmitry P538454c2015-09-09 12:24:39 +08005Preparation (if needed):
6```sh
7sudo apt-get install gcc
8export go env GOROOT
9```
10
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110011First step is to build host version of go:
12
whit3bde6ed52015-02-08 11:14:41 +010013```sh
Dmitry P9da025e2015-09-09 15:07:27 +080014cd $GOROOT/src
15sudo -E GOOS=windows GOARCH=386 PATH=$PATH ./make.bash
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110016```
17
18Next you need to build the rest of go compilers and linkers. I have small program to do that:
19
whit3bde6ed52015-02-08 11:14:41 +010020```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110021$ cat ~/bin/buildcmd
22#!/bin/sh
23set -e
24for 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
28done
29exit 0
30```
31
32Last step is to build windows versions of standard commands and libraries. I have small script for that too:
33
whit3bde6ed52015-02-08 11:14:41 +010034```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110035$ cat ~/bin/buildpkg
36#!/bin/sh
37if [ -z "$1" ]; then
38 echo 'GOOS is not specified' 1>&2
39 exit 2
40else
41 export GOOS=$1
42 if [ "$GOOS" = "windows" ]; then
43 export CGO_ENABLED=0
44 fi
45fi
46shift
47if [ -n "$1" ]; then
48 export GOARCH=$1
49fi
50cd $GOROOT/src
51go tool dist install -v pkg/runtime
52go install -v -a std
53```
54
55I run it like that:
56
whit3bde6ed52015-02-08 11:14:41 +010057```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110058$ ~/bin/buildpkg windows 386
59```
60
61to 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
63Now we're ready to build our windows executable:
64
whit3bde6ed52015-02-08 11:14:41 +010065```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110066$ cat hello.go
67package main
68
69import "fmt"
70
71func main() {
72 fmt.Printf("Hello\n")
73}
74$ GOOS=windows GOARCH=386 go build -o hello.exe hello.go
75```
76
77We just need to find Windows computer to run our hello.exe.