2015-06-17 21 views
7

L'imminente rilascio di Go 1.5 viene fornito con new buildmodes che consente l'esportazione dei simboli Go da collegare e chiamare dal codice C. Ho giocato con esso e ho ottenuto esempi base di "Hello world", ma ora sto provando a collegare una libreria Go che avvia uno net/http.Server e non funziona. Il codice simile a questo (it's also available here):Utilizzo di Go 1.5 buildmode = c-archive con net/http.Server collegato da C

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() {} 

esempi/c/main.c:

#include <stdio.h> 
#include "../../gohttplib.h" 

int main() 
{ 
    ListenAndServe(":8000"); 
    return 0; 
} 

Produrre l'oggetto collegata in modo statico e intestazioni funziona bene:

$ go build -buildmode=c-archive 

Ma compilazione contro di esso sta fallendo:

$ 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 

Questo sta usando una versione recente dal repository github Go (38e3427) su OS X 10.9.5. Capisco che Go 1.5 non è ancora stato rilasciato e che non ci sono garanzie sul fatto che funzioni, ma lo sto facendo a scopo didattico e sospetto che mi manchi qualcosa.

versioni correlati:

$ 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 
+0

Funziona per me sulla versione 'a2aaede' e Ubuntu 14.04. Hai provato ad aggiornare a una nuova versione di sviluppo? –

+0

Tirato 'a2aaede', lo stesso problema per me però. Potrebbe essere collegato a OSX in qualche modo, sebbene gcc-5 non funzioni in modo simile. – shazow

+0

Su questo, potrebbe valere la pena [aprire un problema] (https://github.com/golang/go/issues/new). –

risposta

7

scopre questo problema esiste su OSX/Darwin. Per ovviare a questo problema, è necessario aggiungere le opzioni -framework CoreFoundation -framework Security al comando di collegamento gcc. Il comando finale è simile al seguente:

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

Questo requisito potrebbe essere rimosso in una versione futura di Go. Ulteriori discussioni su questo tema qui: https://github.com/golang/go/issues/11258