You can't do it, not today and possibly never.
The biggest problem is the fact that Go has its own runtime, namely a
garbage collector and a scheduler (for go routines) and segmented
stacks that do not play well with C stacks. It's much more complicated
than linking the executable code and translating between calling
conventions (e.g. what happens to a goroutine created in Go code you
called from C after Go code returns to C?)
Now, it's not impossible to overcome this e.g. V8 or Microsoft's .NET
have embedding APIs that allow for decent way of calling
JavaScript/CLR code from C/C++ code, but that doesn't exist for any
current implementation of Go (as far as I know) and doing it right is
significant amount of work.
-- kjk
On Mon, Oct 15, 2012 at 9:56 AM, Jens Alfke <
je...@mooseyard.com> wrote:
> [This seems like it ought to be an FAQ, but I haven't seen it in any of the
> documentation, or in a cursory search of this list.]
>
> Some of the uses I'd like to put Go to involve writing a library in it,
> which would be statically linked into and called from traditional
> C/C++/Objective-C applications (primarily iOS / Mac OS. Is this feasible?
> I've only seen documentation about going the other direction, linking C
> libraries into a Go binary.
>
> When the Go compiler builds packages it creates .a files (on Darwin), but I
> haven't had any luck examining their contents with otool — I'm guessing that
> their contents aren't in a format that the regular Darwin toolchain
> understands. Either that or I just suck at otool, which is also plausible.
> :) I also understand that the Go calling conventions differ from C, so
> something CGo-like would have to be done to make entry points into the
> package that can be called from C code.
>
> --Jens
>
> --
>
>