Using Go 1.5 buildmode = c-archive with net / http.Server related to C

In the upcoming release of Go 1.5, there are new buildmodes that allow you to export Go characters that need to be linked and called from C code. I play with it and get the basic "Hello world" examples, but now I'm trying to link the Go library that runs net/http.Server and it does not work. The code looks like this ( it is 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 of /C/main.c:

 #include <stdio.h> #include "../../gohttplib.h" int main() { ListenAndServe(":8000"); return 0; } 

Creating statically linked objects and headers works great:

 $ go build -buildmode=c-archive 

But compilation against it fails:

 $ 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 

The latest version is used from the Go github repository (38e3427) in OS X 10.9.5. I understand that Go 1.5 has not yet been released and that there are no guarantees that it works, but I do it for educational purposes, and I suspect that something is missing.

Similar 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 
+7
c linker go ld gccgo
source share
1 answer

Turns out this problem exists on OSX / darwin. To get around this, we need to add the -framework CoreFoundation -framework Security options to the gcc linking command. The last command looks like this:

 $ gcc -o gohttp-c examples/c/main.c gohttplib.a \ -framework CoreFoundation -framework Security -lpthread 

This requirement may be removed in a future version of Go. More discussion of this issue here: https://github.com/golang/go/issues/11258

+7
source

All Articles