3

I have a project where I would like to load Go plugins inside a C++ application.

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 ?).

cgo provides the ability to call C from Go or Go from C (inside Go), but not from plain old C. Or does it ?

  • gc doesn't seem to support shared library (even if https://code.google.com/p/go/issues/detail?id=256 mentions it does)
  • gccgo support Go shared libraries but I couldn't make it work (probably because the main entry point is not in Go ...)
  • SWIG doesn't seem to help either :(

Apparently something is going on upstream as well (https://codereview.appspot.com/7304104/)

main.c

extern void Print(void) __asm__ ("example.main.Print");

int main() {
        Print();
}

print.go

package main

import "fmt"

func Print() {
    fmt.Printf("hello, world\n")
}

Makefile :

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

Output :

/usr/lib/libgo.so.3: undefined reference to `main.main'
/usr/lib/libgo.so.3: undefined reference to `__go_init_main'

Is there a solution for that ? What is the best approach ? forking + IPC ?

References :

1
  • The recommend solution is to let your main program start your Go-plugin as an external process that is connected to your main program with a pipe. Much less of a hassle and much less of a security risk
    – fuz
    Commented May 29, 2013 at 17:10

2 Answers 2

7

I don't think you can embed Go into C. However you can embed C into Go and with a little stub C program you can call into C first thing which is the next best thing! Cgo definitely supports linking with shared libraries so maybe this approach will work for you.

Like this

main.go

// Stub go program to call cmain() in C
package main

// extern int cmain(void);
import "C"

func main() {
     C.cmain()
}

main.c

#include <stdio.h>

// Defined in Go
extern void Print(void);

// C Main program
int cmain() {
  printf("Hello from C\n");
  Print();
}

print.go

package main

import "fmt"

import "C"

//export Print
func Print() {
    fmt.Printf("Hello from Go\n")
}

Compile with go build, and produces this output when you run it

Hello from C
Hello from Go
2
  • Thanks for the answer ! However, I assume that the C++ runtime is started first without any plugin loaded. Then, I load Go plugins as needed (component configurator pattern). According to your example I'm not sure that calling C from Go is going to help me.
    – 3XX0
    Commented May 29, 2013 at 6:53
  • The above answer was intended to show you a way of making the code in the question work. Alas it isn't a solution for using Go plugins to extend C++. Go doesn't make shared objects (yet)- it is statically linked, so if you want to be able to plug in Go code at runtime, you'll have to go with subprocesses and IPC. If you are happy with a static set of plugins pre-compiled in then you might get the above technique to work with C++ but you'll need a bit of unknown extra magic to start the C++ runtime. Commented May 29, 2013 at 7:07
2

AFAIK, you cannot compile a Go package to a shared library witg 'gc' ATM, it may change in the future. There might be some chance with 'gccgo' as 'libgo' (Go runtime, I suppose) is a shared library already. I guess the missing piece is then only to properly initialize the runtime, which normally a Go command handles automatically.

The gccgo expert is I.L. Taylor, he is reachable at the golang-nuts mailing list almost daily. I suggest to ask him directly.

PS: Other problems possibly include the interaction of the Go garbage collector and the (C++) process memory etc. Maybe I'm too optimistic and it is not at all feasible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.