golang 调用C 遇到问题

weixin_42639295 2019-02-28 10:55:33
在尝试用 cgo的时候,
无法调用c中的random函数
请大神帮忙看下
package main
// #include <stdio.h>
// #include <stdlib.h>
import "C"
import "fmt"

func main() {
fmt.Println(C.random(123))
}

报错信息是调用函数的那行
could not determine kind of name for C.random
...全文
812 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2019-03-01
  • 打赏
  • 举报
回复
大概看了一下cgo的介绍,感觉要自己进行c编译,再link
你可以看下https://github.com/yugui/cgo-explained/tree/master/example1 的Makefile
大概是 go tool cgo import_example.go 生成c的代码
然后再调用gcc编译c代码
然后再go tool cgo -dynimport 生成动态import
然后再go tool compile -o example1.a -pack GO_FILES 生成动态库
然后再go tool pack r example1.a _all.o 动态库打包
然后再go tool link -o example example1.a 链接

如果没有这些步骤,估计你直接go build行不通






2,348

社区成员

发帖
与我相关
我的任务
社区描述
go语言学习与交流版
社区管理员
  • go语言社区
  • 俺叫西西弗斯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧