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.

5
  • 3
    Without a minimal reproducible example we can only guess. What are the build constraints for the file in which Image is declared? Commented Jun 28, 2018 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? Commented Jun 28, 2018 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? Commented Jun 28, 2018 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). It's probably much easier to build this in a docker container than trying to get cross compilation running. Commented Jun 28, 2018 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. Commented Jun 28, 2018 at 21:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for your answer. How can I possibly detect missing parts of the toolchain?
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. Also, try to post a minimal example so that we can try to reproduce your error.
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)
Please add the output of $go env and $gcc -v to your question.
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

1 Comment

This should not be the accepted answer. Quote "Since Go version 1.5 cross-compiling has become very easy" official source. In this particular case, some of the required go tools are not installed see here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.