Gabriel Ayuso
Gabriel Ayuso

Reputation: 207

How to link go package in Xcode CoreFoundation project?

My goal is to be able to call Go functions from a Cocoa project but I just started with a pure C CoreFoundation project.

Here is my simple go package:

package hello

import "C"

import (
    "fmt"
)

//export SayHello
func SayHello() {
    fmt.Println("Hello, World!")
}

I build this using go install which generates the lib hello.a.
I want to be able to link this library to my CoreFoundation project so I can call SayHello from my C code.

Doing this causes Xcode to show a warning stating that hello.a was ignored because it wasn't build for the X86_64 architecture.

I can tell that the issue most likely is due to the fact that the way the Go code was compiled is not compatible with the way XCode is compiling the CoreFoundation project.

Therefore my question is: Is it possible to somehow compile my Go package in a way which is linkable with my CoreFoundation project?

Upvotes: 1

Views: 275

Answers (1)

Jeremy Wall
Jeremy Wall

Reputation: 25237

You can't link a Go library into a C program. The *.a archives that go outputs are not the same format as C object files so the C compiler won't know how to link them in.

The *.a files folow the format described here: http://golang.org/cmd/pack/ and here: http://plan9.bell-labs.com/magic/man2html/1/ar

CGO allows C to call go functions and vice versa but That will require the main app to be a Go binary not a C binary in order for the linking to work correctly.

Upvotes: 1

Related Questions