Build static library with Go

1,661 views
Skip to first unread message

Erik Aigner

unread,
Mar 28, 2014, 5:49:39 AM3/28/14
to golan...@googlegroups.com
Is it - or will it ever be - possible to build a static library with Go?

I don't know if it's technically possible, but I'd like to rewrite parts
of C code in Go and link it as a static library.

Konstantin Khomoutov

unread,
Mar 28, 2014, 6:13:27 AM3/28/14
to Erik Aigner, golan...@googlegroups.com
No. The "problem" is that Go code assumes the presence of certain
"environment" which is richer than that of C. Suppose that the library
you want existed, and you call from your C code a function from it
(let's ignore differences in calling convention for a moment), and that
function spawns a goroutine -- what should happen? In a Go program,
the goroutine scheduler kicks in and makes sure everything ticks as
expected. The same problem applies to garbage collector: say, you
finished executing your Go function and is about to return back to
C code -- what to do with the garbage created? I'm only scratching the
surface accessible for mere mortals like me, but there exist advanced
things like segmented (or growable, since 1.3) stacks which are used by
goroutines but which have nothing to do with "normal" C stacks.
In other words, calling C from Go is possible (via syscalls and Cgo)
but the reverse is not quite true (there's limited support for
callbacks from C back to Go). The reason: Go advanced runtime must be
there in order for Go code to work.

What you can do therefore is to either rewrite your project in a way
so that Go calls parts written in C, not vice-versa, or turn your Go
library to a program, start it from your C program and do some sort of
IPC between them (sockets, pipes, shared memory etc).

Ukiah Danger Smith

unread,
Mar 28, 2014, 6:43:45 AM3/28/14
to golan...@googlegroups.com
Wouldn't the Go runtime itself be another shared lib that other Go shared libs would require?

Are there any benefits of shared over static?

Message has been deleted

Konstantin Khomoutov

unread,
Mar 28, 2014, 7:29:09 AM3/28/14
to Ukiah Danger Smith, golan...@googlegroups.com
On Fri, 28 Mar 2014 03:43:45 -0700 (PDT)
Ukiah Danger Smith <super...@walledcity.com> wrote:

> Wouldn't the Go runtime itself be another shared lib that other Go
> shared libs would require?

"The trick" here is that the Go runtime which is required for Go code
to run is not a static bunch of callable functions, -- instead, it's
a *live* environment which schedules goroutines, controls system calls
and periodically performs garbage collection (which currently requires
"stopping the world").

IOW, *may be* that on certain platforms it might be possible to tweak
Go to compile its runtime to produce a shared library which would
provide instantiating some "context" and returning a handle on it so
that the client code would subsequently be able to somehow call into
that context. In either case that "context" should run on a separate
thread (and be able to create other threads), and calls to it should be
somehow "gated" in and out. I'm not sure it's doable, and in any case
I did not heard of any interest to implement something like this from
those who are actually able to do that. And honestly, I'd prefer those
people rather working on the Go core. ;-)

Matt Harden

unread,
Mar 28, 2014, 11:11:24 PM3/28/14
to Konstantin Khomoutov, Ukiah Danger Smith, golang-nuts
It may be possible using gccgo.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

minux

unread,
Mar 28, 2014, 11:50:46 PM3/28/14
to Erik Aigner, golang-nuts

it's also possible for the gc toolchain (with gccgo, it's obviously possible).

for example, by using the .syso mechanism, you can include arbitrary object files into a go program. including all C/C++ source files with a cgo package is also a possible way.

i'm replying from phone, so i can't add an example here, but i think the syso trick has been explained multiple times on the mailing list.

minux

unread,
Mar 28, 2014, 11:56:32 PM3/28/14
to Erik Aigner, golang-nuts


On Mar 28, 2014 11:50 PM, "minux" <minu...@gmail.com> wrote:
> On Mar 28, 2014 5:49 AM, "Erik Aigner" <aigne...@gmail.com> wrote:
> > Is it - or will it ever be - possible to build a static library with Go?

oops, i misread your message.

if you want to build a static library, possible, but you won't be able to link library to another program due to the runtime problem others have explained.

when doing external linking, the gc toolchain actually builds a relocatable object file, but all you can do is to link it with cgo object files to produce a final executable.

what are you really trying to do?

if just statically linking C code, use cgo or syso, but if you want to embed go in, say, C program, that's not going to work at least for the current implementations.

Brendan Tracey

unread,
Mar 29, 2014, 1:32:41 PM3/29/14
to golan...@googlegroups.com, Erik Aigner
It may be in the future. Issue 2790 is marked as "long term" code.google.com/p/go/issues/detail?id=2790

Erik Aigner

unread,
Mar 30, 2014, 1:34:05 PM3/30/14
to golan...@googlegroups.com, Erik Aigner

what are you really trying to do?

I want to write code for data processing in Go and link it statically with an app on iOS.

minux

unread,
Mar 30, 2014, 1:39:51 PM3/30/14
to Erik Aigner, golang-nuts
On Sun, Mar 30, 2014 at 1:34 PM, Erik Aigner <aigne...@gmail.com> wrote:

what are you really trying to do?

I want to write code for data processing in Go and link it statically with an app on iOS.
Officially, Go doesn't support iOS yet, so I'd not worry about static linking first.
When we have the ability to build a shared library for a Go application, building static
library should also be easy enough (actually it's mainly a runtime change, the linker
is already capable of generating relocating object files).
Reply all
Reply to author
Forward
0 new messages