douguxun6866 2015-10-25 12:35
浏览 67
已采纳

用C接口构建Got +

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条回答 默认 最新

  • douwen5690 2015-10-25 12:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog