Using Go to build a shared library to be called from C

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Using http://gcc.gnu.org/onlinedocs/gccgo/Function-Names.html#Function-Names i've been able to make a shared library written in Go:
-----main.go------
package main

func Myfunc () int32 {
  return 1
}
------------------

it's linked like this:
gccgo -shared main.go -o libmain.so -lgcc

and is used by this C program:
-----main.c-----
#include <stdio.h>
extern int Myfunc () __asm__ ("go.main.Myfunc");

int main (int argc, char **argv)
{
  int tmp;
  printf ("Calling the function\n");
  tmp = Myfunc ();
  printf ("Called the function, got %d\n", tmp);
  return 0;
}
---------------

which is linked like this:
gcc -g -O0 main.c -o main -L. -lmain

and when i run it like that:
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./main

it works.

However, if i put anything more complex than "return 1" into Myfunc in Go (such as using fmt.Printf() or returning a string - with appropriate prototype changes to 'string' and 'char*' in Go and C, naturally), it segfaults at runtime.

Is that a limitation of gccgo, a bug, or am i simply doing something wrong?


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux