Unsupported File Format error while building for linux-amd64 on mac os

945 views
Skip to first unread message

Ankit Gupta

unread,
Sep 28, 2018, 5:08:55 AM9/28/18
to golang-nuts
I am working with confluent-kafka-go library (https://github.com/confluentinc/confluent-kafka-go) which builds fine on the mac machine (mac OS 10.13.6) along with the code files I wrote. In order to deploy it on Linux server (Ubuntu 64 bit), I try this - 

$> CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build program.go


I get below linking error -


/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1

ld: warning: ignoring file /var/folders/fy/9ph54yjs6cq1kyxgs6cc9rvjvs6t0m/T/go-link-060577916/go.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): /var/folders/fy/9ph54yjs6cq1kyxgs6cc9rvjvs6t0m/T/go-link-060577916/go.o

Undefined symbols for architecture x86_64:

  "__cgo_topofstack", referenced from:

      __cgo_f2fa82ea8f11_Cfunc_rd_kafka_conf_new in 000003.o

      __cgo_f2fa82ea8f11_Cfunc_rd_kafka_conf_set in 000003.o

      __cgo_f2fa82ea8f11_Cfunc_rd_kafka_topic_conf_new in 000003.o

      __cgo_f2fa82ea8f11_Cfunc_rd_kafka_topic_conf_set in 000003.o

      __cgo_f2fa82ea8f11_Cfunc__c_rdkafka_topic_partition_list_entry in 000004.o

      __cgo_f2fa82ea8f11_Cfunc_rd_kafka_assign in 000004.o

      __cgo_f2fa82ea8f11_Cfunc_rd_kafka_assignment in 000004.o

      ...

  "_main", referenced from:

     implicit entry/start for main executable

     (maybe you meant: __cgo_f2fa82ea8f11_Cfunc_rd_kafka_queue_get_main)

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)


It points to /var/folders which I am given to understand is a temp location in mac.


Any ideas on how to fix it?


Ian Lance Taylor

unread,
Sep 28, 2018, 9:53:44 AM9/28/18
to Ankit Gupta, golang-nuts
When cross-compiling a Go program that uses cgo, you need to be using
a C cross-compiler. It looks like you are using the native Darwin C
compiler. You need a cross-compiler from Darwin to GNU/Linux.

Ian

Ankit Gupta

unread,
Sep 28, 2018, 9:59:14 AM9/28/18
to Ian Lance Taylor, golang-nuts
Thanks Ian for responding. Can you point me as to how to get the cross compiler. I am farely new to Mac.

Ian Lance Taylor

unread,
Sep 28, 2018, 10:05:41 AM9/28/18
to Ankit Gupta, golang-nuts
On Fri, Sep 28, 2018 at 6:58 AM, Ankit Gupta
<ankit.gu...@gmail.com> wrote:
> Thanks Ian for responding. Can you point me as to how to get the cross
> compiler. I am farely new to Mac.

Sorry, I don't use Macs at all.

Ian

David Chase

unread,
Sep 28, 2018, 2:58:43 PM9/28/18
to golang-nuts
One possibility is to use Docker on your Mac to run Linux in a container; I use Macs a lot, but don't know how to cross-compile directly when cgo is involved (works fine for non-cgo binaries, however).  This assumes you are okay with Docker and Linux; in both cases, I flail quite a bit but have managed to build and debug Linux programs in a container.

Tamás Gulácsi

unread,
Sep 28, 2018, 4:28:20 PM9/28/18
to golang-nuts
Native compilation is much easier.
If you have Docker installed, https://github.com/karalabe/xgo can help a lot!

roger peppe

unread,
Sep 29, 2018, 5:58:56 AM9/29/18
to Tamás Gulácsi, golang-nuts
I've also found that multipass (https://github.com/CanonicalLtd/multipass) works well for running Ubuntu on different platforms FWIW.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ankit Gupta

unread,
Oct 1, 2018, 5:32:23 AM10/1/18
to golang-nuts
Tamas,

I installed docker on mac and cross compiled using xgo like you suggested. Worked perfectly. Thanks a lot!

Matt Joiner

unread,
Jan 5, 2021, 9:46:28 PM1/5/21
to golang-nuts
I found this, which worked without GOPATH assumptions: https://blog.filippo.io/easy-windows-and-linux-cross-compilers-for-macos/
Reply all
Reply to author
Forward
0 new messages