为了账号安全,请及时绑定邮箱和手机立即绑定

从c调用go函数时出错

从c调用go函数时出错

Go
POPMUISE 2021-09-27 20:57:35
查看完整描述

2 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

新来这里。试图从 C 调用 go 函数但遇到了一些编译问题


这是 go 脚本


package main

// #cgo CFLAGS: -Wno-error=implicit-function-declaration

// #include <stdlib.h> 

// #include "wrapper.c"

import "C"

//import "unsafe"

import "fmt"

//import "time"


//export dummy

func dummy() int {

    fmt.Println("hi you");

    return 0

}


func main() {

    C.testc()

}

这是包装纸


#include <sys/types.h>

#include <stdio.h>

#include <errno.h>

#include <string.h>


extern int dummy();


void testc(){

    dummy();

}

运行时报错


xyz@xyz-HP:~/learn/go$ go run reader.go 

# command-line-arguments

In file included from $WORK/command-line-arguments/_obj/_cgo_export.c:2:0:

reader.go:30:14: error: conflicting types for ‘dummy’

In file included from reader.go:3:0,

                 from $WORK/command-line-arguments/_obj/_cgo_export.c:2:

./wrapper.c:6:12: note: previous declaration of ‘dummy’ was here

/tmp/go-build528677551/command-line-arguments/_obj/_cgo_export.c:8:7: error: conflicting types for ‘dummy’

In file included from reader.go:3:0,

                 from $WORK/command-line-arguments/_obj/_cgo_export.c:2:

./wrapper.c:6:12: note: previous declaration of ‘dummy’ was here


查看完整回答
反对 回复 2021-09-27
  • 2 回答
  • 0 关注
  • 234 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信