doudou0111 2016-02-12 05:18 采纳率: 0%
浏览 19

cgo:从位于某个程序包中的C函数调用另一个函数

I am trying to follow the guidelines to organize my code with packages and C files located in these directories should be detected and compiled by the tool. However, when I try to reference a function from another package (e.g. calling bar from foo), I am getting the following error:

ld: symbol(s) not found for architecture x86_64

Directories and files:

test/main.go

test/bar:
bar.c  bar.go bar.h

test/foo:
foo.c  foo.go foo.h

bar.h:

extern void bar();

bar.c:

#include <stdio.h>

void bar() {
  printf("bye
");
}

bar.go:

package bar

//#include "bar.h"
import "C"

func Bar() {
    C.bar()
}

foo.h:

extern void foo();

foo.c:

#include <stdio.h>

#include "../bar/bar.h"

void foo() {
  printf("hello
");
  bar();
}

foo.go:

package foo

//include "foo.h"
//include "../bar/bar.h"
import "C"

func Foo() {
    C.foo()
}

main.go:

package main

import "github.com/user/test/foo"
import "github.com/user/test/bar"


func main() {
  foo.Foo()
  bar.Bar()
}

And the output:

cd test ; go build     
# github.com/user/test/foo
Undefined symbols for architecture x86_64:
  "_bar", referenced from:
      _foo in foo.o
ld: symbol(s) not found for architecture x86_64
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥170 如图所示配置eNSP
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上