在Go中的共享库?

huangapple go评论98阅读模式
英文:

Shared library in Go?

问题

能否使用Go创建一个共享库(.so)?

更新:为此创建了一个“问题”。

英文:

Is it possible to create a Shared Library (.so) using Go?

UPDATED: created an "issue" for it.

答案1

得分: 49

现在可以使用-linkshared标志来实现这一点。

你需要做的是首先运行以下命令:

go install -buildmode=shared -linkshared std

(上述代码使所有常用包可共享!)
然后

go install -buildmode=shared -linkshared userownpackage

最后,在编译代码时需要运行:

go build -linkshared yourprogram

上述操作不再静态链接所有内容,而是动态链接它们,你将得到更小的编译文件。只是为了给你一个概念,我的“hello.go”文件在静态链接时为2.3MB,而使用动态链接的相同代码只有12KB!

英文:

This is possible now using -linkshared flag

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!

答案2

得分: 14

现在可以了!我使用Go构建了一个.so文件,然后很容易地导入到Python中!这是一篇我喜欢的文章:http://www.darkcoding.net/software/building-shared-libraries-in-go-part-1/

英文:

Possible now! I built a .so file using Go and then imported into python quite easily! Here is an articles that I liked: http://www.darkcoding.net/software/building-shared-libraries-in-go-part-1/

答案3

得分: 12

Go执行模式描述了将Go包构建为共享库

“在这种模式下,一个或多个Go包可以被构建为共享库。导入这些Go包的Go程序可以链接到这个共享库。共享库可以在Go程序链接和运行之间进行更改;程序启动时可用的共享库将被使用...

在Go 1.5版本中,这仅针对linux-amd64目标实现。在使用gccgo时,它适用于任何支持的目标。”

英文:

Go Execution Modes describes Building Go Packages as a shared library:

"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...

In the Go 1.5 release this is implemented for the linux-amd64 target only. When using gccgo it is implemented for any supported target."

答案4

得分: 4

显然,现在是可能的,但只在非常特定的情况下。更准确地说,如果你是为Android平台编写代码。

GitHub上的"goandroid"项目提供了一组补丁,允许Go构建一个用于Android NDK的特定用途的共享库。请参阅https://github.com/eliasnaur/goandroid

英文:

Apparently, it now is possible, but only under very precise set of circumstances. More precisely, if you're writing for the Android platform.

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. See https://github.com/eliasnaur/goandroid

huangapple
  • 本文由 发表于 2009年11月19日 00:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/1757090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定