thelearner
thelearner

Reputation: 1136

unable to cross compile from OSX to Linux

When I try to cross compile my golang project from OSX to Linux, then I get following error message:

# runtime/cgo ld: unknown option: --build-id=none clang: error: linker command failed

and the compilation aborts.

This is how I try to build my application:

CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build

I also tried using gox:

 gox -os="linux"

but it still did not work.

Everything works as expected if I do not use the GOOS=linux tag and I am able to build/run my project for/on my OSX machine successfully.

Upvotes: 2

Views: 3700

Answers (2)

thelearner
thelearner

Reputation: 1136

You need to install a proper toolchain to do that.

In order to build binaries for architectures different that your build host, you need far more than just a cross-compiler - you need a full-blown toolchain, which can be a real pain to create, as you probably discovered.

A couple of approaches:

Use a proper Linux distribution in a virtual machine, such as VirtualBox. If you only want to build binaries for Linux/i386 on an MacOSX/x86_64 host, this is - in my opinion - the easiest, safest and most clean solution. It is not a cross-compiler, of course, but it works and it has the added advantage that you can actually test your executables.

Use a script such crosstool-NG (a descendant of the original crosstool) to automatically build the toolchain - definitely easier than building it on your own, although you may have to compromise for slightly older compiler versions.

Cross compiler for linux on mac os x

Upvotes: 0

Daniel Schütte
Daniel Schütte

Reputation: 618

I can confirm that the command

$env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v main.go

works perfectly fine with a "Hello World" main.go file on MacOS X High Sierra without installing anything else than just go (see also here).

As already pointed out in the comments, you are probably trying to compile with cgo and obviously lack parts of the tool chain and/or headers and that is why your linker throws an error. Please provide an acceptable example, otherwise we won't be able to help you.

Upvotes: 4

Related Questions