问题描述
围棋1.5即将发布的带有<一个href=\"https://docs.google.com/document/d/1nr-TQHw_er6GOQRsF6T43GGhFDelrAP0NqSS_00RgZQ/view?pli=1\">new buildmodes 允许出口转到符号链接,并从C code调用。我一直在玩了,并得到了基本的Hello World的例子的工作,但现在我试图链接,启动围棋库网/ http.Server
而它的失败。在code看起来像这样():
gohttplib.go:
主包进口C
进口网/ HTTP//出口ListenAndServe
FUNC ListenAndServe(caddr * C.char){
地址:= C.GoString(caddr)
http.ListenAndServe(地址,无)
}FUNC主(){}
例子/ C / main.c中:
的#include&LT;&stdio.h中GT;
#包括../../gohttplib.h诠释的main()
{
ListenAndServe(:8000);
返回0;
}
产生静态链接的对象和头正常工作:
$去打造-buildmode = C-档案
但编译反对失败:
$ gcc的-o gohttp-C的例子/ C / main.c中gohttplib.a -lpthread
适用于建筑x86_64的未定义符号:
_CFArrayGetCount,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
_CFArrayGetValueAtIndex,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
_CFDataAppendBytes,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
_CFDataCreateMutable,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
_CFDataGetBytePtr,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
__cgo_6dbb806e9976_Cfunc_CFDataGetBytePtr在gohttplib.a(000003.o)
(也许你的意思是:__cgo_6dbb806e9976_Cfunc_CFDataGetBytePtr)
_CFDataGetLength,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
__cgo_6dbb806e9976_Cfunc_CFDataGetLength在gohttplib.a(000003.o)
(也许你的意思是:__cgo_6dbb806e9976_Cfunc_CFDataGetLength)
_CFRelease,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
__cgo_6dbb806e9976_Cfunc_CFRelease在gohttplib.a(000003.o)
(也许你的意思是:__cgo_6dbb806e9976_Cfunc_CFRelease)
_SecKeychainItemExport,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
_SecTrustCopyAnchorCertificates,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
_kCFAllocatorDefault,从引用:
在gohttplib.a _FetchPEMRoots(000003.o)
LD:符号(S)未找到x86_64的架构
铿锵:错误:连接命令,退出code 1(使用-v看看调用)失败
使:*** [例如-C]错误1
这是使用从OS X 10.9.5转到GitHub的库(38e3427)最近的版本。据我所知,围棋1.5尚未发布,而且也不能保证有关工作,但我这样做是为教育目的,我怀疑我失去了一些东西。
相关版本:
$ LD -v
@(#)计划:LD项目:ld64-241.9
配置为支持archs:ARMv6的ARMv7的armv7s arm64的i386 x86_64的x86_64h armv6m armv7m armv7em
LTO支持使用:LLVM版本3.5svn
$ GCC --version
配置有: - preFIX = /应用/ X code.app /内容/开发/ USR --with-GXX-包括-DIR = / usr / include目录/ C ++ / 4.2.1
(基于LLVM 3.5svn)苹果LLVM 6.0版(铛-600.0.57)
目标:x86_64的 - 苹果darwin13.4.0
线程模型:POSIX
原来在OSX /达尔文存在这个问题。要解决它,我们需要添加 -framework的CoreFoundation -framework安全
选项,gcc的链接命令。最后的命令是这样的:
$ gcc的-o gohttp-C的例子/ C / main.c中gohttplib.a \\
-framework的CoreFoundation -framework安全-lpthread
这个要求可能在围棋的未来版本中删除。这里就这一问题进行更多的讨论:
The upcoming release of Go 1.5 comes with new buildmodes which allow for exporting Go symbols to be linked and called from C code. I've been playing around with it and got basic "Hello world" examples working, but now I'm trying to link a Go library which starts a net/http.Server
and it's failing. The code looks like this (it's also available here):
gohttplib.go:
package main
import "C"
import "net/http"
//export ListenAndServe
func ListenAndServe(caddr *C.char) {
addr := C.GoString(caddr)
http.ListenAndServe(addr, nil)
}
func main() {}
examples/c/main.c:
#include <stdio.h>
#include "../../gohttplib.h"
int main()
{
ListenAndServe(":8000");
return 0;
}
Producing the statically-linked object and headers works fine:
$ go build -buildmode=c-archive
But compiling against it is failing:
$ gcc -o gohttp-c examples/c/main.c gohttplib.a -lpthread
Undefined 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)
"_SecKeychainItemExport", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
"_SecTrustCopyAnchorCertificates", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
"_kCFAllocatorDefault", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [example-c] Error 1
This is using a recent version from the Go github repository (38e3427) on OS X 10.9.5. I understand that Go 1.5 is not released yet and that there are no guarantees about it working, but I'm doing this for educational purposes and I suspect I'm missing something.
Related versions:
$ ld -v
@(#)PROGRAM:ld PROJECT:ld64-241.9
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7m armv7em
LTO support using: LLVM version 3.5svn
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Turns out this problem exists on OSX/darwin. To work around it, we need to add -framework CoreFoundation -framework Security
options to the gcc linking command. The final command looks like this:
$ gcc -o gohttp-c examples/c/main.c gohttplib.a \
-framework CoreFoundation -framework Security -lpthread
This requirement might be removed in a future version of Go. More discussion on this issue here: https://github.com/golang/go/issues/11258
这篇关于使用转到1.5 buildmode = C-归档净/ http.Server从C链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!