Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Compiling Go

I am trying to cross compile Go for ubuntu linux x86_64 on my macbook. I have followed instructions outlined here but when I run go-linux-amd64 build I get the following message go build runtime: linux/amd64 must be bootstrapped using make.bash. Any help with this will be appreciated.

like image 489
cobie Avatar asked Dec 10 '14 23:12

cobie


1 Answers

What it's saying you need to do is rebuild the library and runtime for linux-amd64. You can do that this way:

  1. Find the root of your Go installation (if you don't know where this is, running which go may help - the binary is often installed with the rest of the sources).
  2. cd into the src directory
  3. Run GOOS=linux GOARCH=amd64 ./make.bash --no-clean (or GOOS=linux GOARCH=amd64 bash make.bash --no-clean if make.bash is not executable). This will rebuild the library and runtime using the specified OS and architecture.

Once you've done this, you can build a go package or binary for this architecture using GOOS=linux GOARCH=amd64 go build. You can follow the same instructions for other architectures and operating systems.

Edit (08/13/15):

As of Go 1.5, cross compiling is much easier. Since the runtime is written in Go, there's no need to set anything up in order to be able to cross-compile. You can now just run GOOS=<os> GOARCH=<arch> go build from a vanilla Go installation and it will work.

However, there's one exception to this. If you're using cgo, you'll still need to set stuff up ahead of time. And you'll need to inform the tooling that you want to enable cgo cross-compiling by setting the CGO_ENABLED environment variable to 1. So, to be precise:

  1. cd into the src directory of your Go installation (see the instructions above).
  2. Run CGO_ENABLED=1 GOOS=<os> GOARCH=<arch> ./make.bash --no-clean
  3. Run CGO_ENABLED=1 go build to build your project. It is important that you specify CGO_ENABLED=1 even when you're compiling.
like image 119
joshlf Avatar answered Sep 25 '22 03:09

joshlf