Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ponies: embed Go in C program #2790

Closed
gopherbot opened this issue Jan 26, 2012 · 26 comments
Closed

ponies: embed Go in C program #2790

gopherbot opened this issue Jan 26, 2012 · 26 comments
Milestone

Comments

@gopherbot
Copy link

by daniel@heroku.com:

Although it's already possible to link C code into a Go program (that controls the entry
point), it would be highly useful to me to have the opposite (running a Go runtime in a
C program).

I'm mostly interested in exposing exported Go symbols and bindings to Python (and Ruby,
Java, and similar), and PostgreSQL.  In the latter case, the sandboxed variant of Go
provides a more unique advantage as it can be run as a "trusted" language.
@rsc
Copy link
Contributor

rsc commented Jan 26, 2012

Comment 1:

Labels changed: added priority-later, removed priority-triage.

Status changed to LongTerm.

@gopherbot
Copy link
Author

Comment 2 by lewis@squareup.com:

I am interested in this as well.  I'd like to use Go to make PostgreSQL extensions among
other things.  It provides a unique balance between simplicity of scripting languages
and performance of a compiled language.
To me, go seems like it's ideal for implementing database features.

@gopherbot
Copy link
Author

Comment 4 by Szczur.nh:

I am interested in this as well. I want to use Go as script language in my app.

@remyoudompheng
Copy link
Contributor

Comment 5:

Elias Naur is working on this: https://golang.org/cl/6822078

@eliasnaur
Copy link
Contributor

Comment 6:

Updated implementation (and linux/amd64 implementation) at
https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/zmjXkGrEx6Q

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 7:

Labels changed: added go1.3maybe.

@gopherbot
Copy link
Author

Comment 8 by jwatte@imvu.com:

Go might be able to do something here. LUA is too simplistic, and the syntax is wonky.
V8 is too heavy-weight, and single-threaded. Python is single-threaded. A good embedding
interface could have significant value!

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 9:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 10:

Labels changed: added repo-main.

@gopherbot
Copy link
Author

Comment 11:

CL https://golang.org/cl/9738047 references this issue.

@keithkml
Copy link

keithkml commented Jan 8, 2015

I just wrote a blog post about this:

Using Go in mobile apps, Part 1: Calling Go functions from C++

@keithkml
Copy link

keithkml commented Jan 8, 2015

Seems like #256 is related and #4848 is a duplicate

@keithkml
Copy link

@ianlancetaylor Thanks for posting the document. It appears that adding -buildmode=archive would be the simplest of all these, right? And this would allow linking from C code and manually starting the Go runtime? I'm considering trying to implement this sometime soon. Is it on the official Go roadmap at all?

@ianlancetaylor
Copy link
Contributor

Thanks for your interest. You should coordinate with Michael Hudson-Doyle michael.hudson@canonical.com. He is the only person I know to be working in this area at the moment.

@asmaloney
Copy link
Contributor

Does anyone know if there has been any progress on any of these ideas?

I'm specifically interested in what's described in the doc @ianlancetaylor mentioned as Go code linked into, and called from, a non-Go program.

I have a large(ish) C++ app and I'd like to reimplement part of it in Go as a static lib and just link it in using my current build process.

@ianlancetaylor
Copy link
Contributor

Michael Hudson-Doyle has been working on shared libraries.

I plan to work on linking Go into a C program soon but I haven't started yet.

@mwhudson
Copy link
Contributor

Yes, I'm making progress on shared libraries, and also understanding how awkward doing dlopen-style embedding is going to be :-)

It doesn't feel like embedding Go as a static library should be all that hard? cgo_static_export the right things, avoid generating a entrypoint, generate a .o as for host linking and then don't invoke the host linker?

@mwhudson
Copy link
Contributor

Oh, I guess you have to worry about TLS a bit.

@asmaloney
Copy link
Contributor

Thank you @ianlancetaylor - I look forward to it! If you need some help testing when you get to that point please let me know.

@keithkml
Copy link

@asmaloney I had an idea about how to do this, though I haven't tried it out yet.

  1. Compile Go code into an executable normally with go build
  2. Run obcopy to rename main() to startGoRuntime()
  3. Rename the executable to myGoLib.o
  4. Link your C program against that .o file
  5. Call startGoRuntime() from your C code (in a new thread) whenever you need to start the Go runtime (can only do this once obviously)

This method has limitations – for example, I'm pretty sure C code could never call into Go code from a non-Go thread.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc removed the repo-main label Apr 14, 2015
@eaigner
Copy link
Contributor

eaigner commented Jun 16, 2015

Isn't this already implemented using the -buildmode flag? As far as I know it generates a header and a .a file, which should satisfy this issue.

@ianlancetaylor
Copy link
Contributor

Yes, this should now work on tip, using -buildmode=c-archive or -buildmode=c-shared. They don't work on all systems, but let's use separate issues to track specific systems where they need work.

@eatonphil
Copy link

Are there plans to support freebsd (if someone doesn't feel like using the linux compatability layer and 32-bit binaries)?

@ianlancetaylor
Copy link
Contributor

@eatonphil We try not to hold discussions on closed issues. See https://golang.org/wiki/Questions .

I would be happy to review patches adding support for FreeBSD. It shouldn't be hard. As I said above, you can also open a separate issue for that.

@eatonphil
Copy link

Sorry about that! I misread the "Open" sign above my comment as referring to this thread.

I'll open a new issue.

@golang golang locked and limited conversation to collaborators Feb 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests