2013-07-04 67 views
2

我正在玩XPC,GCD,但是當我的代碼無法編譯時出現以下錯誤消息(我不喜歡它「不懂):使用cgo包裝時出現錯誤包裝<dispatch/dispatch.h>

main(__DATA/__const): unexpected reloc for dynamic symbol _NSConcreteGlobalBlock main(__DATA/__const): unhandled relocation for _NSConcreteGlobalBlock (type 28 rtype 120)

我使用go build下面的代碼編譯:

main.go

package main 

/* 
#include <xpc/xpc.h> 
#include "wrapper.h" 
*/ 
import "C" 

import (
    "fmt" 
) 

//export HandleXPCEvent 
func HandleXPCEvent(event C.xpc_object_t) { 
    fmt.Println("Event was handled") 
} 

func main() { 
    name := C.CString("com.example.xpc") 
    queue := C.dispatch_queue_create(name, nil) 
    conn := C.xpc_connection_create(name, queue) 
    C.set_event_handler(conn) 

    //C.xpc_connection_resume(conn) 
} 

wrapper.h

#ifndef _WRAPPER_H_ 
#define _WRAPPER_H_ 

#include <stdlib.h> 
#include <stdio.h> 
#include <xpc/xpc.h> 

xpc_connection_t connect(char* name); 
void set_event_handler(xpc_connection_t connection); 

#endif 

wrapper.c

#include "wrapper.h" 
#include <dispatch/dispatch.h> 

extern void HandleXPCEvent(xpc_object_t); 

xpc_connection_t connect(char* name) { 
    dispatch_queue_t queue = dispatch_queue_create(name,0); 
    return xpc_connection_create(name,queue); 
} 

void set_event_handler(xpc_connection_t connection) { 
    xpc_connection_set_event_handler(connection, ^(xpc_object_t event) { 
     xpc_retain(event); 
     // Call Go function 
     HandleXPCEvent(event); 
    }); 
} 

我做錯了什麼?這是一種錯誤還是如何解決?


更新:

我跑我的項目go build -x -work,我得到了以下的輸出:$ WORK目錄的

➣ go build -x -work 
WORK=/var/folders/fb/bgfqk8wx5x16w7yh2cg50vrw0000gn/T/go-build524335717 
mkdir -p $WORK/github.com/gabrielayuso/go-xpc/_obj/ 
mkdir -p $WORK/github.com/gabrielayuso/go-xpc/_obj/exe/ 
cd /Users/gabrielayuso/Documents/Workspace/Projects/go/src/github.com/gabrielayuso/go-xpc 
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/gabrielayuso/go-xpc/_obj/ -- -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ main.go 
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_defun.c 
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name 
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_main.o -c $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_main.c 
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_export.o -c $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_export.c 
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -o $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo2.o -c $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo2.c 
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -o $WORK/github.com/gabrielayuso/go-xpc/_obj/wrapper.o -c ./wrapper.c 
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_.o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_main.o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_export.o $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo2.o $WORK/github.com/gabrielayuso/go-xpc/_obj/wrapper.o 
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/gabrielayuso/go-xpc/_obj/ -dynimport $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_.o -dynout $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_import.c 
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/github.com/gabrielayuso/go-xpc/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_import.c 
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_all.o $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_export.o $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo2.o $WORK/github.com/gabrielayuso/go-xpc/_obj/wrapper.o -Wl,-r -nostdlib /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/x86_64/libgcc.a 
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/github.com/gabrielayuso/go-xpc/_obj/_go_.6 -p github.com/gabrielayuso/go-xpc -D _/Users/gabrielayuso/Documents/Workspace/Projects/go/src/github.com/gabrielayuso/go-xpc -I $WORK $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_gotypes.go $WORK/github.com/gabrielayuso/go-xpc/_obj/main.cgo1.go 
/usr/local/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/github.com/gabrielayuso/go-xpc.a $WORK/github.com/gabrielayuso/go-xpc/_obj/_go_.6 $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_import.6 $WORK/github.com/gabrielayuso/go-xpc/_obj/_cgo_defun.6 $WORK/github.com/gabrielayuso/go-xpc/_obj/_all.o 
cd . 
/usr/local/go/pkg/tool/darwin_amd64/6l -o $WORK/github.com/gabrielayuso/go-xpc/_obj/exe/a.out -L $WORK $WORK/github.com/gabrielayuso/go-xpc.a 
# github.com/gabrielayuso/go-xpc 
main(__DATA/__const): unexpected reloc for dynamic symbol _NSConcreteGlobalBlock 
main(__DATA/__const): unhandled relocation for _NSConcreteGlobalBlock (type 28 rtype 120) 

內容由go build -x -work爲產生:go-xpc_work.zip

我我不太熟悉編譯和鏈接,因此我無法理解這個輸出。我剛剛注意到由cgo生成的文件(選項-dynimport-dynout)在第一行中有#pragma cgo_import_dynamic _NSConcreteGlobalBlock _NSConcreteGlobalBlock "",它與鏈接器給出的錯誤消息有關。

希望這些信息可以幫助找出問題所在,以及如何解決問題。

+0

學習有關-x選項幫助,謝謝! –

回答

0

我對這些庫不太瞭解,但沒有用我們這裏的代碼跳出來。

一些有用的調試輸出可以從go build -x -work獲得,它將爲您打印命令和工作目錄。

工作目錄將保持不變,因此您可以查看cgo爲您生成的代碼。加上它將打印給你的命令應該讓你開始追蹤問題。

+0

感謝您的提示。我用'go build -x work'的輸出更新了問題的描述。不幸的是,我自己無法理解。 –