与Gtk+的C接口构建Go;

与Gtk+的C接口构建Go;,go,compiler-errors,linker,cgo,Go,Compiler Errors,Linker,Cgo,我正在尝试构建一个Go程序,它使用外部C代码作为Gtk+的接口 这是我得到的基本Go代码(ui.h.Go): C代码从vala编译成一个对象文件(ui.o)和一个头文件(ui.h): 这是合乎逻辑的,我没有提供我的对象文件。但是如果我在ui.h.go的cgo头中这样指定它 //#cgo LDFLAGS: ui.o //#cgo pkg-config: gtk+-3.0 //#include "ui.h" import "C" 我得到多个定义错误,好像它被链接了两次 # command-line

我正在尝试构建一个Go程序,它使用外部C代码作为Gtk+的接口

这是我得到的基本Go代码(ui.h.Go):

C代码从vala编译成一个对象文件(ui.o)和一个头文件(ui.h):

这是合乎逻辑的,我没有提供我的对象文件。但是如果我在
ui.h.go
的cgo头中这样指定它

//#cgo LDFLAGS: ui.o
//#cgo pkg-config: gtk+-3.0
//#include "ui.h"
import "C"
我得到多个定义错误,好像它被链接了两次

# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
ui.o:(.bss+0x0): multiple definition of `window'
/tmp/go-link-461834384/000000.o:/home/oleg/Документы/Projects/rasp/ui.h.go:38: first defined here
ui.o: In function `ShowWindow':
ui.c:(.text+0x0): multiple definition of `ShowWindow'
/tmp/go-link-461834384/000000.o:(.text+0x25): first defined here
ui.o: In function `Init':
ui.c:(.text+0x29): multiple definition of `Init'
/tmp/go-link-461834384/000000.o:(.text+0x4e): first defined here
ui.o: In function `Main':
ui.c:(.text+0x116): multiple definition of `Main'
/tmp/go-link-461834384/000000.o:(.text+0x13b): first defined here
collect2: error: ld returned 1 exit status
如何将ui.o文件正确链接到Go程序?
谢谢。

嗯,我发现cgo与静态库的链接很好。因此,我决定将我的
ui.o
归档到
libui.a
中,并使用
#cgo LDFLAGS:-L.-lui
将其链接起来,它工作正常

# command-line-arguments
/tmp/go-build916459533/command-line-arguments/_obj/ui.h.cgo2.o: In function `_cgo_80fc53cbf347_Cfunc_Init':
./ui.h.go:37: undefined reference to `Init'
/tmp/go-build916459533/command-line-arguments/_obj/ui.h.cgo2.o: In function `_cgo_80fc53cbf347_Cfunc_Main':
./ui.h.go:46: undefined reference to `Main'
/tmp/go-build916459533/command-line-arguments/_obj/ui.h.cgo2.o: In function `_cgo_80fc53cbf347_Cfunc_ShowWindow':
./ui.h.go:55: undefined reference to `ShowWindow'
collect2: error: ld returned 1 exit status
//#cgo LDFLAGS: ui.o
//#cgo pkg-config: gtk+-3.0
//#include "ui.h"
import "C"
# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
ui.o:(.bss+0x0): multiple definition of `window'
/tmp/go-link-461834384/000000.o:/home/oleg/Документы/Projects/rasp/ui.h.go:38: first defined here
ui.o: In function `ShowWindow':
ui.c:(.text+0x0): multiple definition of `ShowWindow'
/tmp/go-link-461834384/000000.o:(.text+0x25): first defined here
ui.o: In function `Init':
ui.c:(.text+0x29): multiple definition of `Init'
/tmp/go-link-461834384/000000.o:(.text+0x4e): first defined here
ui.o: In function `Main':
ui.c:(.text+0x116): multiple definition of `Main'
/tmp/go-link-461834384/000000.o:(.text+0x13b): first defined here
collect2: error: ld returned 1 exit status