从Go的so文件中调用函数 [英] Calling functions in an so file from Go

查看:324
本文介绍了从Go的so文件中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Go调用静态对象(.so)文件? 我一直在Google搜寻,我一直在宣称我可以做到

Is it possible to call a static object (.so) file from Go? I've been searchign Google and I keep hitting upon the claim that I can do

lib, _ := syscall.LoadLibrary("...")

但是尝试这样做会出错

undefined: syscall.LoadLibrary

并通过Godocs搜索我无法在syscall包中找到对此功能的引用. 是否可以加载库并调用其函数?

and searching through Godocs I cannot find reference to this function in the syscall package. Is it possible to load a library and call its functions?

推荐答案

在POSIX平台上,您可以使用cgo调用

On a POSIX platform, you could use cgo to call dlopen and friends:

// #cgo LDFLAGS: -ldl
// #include <dlfcn.h>
import "C"

import fmt

func foo() {
     handle := C.dlopen(C.CString("libfoo.so"), C.RTLD_LAZY)
     bar := C.dlsym(handle, C.CString("bar"))
     fmt.Printf("bar is at %p\n", bar)
}

这篇关于从Go的so文件中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆