使用cgo包装<dispatch dispatch.h =“”> </dispatch>时构建错误

时间:2013-07-04 19:49:07

标签: go

我正在玩XPC,GCD,但是当我的代码无法使用以下错误消息(我不明白)编译时,我遇到了一个快速的问题:

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,得到了以下输出:

➣ 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生成的$ WORK目录的内容:go-xpc_work.zip

我对编译和链接不太熟悉,因此我无法理解这个输出。我刚刚注意到_cgo_import.c由cgo生成的文件(带有选项-dynimport-dynout)在第一行中有#pragma cgo_import_dynamic _NSConcreteGlobalBlock _NSConcreteGlobalBlock "",这与链接器给出的错误消息有关。

希望这些信息有助于找出问题所在以及如何解决问题。

2 个答案:

答案 0 :(得分:0)

我对这些图书馆了解不多,但是没有任何内容可以通过你这里的代码向我跳出来。

可以从go build -x -work获取一些有用的调试输出,它将为您打印命令和工作目录。

工作目录将不受影响,因此您可以查看cgo为您生成的代码。加上它将为您打印的命令应该让您开始追踪问题。

答案 1 :(得分:0)

我在Cocoa应用程序中使用Go和XPC编写了一个包装器。

它位于:

https://github.com/aventurella/go-xpc