Monday, April 29, 2024
HomeGolangCross Compile Your Go Packages

Cross Compile Your Go Packages


Introduction
In my publish about constructing and operating packages in Iron.Io, I wanted to converted to my Ubuntu VM to construct linux variations of my check packages regionally. I like the flexibility to have Ubuntu out there to me for constructing and testing my code. Nevertheless, if I can keep on the Mac aspect it’s higher.

I’ve needed to discover ways to cross compile my Go packages for the 2 platforms I take advantage of, darwin/amd64 and linux/amd64. This fashion I might create ultimate builds of my packages and publish every thing from my Mac OS. After a few hours I’m lastly in a position to do that.

In the event you don’t have the necessity to cross compile your code then I like to recommend you stick to the standard distribution packages and installs for Go. If that is one thing you want, then all of it begins with downloading the present launch of the Go supply code.

Putting in Git
The Go supply code is saved in a DVCS known as Github and is positioned on github.com/golang/go. The very first thing it’s good to do is set up Git in case you don’t have already got it.

Go to the obtain web page on the Git web site:  http://git-scm.com/downloads

Cloning Go Supply Code
http://golang.org/doc/set up/supply

Open up a Terminal session and go to your $HOME listing and clone the present launch of the Go supply code. It will create a folder named Go, be sure that this doesn’t exist already:

If every thing works appropriately you need to see the next output or one thing comparable:

Cloning into ‘go’…
distant: Sending roughly 57.30 MiB …
distant: Counting objects: 6825, performed
distant: Discovering sources: 100% (13451345)
distant: Whole 194730 (delta 160700), reused 194473 (delta 160700)
Receiving objects: 100% (194730194730), 55.07 MiB | 2.92 MiB/s, performed.
Resolving deltas: 100% (161032161032), performed.
Checking connectivity… performed.

As soon as that’s performed you now have a folder known as go within the $HOME listing with the supply code below src. Now change your listing to the go folder and checkout the most recent model:

cd go
git checkout go1.4.1

If every thing works appropriately you need to see the next output or one thing comparable:

Word: trying out ‘go1.4.1’.

You’re in ‘indifferent HEAD’ state. You possibly can go searching, make experimental
adjustments and commit them, and you may discard any commits you make on this
state with out impacting any branches by performing one other checkout.

If you wish to create a brand new department to retain commits you create, chances are you’ll
accomplish that (now or later) by utilizing -b with the checkout command once more. Instance:

  git checkout -b new_branch_name

HEAD is now at 886b02d… [release-branch.go1.4] go1.4.1

Constructing Go For Every Goal
Now it’s good to construct the Go code for the targets you want. For now simply construct darwin/amd64 and linux/amd64.

First construct the Go code for the darwin/amd64 goal which is for the host machine:

cd src
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 ./make.bash –no-clean

If every thing builds appropriately you need to see the construct finish like this:


Put in Go for darwin/amd64 in /Customers/identify/go
Put in instructions in /Customers/identify/go/bin

In the event you get the next error in your Mac there’s a repair:

crypto/x509 root_darwin.go:9:43: error: CoreFoundation/CoreFoundation.h: No such file or listing root_darwin.go:10:31: error: Safety/Safety.h: No such file or listing mime/multipart internet/mail

This implies you don’t have the Command Line Instruments for Xcode put in in your machine. To put in the command line instruments on 10.9 comply with these directions:

http://www.computersnyou.com/2025/2013/06/install-command-line-tools-in-osx-10-9-mavericks-how-to/

Click on the Set up button for the Command Line Instruments. As soon as that’s performed attempt constructing the Go code once more.

As soon as the construct is profitable, open the Go folder. You must see every thing you want for constructing Go packages on the Mac 64 bit setting:

Screen Shot

You’ve gotten the go, godoc and gofmt instruments and all of the bundle associated libraries and instruments. Subsequent it’s good to repair your PATH to level to the bin folder so you can begin utilizing the instruments.

export PATH=$HOME/go/bin:$PATH

It’s possible you’ll need to set that in .bashrc or .bash_profile file from contained in the $HOME listing.

With the trail set, examine that Go is working. Examine the model and the setting:

go model
go model go1.4.1 darwin/amd64

go env
GOARCH=”amd64″
GOBIN=””
GOCHAR=”6″
GOEXE=””
GOHOSTARCH=”amd64″
GOHOSTOS=”darwin”
GOOS=”linux”
GOPATH=””
GORACE=””
GOROOT=”/Customers/identify/go”
GOTOOLDIR=”/Customers/identify/go/pkg/device/darwin_amd64″
CC=”gcc”
GOGCCFLAGS=”-g -O2 -fPIC -m64″
CGO_ENABLED=”1″

If every thing appears to be like good, now construct a model of Go that can allow you to construct linux/amd64 binaries:

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

You possibly can’t use CGO when cross compiling. Ensure CGO_ENABLED is about to 0

If every thing builds appropriately you need to see the construct finish like this:


Put in Go for linux/amd64 in /Customers/identify/go
Put in instructions in /Customers/identify/go/bin

In the event you take a look at the Go folder once more you need to see some new folders for linux/amd64:

Screen ShotScreen Shot

Now it’s time to check in case you can construct Go packages for each the Mac and Linux working programs. Arrange a fast GOPATH and Go program. In Terminal run the next instructions:

cd $HOME
mkdir instance
mkdir src
mkdir easy
export GOPATH=$HOME/instance
cd instance/src/easy

Create a file known as foremost.go within the straightforward folder with this code:

bundle foremost

import (
    “fmt”
)

func foremost() {
    fmt.Printf(“Hey Gophersn”)
}

First construct the Mac model and ensure it’s a Mac executable utilizing the file command:

go construct
file easy

easy: Mach-O 64-bit executable x86_64

The file command tells us what sort of file our program is. It’s definitely a Mac executable file.

Now construct the code for linux/amd64:

export GOARCH=”amd64″
export GOOS=”linux”

go construct
file easy

Easy: ELF 64-bit LSB executable, x86-64, model 1 (SYSV), statically linked, not stripped

That you must change both one or each of the OS/ARCH setting variables to level to the goal platform and structure. Then you possibly can construct the code. After the construct you possibly can see the file command is reporting this system is a linux executable.

As a reference, listed here are the completely different platforms and architectures you possibly can construct for cross compilation:

$GOOS     $GOARCH
darwin    386      – 32 bit MacOSX
darwin    amd64    – 64 bit MacOSX
freebsd   386
freebsd   amd64
linux     386      – 32 bit Linux
linux     amd64    – 64 bit Linux
linux     arm      – RISC Linux
netbsd    386
netbsd    amd64
openbsd   386
openbsd   amd64
plan9     386
home windows   386      – 32 bit Home windows
home windows   amd64    – 64 bit Home windows

Putting in Go Tooling
When you end constructing Go for the targets you need, it would be best to set up these Go instruments for the default goal.

go get golang.org/x/instruments/cmd/godoc
go get golang.org/x/instruments/cmd/vet
go get golang.org/x/instruments/cmd/goimports
go get golang.org/x/instruments/cmd/gorename
go get golang.org/x/instruments/cmd/oracle
go get golang.org/x/instruments/cmd/gotype
go get github.com/golang/lint/golint

Launch Updates
In case you are performing an replace make sure to take away the pkg folder out of your GOPATH. These archive information will not be legitimate.

Then it would be best to replace the supply code to the present launch:

**NOTE: Learn This First: http://golang.org/s/go14nopkg

cd $HOME/go/src
git fetch
git checkout go<tag>

Then it would be best to construct your targets once more:

GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 ./make.bash –no-clean
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 ./make.bash –no-clean

Lastly re-build the instruments. Earlier than doing so, take away it’s a good suggestion to take away the pkg folder out of your GOPATH.

go get -u golang.org/x/instruments/cmd/godoc
go get -u golang.org/x/instruments/cmd/vet
go get -u golang.org/x/instruments/cmd/goimports
go get -u golang.org/x/instruments/cmd/gorename
go get -u golang.org/x/instruments/cmd/oracle
go get -u golang.org/x/instruments/cmd/gotype
go get -u github.com/golang/lint/golint

It’s possible you’ll need to take away every thing from the bin folder as properly and go get them to re-build and set up.

Conclusion
That is the documentation for constructing Go from the supply:

http://golang.org/doc/set up/supply

It is a doc written by Dave Cheney about cross compilation. He has construct a script that you could obtain. It makes all of this actual easy to carry out:

http://dave.cheney.internet/2013/07/09/an-introduction-to-cross-compilation-with-go-1-1

Mitchell Hashimoto constructed this useful gizmo known as gox. This device makes it actual straightforward to construct your program for all of the completely different targets with out the necessity to manually change the GOARCH and GOOS setting variables.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments