Note that there are some explanatory texts on larger screens.

plurals
  1. POGo shared library as C++ plugin
    text
    copied!<p>I have a project where I would like to load Go plugins inside a C++ application.</p> <p>After a lot of research, it is not clear for me whether or not Go supports this. I encountered a lot of discussions pointing out the bad habit of dynamic linking, proning IPC instead. Moreover it is not clear for me if dynamic linking is intended by the language or not (new Go philosophy ?).</p> <p>cgo provides the ability to call C from Go or Go from C (inside Go), but not from plain old C. Or does it ?</p> <ul> <li>gc doesn't seem to support shared library (even if <a href="https://code.google.com/p/go/issues/detail?id=256" rel="nofollow noreferrer">https://code.google.com/p/go/issues/detail?id=256</a> mentions it does) </li> <li>gccgo support Go shared libraries but I couldn't make it work (probably because the main entry point is not in Go ...)</li> <li>SWIG doesn't seem to help either :(</li> </ul> <p>Apparently something is going on upstream as well (<a href="https://codereview.appspot.com/7304104/" rel="nofollow noreferrer">https://codereview.appspot.com/7304104/</a>)</p> <p><strong>main.c</strong></p> <pre><code>extern void Print(void) __asm__ ("example.main.Print"); int main() { Print(); } </code></pre> <p><strong>print.go</strong></p> <pre><code>package main import "fmt" func Print() { fmt.Printf("hello, world\n") } </code></pre> <p><strong>Makefile :</strong></p> <pre><code>all: print.o main.c gcc main.c -L. -lprint -o main print.o: print.go gccgo -fno-split-stack -fgo-prefix=example -fPIC -c print.go -o print.o gccgo -shared print.o -o libprint.so </code></pre> <p><strong>Output :</strong></p> <pre><code>/usr/lib/libgo.so.3: undefined reference to `main.main' /usr/lib/libgo.so.3: undefined reference to `__go_init_main' </code></pre> <p>Is there a solution for that ? What is the best approach ? forking + IPC ?</p> <p><em>References :</em></p> <ul> <li><a href="https://code.google.com/p/go-wiki/wiki/cgo" rel="nofollow noreferrer">cgo - go wiki</a> </li> <li><a href="http://cheesesun.blogspot.com/2010/04/callbacks-in-cgo.html" rel="nofollow noreferrer">callback in cgo</a> </li> <li><a href="https://stackoverflow.com/questions/4312894/c-callbacks-and-non-go-threads">c callbacks and non go threads</a> </li> <li><a href="http://golang.org/doc/articles/c_go_cgo.html" rel="nofollow noreferrer">cgo - golang</a> </li> <li><a href="https://stackoverflow.com/questions/6125683/call-go-functions-from-c">call go function from c</a></li> <li><a href="http://cxwangyi.wordpress.com/2011/03/28/statically-linking-c-code-with-go-code/" rel="nofollow noreferrer">link c code with go code</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload