How to compile linux/amd64 binary w/ cgo support on OSX?

1,386 views
Skip to first unread message

radek...@gmail.com

unread,
Jan 4, 2016, 11:32:42 AM1/4/16
to golang-nuts
Hi,
I'm having troubles with cross-compilation on OSX 10.10.5

$ uname -a
Darwin ... 14.5.0 Darwin Kernel Version 14.5.0: Tue Sep  1 21:23:09 PDT 2015; root:xnu-2782.50.1~1/RELEASE_X86_64 x86_64

$ go version
go version go1.5.2 darwin/amd64

$ printenv | grep GO
GOPATH=/Users/radeksimko/gopath

I have installed the latest go via Homebrew.


I'm unable to compile it for linux/amd64:

$ CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v -o linux-amd64-bin.o
runtime
errors
sync/atomic
math
unicode/utf8
unicode
runtime/cgo
sync
io
syscall
strings
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)
strconv
time
reflect
os
fmt

Is there anything wrong with my approach to the cross-compilation or is it a bug in go itself?

Radek Simko

Jakob Borg

unread,
Jan 4, 2016, 1:09:59 PM1/4/16
to radek...@gmail.com, golang-nuts
2016-01-04 17:31 GMT+01:00 <radek...@gmail.com>:
> Hi,
> I'm having troubles with cross-compilation on OSX 10.10.5
[...]
> I'm unable to compile it for linux/amd64:
>
> $ CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v -o linux-amd64-bin.o

Cross compiling with cgo requires having a C cross compiler as well.
Without using cgo, you should be good to go.

//jb

radek...@gmail.com

unread,
Jan 5, 2016, 7:11:20 AM1/5/16
to golang-nuts, radek...@gmail.com
Understood, I can (surprisingly) compile the attached gist with os/user when using CGO_ENABLE=0 without any errors, but then it doesn't work when I try running it on the target platform (linux/amd64).

I guess the lack of my C & C-binding knowledge plays the role here.

Setting up the full C cross-compile toolchain doesn't seem to be trivial from what I've read so far and it looks like I can get around this by replacing os/user with github.com/mitchellh/go-homedir since I only need to get the user's homedir.

-- 
Radek Simko
Reply all
Reply to author
Forward
0 new messages