2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
thelearner
  • 1,440
  • 3
  • 27
  • 58
  • 3
    Without a [mcve] we can only guess. What are the build constraints for the file in which `Image` is declared? – JimB Jun 28 '18 at 20:20
  • `Image` is in a file that uses cgo, and you're not compiling with cgo. Do you also have the complete toolchain installed to cross-compile C code to Linux? – JimB Jun 28 '18 at 20:30
  • If you're now using `CGO_ENABLED=1` and there are no build constraints in the source, and it's still not there, what is the name of the file? – JimB Jun 28 '18 at 20:45
  • 1
    cgo was not enabled before, because you need to explicitly enable it for cross-compilation. That message is because you aren't using the right toolchain to target linux. If you have the right tools, you can use `CC_FOR_TARGET` and `CXX_FOR_TARGET ` to specify them (look for "cross-compiling" in the [docs here](https://golang.org/cmd/cgo/)). It's probably much easier to build this in a docker container than trying to get cross compilation running. – JimB Jun 28 '18 at 20:50
  • It's not really a beginner topic, but you would need begin by installing a cross-compiler for linux, probably gcc (and possibly any missing headers). Building in a docker container would "just work", so that would be my recommendation. There are a ton of resources out there for how to use docker. – JimB Jun 28 '18 at 21:05
  • Note we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Jul 01 '18 at 10:32

2 Answers2

4

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel Schütte
  • 578
  • 1
  • 6
  • 20
  • Thanks a lot for your answer. How can I possibly detect missing parts of the toolchain? – thelearner Jun 30 '18 at 12:57
  • First, I recommend to check if you can compile a "Hello World" program with the command described *above*. Does that work? Also, what is the output of `$go env` and `$gcc -v`? I am by no means a `cgo` expert, so you might want to take a closer look at the respective [docs](https://golang.org/cmd/cgo/). Also, try to post a minimal example so that we can try to reproduce your error. – Daniel Schütte Jun 30 '18 at 18:15
  • Hello Daniel, thanks a lot for helping me. I'm even unable to compile a simple hello world app. I'm getting following error now. # runtime/cgo ld: unknown option: --build-id=none clang: error: linker command failed with exit code 1 (use -v to see invocation) – thelearner Jul 03 '18 at 15:22
  • Please add the output of `$go env` and `$gcc -v` to your question. – Daniel Schütte Jul 03 '18 at 16:27
0

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

thelearner
  • 1,440
  • 3
  • 27
  • 58
  • 1
    This should not be the accepted answer. Quote "Since Go version 1.5 cross-compiling has become very easy" [official source](https://github.com/golang/go/wiki/WindowsCrossCompiling). In this particular case, some of the required `go tools` are not installed [see here](https://golang.org/doc/install/source). – Daniel Schütte Jul 05 '18 at 23:08