Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cross compile my Go program from Mac OS X to Ubuntu 64-bit

Tags:

As the title says I'm wondering how to cross-compile my program so that I can run it on Ubuntu 64-bit

I've went into the /usr/local/go/src folder and ran

GOOS=linux GOARCH=amd64 ./make.bash --no-clean

everything compiled fine

then went into my project directory and ran go build -v -a and then took the compiled binary and moved it to my linux server, but when running it I get this error:

root@PanicCSGO40:~/test# ./test -bash: ./test: cannot execute binary file: Exec format error root@PanicCSGO40:~/test# sudo ./test ./test: 1: ./test: Syntax error: "(" unexpected root@PanicCSGO40:~/test# 

Not sure what I am doing wrong any information would be great thanks.

I've also tried doing it with GOARCH=386 but still get the same errors. Thanks!

This link does not solve my question because the chosen answer is a link to a blog post which relies heavily on doing all cross-compilation on using the blog writers bash scripts to do it, I just simply wanted to know what the correct way to do it was and now I do.

like image 585
Datsik Avatar asked Sep 14 '15 04:09

Datsik


People also ask

Are Go binaries cross platform?

You can't take a 64bit binary and run it on a 32bit system. That hasn't been my experience with creating binaries that are meant to be cross platform. To answer your question: "Not at all." Simply because this is impossible to do. No compiler/linker/loader/toolchain can do this.

How do I create an executable in Golang?

Go The Go Command Go BuildPrintln("Hello, World!") } build creates an executable program, in this case: main or main.exe . You can then run this file to see the output Hello, World! . You can also copy it to a similar system that doesn't have Go installed, make it executable, and run it there.


1 Answers

The build command needs to identify the target environment:

$ GOOS=linux GOARCH=amd64 go build -v /path/to/target/package 
like image 93
SnoProblem Avatar answered Sep 29 '22 15:09

SnoProblem