简体   繁体   English

Go中的共享库?

[英]Shared library in Go?

Is it possible to create a Shared Library (.so) using Go? 是否可以使用Go创建共享库(.so)?

UPDATED : created an " issue " for it. 更新 :为它创建了一个“ 问题 ”。

This is possible now using -linkshared flag 现在可以使用-linkshared标志

What you need to do is to first run this command: 您需要做的是首先运行此命令:

go install -buildmode=shared -linkshared  std

(Above code makes all common packages shareable!) then (上面的代码使所有常见的包可共享!)然后

go install  -buildmode=shared -linkshared userownpackage

finally when compiling your code you need to run: 最后在编译代码时需要运行:

go build -linkshared yourprogram

What the above those is now it rather than statically linking everything only dynamically links them and you will end up with much smaller compiled files. 以上是什么现在它而不是静态链接一切只动态链接它们,你将最终得到更小的编译文件。 Just to give you an idea my "hello.go" file with static linking is 2.3MB while the same code using dynamic linking is just 12KB! 只是为了给你一个想法,我的“hello.go”文件与静态链接是2.3MB,而使用动态链接的相同代码只有12KB!

Go Execution Modes describes Building Go Packages as a shared library : Go Execution ModesBuilding Go Packages描述为共享库

"In this mode a Go package, or set of packages, may be built as a shared library. A Go program that imports one or more of those Go packages may be linked against this shared library. The shared library may be changed between the time the Go program is linked and the time it is run; the shared library that is available when the program starts is the one that will be used... “在这种模式下,Go包或一组包可以构建为共享库。导入一个或多个Go包的Go程序可以链接到这个共享库。共享库可以在时间之间更改Go程序是链接的,它运行的时间;程序启动时可用的共享库是将要使用的共享库...

In the Go 1.5 release this is implemented for the linux-amd64 target only. 在Go 1.5版本中,这仅针对linux-amd64目标实现。 When using gccgo it is implemented for any supported target." 使用gccgo时,它是针对任何支持的目标实现的。“

Possible now! 现在可能! I built a .so file using Go and then imported into python quite easily! 我使用Go构建了一个.so文件,然后很容易导入到python中! Here is an articles that I liked: http://www.darkcoding.net/software/building-shared-libraries-in-go-part-1/ 这是我喜欢的文章: http//www.darkcoding.net/software/building-shared-libraries-in-go-part-1/

Apparently, it now is possible, but only under very precise set of circumstances. 显然,它现在是可能的,但只有在非常精确的情况下。 More precisely, if you're writing for the Android platform. 更确切地说,如果您正在为Android平台撰写文章。

The "goandroid" project on GitHub provides a set of patches that allows Go to build a shared library for specific use with the Android NDK. GitHub上的“goandroid”项目提供了一组补丁,允许Go构建一个共享库,以便与Android NDK一起使用。 See https://github.com/eliasnaur/goandroid 请参阅https://github.com/eliasnaur/goandroid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM