English 中文(简体)
Shared library in Go?
原标题:

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

UPDATED: created an "issue" for it.

最佳回答

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!

问题回答

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/

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

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





相关问题
How to combine shared libraries?

I ve got some .so libraries that I d like to combine into one shared library so that it doesn t depend on the original .so files anymore. The .so files have dependencies to each other. How can I do ...

Shared library in Go?

Is it possible to create a Shared Library (.so) using Go? UPDATED: created an "issue" for it.

Create a shared lib using another shared lib

I have a shared library "libwiston.so". I am using this to create another shared library called "libAnimation.so", which will be used by another project. Now, the second library "libAnimation.so" can ...

Load multiple copies of a shared library

I am running Linux, and I would like to be able to make parallel function calls into a shared library (.so) which is unfortunately not threadsafe (I am guessing it has global datastructures). For ...

热门标签