如何在Xcode CoreFoundation项目中链接go包?

huangapple go评论101阅读模式
英文:

How to link go package in Xcode CoreFoundation project?

问题

我的目标是能够从Cocoa项目中调用Go函数,但我刚刚开始使用纯C CoreFoundation项目。

这是我的简单go包:

package hello

import "C"

import (
	"fmt"
)

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

我使用go install构建这个包,生成了lib hello.a
我想要将这个库链接到我的CoreFoundation项目中,这样我就可以从我的C代码中调用SayHello

这样做会导致Xcode显示一个警告,指出hello.a被忽略,因为它没有为X86_64架构构建。

我可以看出问题很可能是由于Go代码的编译方式与XCode编译CoreFoundation项目的方式不兼容。

因此我的问题是:是否有可能以某种方式编译我的Go包,使其可以与我的CoreFoundation项目链接起来?

英文:

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?

答案1

得分: 1

你不能将Go库链接到C程序中。Go输出的*.a归档文件与C对象文件的格式不同,因此C编译器不知道如何链接它们。

*.a文件遵循这里描述的格式:http://golang.org/cmd/pack/ 和这里:http://plan9.bell-labs.com/magic/man2html/1/ar

CGO允许C调用Go函数,反之亦然,但这将要求主应用程序是一个Go二进制文件而不是C二进制文件,以便链接能够正常工作。

英文:

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.

huangapple
  • 本文由 发表于 2013年7月5日 03:24:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/17477026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定