public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Using Go to build a shared library to be called from C
@ 2011-06-13 21:02 LRN
  2011-06-14  6:32 ` Ian Lance Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: LRN @ 2011-06-13 21:02 UTC (permalink / raw)
  To: gcc-help

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?

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-10-27 19:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-13 21:02 Using Go to build a shared library to be called from C LRN
2011-06-14  6:32 ` Ian Lance Taylor
2011-06-14  9:54   ` LRN
2011-06-14 14:02     ` Ian Lance Taylor
2011-10-27 11:24   ` LRN
2011-10-27 19:38     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).