4

I'm having issues trying to cross-compile a Go app on OS X to run on linux/amd64. The app in question is using libvips via this vips go package. As such, it is using CGO and needs to be compiled with CGO support.

I'm on Go 1.4 and running the following build command

GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build

but end up with a linker error

ld: unknown option: --build-id=none clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm assuming I probably need to add some kind of -ldflags argument but am not sure.

Is it possible to cross-compile CGO apps in this manner yet, or do I need to do a native-build on the target system to avoid issues and headaches?

cpjolicoeur
  • 12,766
  • 7
  • 48
  • 59
  • I don't have OSX but it looks like a bad ld version or something. I'd say it's easier to just fire up a vm (does qemu work on mac?) and do your compiling there. – OneOfOne Feb 23 '15 at 02:04
  • Yeah, I can native compile via a VM fine, and, of course, that whole process can be automated. I was just trying to fight through and see if I can just cross-compile it all in one go natively like I can with non-CGO golang programs – cpjolicoeur Feb 23 '15 at 02:16
  • I'd try to see if you can get a newer/different version of clang and ld but maybe someone else will know better. – OneOfOne Feb 23 '15 at 02:28
  • `--build-id` is a command line argument recognised by GNU ld (used on Linux), so it looks like you're using the Mac tool chain here. You will likely need to install a Linux cross compiler toolchain to get this to work, but I've never done this on MacOS myself. – James Henstridge Feb 23 '15 at 23:34
  • @cpjolicoeur is a bit late but maybe there are others who I can help. I always get this linker error when using clang on linux (natively) the solution (for me) was to overwrite `CC` and `CXX` environment variables to use gcc in this case. – bodokaiser May 30 '15 at 13:14
  • https://github.com/karalabe/xgo while this requires containers and docker it is very easy to use – Srgrn Jun 17 '15 at 23:41

2 Answers2

1

Have a look at gonative. this allows you to cross-compile cgo code (as long as you are just using the stdlib).

Another approach would be to compile the linux binary using docker.

simonmenke
  • 2,819
  • 19
  • 28
0

Fast-forwarding to 2022, using docker for cross-compiling CGO apps to other platforms is your best and cleanest choice. Build docker containers that contain the correct cross-platform compiler and C libraries. Here is how I cross-compile my Cgo applications with Docker. Here is the repo with the dockerfiles.

dh1tw
  • 1,411
  • 2
  • 23
  • 29