Golang compilation for all platforms in Windows 7 (32 bit)

I am using the Windows 7 operating system [32 bit].

I am creating a go program example.

I want to compile this program for all platforms from my Windows 7 operating system [32 bit].

I want to compile my program for all Linux [32/64] / Mac OSX [32/64] / Windows[32/64] .

Is this possible not from my only OS?

+7
windows cross-platform go cross-compiling
source share
2 answers

Update Go 1.5: see " Cross-compiling just got a lot better in Go 1.5

For successful cross-compilation you will need

  • for the target platform, if they are different from your host platform, i.e. youre on darwin / amd64 (6g), and you want to compile for linux / arm (5g).
  • A standard library for the target platform, which includes some files generated at the time your Go distribution was created.

With the plan to move the Go compiler to Go , which will be implemented in version 1.5, the first problem will be solved.

 package main import "fmt" import "runtime" func main() { fmt.Printf("Hello %s/%s\n", runtime.GOOS, runtime.GOARCH) } 

build for darwin / 386

 % env GOOS=darwin GOARCH=386 go build hello.go # scp to darwin host $ ./hello Hello darwin/386 

Or create for linux / arm

 % env GOOS=linux GOARCH=arm GOARM=7 go build hello.go # scp to linux host $ ./hello Hello linux/arm 

Original answer (Go 1.4 and earlier)

You can try a tool like gox

Gox is a simple, no-frills tool for cross-compiling Go, which behaves just like the standard go build. Gox will parallelize builds across multiple platforms.
Gox will also build cross compilation for you.

Before using Gox, you must create a cross-compilation toolchain. Gox can do it for you. This needs to be done only once (or whenever you update Go):

 $ gox -build-toolchain 

You'll also find many cross-platform development tips on Cross-Platform Development with Go . "

A passionate developer points below to question 19 left by OP Nakka Chandra , although issue 10 reported how to successfully run gox on Windows.

+11
source share

On Windows, run the following commands:

 C:\Users\user\go\src\myapp> set GOOS=linux C:\Users\user\go\src\myapp> set GOARCH=amd64 C:\Users\user\go\src\myapp> go build 

It worked for me.

Please note if you receive an error:

cmd / go: unsupported GOOS / GOARCH linux / amd64 pair

This is because you have a space at the end of the variable.
Example, misuse: set GOOS=linux<space>) , instead it should be: set GOOS=linux .

This is a complete list of tables (taken from here ) for all other systems:

  GOOS - Target Operating System| GOARCH - Target Platform -------------------------------|-------------------------- | android | arm | | darwin | 386 | | darwin | amd64 | | darwin | arm | | darwin | arm64 | | dragonfly | amd64 | | freebsd | 386 | | freebsd | amd64 | | freebsd | arm | | linux | 386 | | linux | amd64 | | linux | arm | | linux | arm64 | | linux | ppc64 | | linux | ppc64le | | linux | mips | | linux | mipsle | | linux | mips64 | | linux | mips64le | | netbsd | 386 | | netbsd | amd64 | | netbsd | arm | | openbsd | 386 | | openbsd | amd64 | | openbsd | arm | | plan9 | 386 | | plan9 | amd64 | | solaris | amd64 | | windows | 386 | | windows | amd64 | ---------------------------------------------------------- 
0
source share

All Articles