英文:
Building Go with C interface to Gtk+
问题
我正在尝试构建一个使用外部C代码作为Gtk+接口的Go程序。
这是我得到的基本Go代码(ui.h.go):
package main
//#cgo pkg-config: gtk+-3.0
//#include "ui.h"
import "C"
func CInit() {
C.Init(nil, 0)
}
func CMain() {
C.Main()
}
func CShowWindow() {
C.ShowWindow()
}
func main() {
CInit()
CShowWindow()
CMain()
}
C代码从Vala编译成一个目标文件(ui.o)和一个头文件(ui.h):
#ifndef __UI_H__
#define __UI_H__
#include <glib.h>
#include <stdlib.h>
#include <string.h>
G_BEGIN_DECLS
void ShowWindow(void);
void Init(gchar** args, int args_length1);
void Main(void);
G_END_DECLS
#endif
当我尝试运行go build ui.h.go
时,我得到以下错误:
# 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
这是合理的,因为我没有提供我的目标文件。但是,如果我在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程序中?
谢谢。
英文:
I'm trying to build a Go programm which uses external C code as an interface for Gtk+.
That's the basic Go code I've got (ui.h.go):
package main
//#cgo pkg-config: gtk+-3.0
//#include "ui.h"
import "C"
func CInit() {
C.Init(nil, 0)
}
func CMain() {
C.Main()
}
func CShowWindow() {
C.ShowWindow()
}
func main() {
CInit()
CShowWindow()
CMain()
}
C code is compiled from vala into an object file (ui.o) and a header file (ui.h):
#ifndef __UI_H__
#define __UI_H__
#include <glib.h>
#include <stdlib.h>
#include <string.h>
G_BEGIN_DECLS
void ShowWindow (void);
void Init (gchar** args, int args_length1);
void Main (void);
G_END_DECLS
#endif
When I try go build ui.h.go
I get:
# 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
Which is logical, I haven't provided my object file. But if I specify it in cgo header of ui.h.go
like that...
//#cgo LDFLAGS: ui.o
//#cgo pkg-config: gtk+-3.0
//#include "ui.h"
import "C"
I get multiple definition error, as if it's being linked twice.
# 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
How do I link my ui.o file to the Go program correctly?
Thank you.
答案1
得分: 2
嗯,我发现cgo与静态库链接得很好。所以我决定将我的ui.o
归档为libui.a
,并使用#cgo LDFLAGS: -L. -lui
进行链接,结果是正确的。
英文:
Well, I figured out that cgo does link well with static libraries. So I decided to archive my ui.o
into libui.a
and link it using #cgo LDFLAGS: -L. -lui
and it worked correctly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论