即將發(fā)布的 Go 1.5 帶有新的構(gòu)建模式,允許導(dǎo)出要從 C 代碼鏈接和調(diào)用的 Go 符號。我一直在玩它,并獲得了基本的“Hello world”示例,但現(xiàn)在我正在嘗試鏈接一個(gè) Go 庫,它啟動(dòng)了 anet/http.Server并且它失敗了。代碼如下所示(也可在此處獲得):gohttplib.go:package mainimport "C"import "net/http"//export ListenAndServefunc ListenAndServe(caddr *C.char) { addr := C.GoString(caddr) http.ListenAndServe(addr, nil)}func main() {}示例/c/main.c:#include <stdio.h>#include "../../gohttplib.h"int main(){ ListenAndServe(":8000"); return 0;}生成靜態(tài)鏈接的對象和標(biāo)題工作正常:$ go build -buildmode=c-archive但是針對它進(jìn)行編譯失敗了:$ gcc -o gohttp-c examples/c/main.c gohttplib.a -lpthreadUndefined symbols for architecture x86_64: "_CFArrayGetCount", referenced from: _FetchPEMRoots in gohttplib.a(000003.o) "_CFArrayGetValueAtIndex", referenced from: _FetchPEMRoots in gohttplib.a(000003.o) "_CFDataAppendBytes", referenced from: _FetchPEMRoots in gohttplib.a(000003.o) "_CFDataCreateMutable", referenced from: _FetchPEMRoots in gohttplib.a(000003.o) "_CFDataGetBytePtr", referenced from: _FetchPEMRoots in gohttplib.a(000003.o) __cgo_6dbb806e9976_Cfunc_CFDataGetBytePtr in gohttplib.a(000003.o) (maybe you meant: __cgo_6dbb806e9976_Cfunc_CFDataGetBytePtr) "_CFDataGetLength", referenced from: _FetchPEMRoots in gohttplib.a(000003.o) __cgo_6dbb806e9976_Cfunc_CFDataGetLength in gohttplib.a(000003.o) (maybe you meant: __cgo_6dbb806e9976_Cfunc_CFDataGetLength) "_CFRelease", referenced from: _FetchPEMRoots in gohttplib.a(000003.o) __cgo_6dbb806e9976_Cfunc_CFRelease in gohttplib.a(000003.o) (maybe you meant: __cgo_6dbb806e9976_Cfunc_CFRelease)這是在 OS X 10.9.5 上使用 Go github 存儲(chǔ)庫 (38e3427) 中的最新版本。我知道 Go 1.5 還沒有發(fā)布,并且不能保證它可以工作,但我這樣做是出于教育目的,我懷疑我遺漏了一些東西。
使用 Go 1.5 buildmode=c-archive 和從 C 鏈接的
人到中年有點(diǎn)甜
2021-10-18 10:28:35