44

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

UPDATED: created an "issue" for it.

hannson
  • 4,465
  • 8
  • 38
  • 46
jldupont
  • 93,734
  • 56
  • 203
  • 318
  • If you want to support dynamic loading (if you make changes in the library, and hence the .so file but you want the main code using this library to automatically use the newer version, without recompiling the main code), Go Plugins may be preferable. https://golang.org/pkg/plugin/ – Meet Sinojia Mar 24 '20 at 09:58

4 Answers4

49

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!

Reza
  • 1,478
  • 1
  • 15
  • 25
  • 8
    this is what I was looking for. But gives `-buildmode=shared not supported on darwin/amd64` on my mac. – rezam May 06 '17 at 16:40
  • I was able to create a .so perfectly, but how to get the methods of my program into that .so? Also my program name is sample.go and it's package is also of the same name, it creates a .so named as libcommand-line-arguments.so – immukul Sep 04 '20 at 04:32
14

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/

Ahsan
  • 3,845
  • 2
  • 36
  • 36
12

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

mozey
  • 2,222
  • 2
  • 27
  • 34
4

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

  • 1
    As-of-now (July 2013), it's an extension to the Go Tools and not built into the Go Tools that Google ships. – nsg Jul 13 '13 at 04:02
  • @Nate can you link to the extension? Is the (stand alone) extension usable only on android or is it available for desktop use too? – Mihai Stancu Jul 08 '15 at 19:57
  • @mihaistancu I imagine it wouldn't be too difficult to cross-compile go for android, see [here](https://jasonplayne.com/programming-2/how-to-cross-compile-golang-for-android). Could get official support soon, considering golang 1.5 supports iOS. – nsg Jul 09 '15 at 21:37
  • @Nate -- my question was: "does that extension work **only** on android or can I (currently) compile desktop go packages as shared objects?". – Mihai Stancu Jul 09 '15 at 22:31
  • @Nate So I'm not interested to compile for android at all. I just want to compile shared objects for desktop. I've researched the subject myself and apparently there is some compiler support with `go build -shared` flags. – Mihai Stancu Jul 09 '15 at 22:34
  • @MihaiStancu interesting, sorry I misunderstood your question. – nsg Jul 10 '15 at 14:19