Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using windows 7 [32 bit] operating system.

I'm build example go program.

I want to compile this program for all platforms from my windows 7 [32 bit] OS.

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

Is it possible are not from my single OS ?

like image 776
Chandra Nakka Avatar asked Jul 31 '14 05:07

Chandra Nakka


3 Answers

Update Go 1.5: see "Cross compilation just got a whole lot better in Go 1.5"

For successful cross compilation you would need

  • compilers for the target platform, if they differed from your host platform, ie you’re on darwin/amd64 (6g) and you want to compile for linux/arm (5g).
  • a standard library for the target platform, which included some files generated at the point your Go distribution was built.

With the plan to translate the Go compiler into Go coming to fruition in the 1.5 release the first issue is now resolved.

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 build 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 before)

You can try a tool like gox

Gox is a simple, no-frills tool for Go cross compilation that behaves a lot like standard go build.
Gox will parallelize builds for multiple platforms.
Gox will also build the cross-compilation toolchain for you.

Before you use Gox, you must build the cross-compilation toolchain. Gox can do this for you. This only has to be done once (or whenever you update Go):

$ gox -build-toolchain

You will also find many cross-platform development tips at "Developing for Multiple Platforms With Go".

Passionate Developer points out below to issue 19, left by the OP Nakka Chandra, even though issue 10 reported making gox run successfully on Windows.

like image 145
VonC Avatar answered Sep 29 '22 11:09

VonC


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.

Notice, if you get the error:

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

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

This is the full table list (taken from here) for all the 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         |
   ----------------------------------------------------------
like image 30
E235 Avatar answered Sep 29 '22 10:09

E235


You can find the full list of supported GOOS/GOARCH platforms supported by your go by running go tool dist list.

For Go 1.18.1, this shows the following pairs, after filtering based on the target OSes you said you are interested in:

$ go tool dist list | grep -e linux -e darwin -e windows
darwin/amd64
darwin/arm64
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
windows/386
windows/amd64
windows/arm
windows/arm64

If you want to compile your code for all of these, you can do this with the bash/sh script below:

bin_name='example'  # Change the binary name as desired

go tool dist list | grep -e linux -e darwin -e windows | while read -r pair; do
    # Extract the GOOS and GOARCH from the pair
    GOOS="${pair%/*}"
    GOARCH="${pair#*/}"

    suffix=''
    # If GOOS is windows, set suffix to .exe
    [ "$GOOS" = 'windows' ] && suffix='.exe'

    # Replace <args> with appropriate arguments as needed
    GOOS="$GOOS" GOARCH="$GOARCH" \
        go build -o "${bin_name}-${GOOS}-${GOARCH}${suffix}" <args>
done

This script will produce an executable for each OS/architecture pair, and append the OS and architecture at the end of the name. It will also append .exe if the OS is Windows.

like image 23
Shane Bishop Avatar answered Sep 29 '22 11:09

Shane Bishop