From 936d9e27c7c1d13e49e23776442357ea1849ef04 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 17 Jul 2011 14:03:47 -0700 Subject: [PATCH] cgo test dir Change-Id: Id4c17b85124c40b00e3c88b7d052c2158bfb79a5 --- misc/cgo/Makefile | 13 +++++++++++++ misc/cgo/callback.go | 22 ++++++++++++++++++++++ misc/cgo/callback_c.c | 4 ++++ misc/cgo/cstuff.h | 1 + misc/cgo/main.go | 6 ++++++ misc/cgo/use.go | 10 ++++++++++ 6 files changed, 56 insertions(+) create mode 100644 misc/cgo/Makefile create mode 100644 misc/cgo/callback.go create mode 100644 misc/cgo/callback_c.c create mode 100644 misc/cgo/cstuff.h create mode 100644 misc/cgo/main.go create mode 100644 misc/cgo/use.go diff --git a/misc/cgo/Makefile b/misc/cgo/Makefile new file mode 100644 index 000000000..9a0720f69 --- /dev/null +++ b/misc/cgo/Makefile @@ -0,0 +1,13 @@ +include $(GOROOT)/src/Make.inc + +TARG=camdev/ctest + +CGOFILES=\ + callback.go\ + +CGO_OFILES=\ + callback_c.o\ + +include $(GOROOT)/src/Make.pkg + + diff --git a/misc/cgo/callback.go b/misc/cgo/callback.go new file mode 100644 index 000000000..3d4a982a0 --- /dev/null +++ b/misc/cgo/callback.go @@ -0,0 +1,22 @@ +package ctest + +/* +#include + +#include "cstuff.h" +*/ +import "C" + +import "unsafe" + +//export GoFoo +func GoFoo(c *C.char) { + gstr := C.GoString(c) + println("I AM GO", gstr) +} + +func CallC(s string) { + cstr := C.CString(s) + defer C.free(unsafe.Pointer(cstr)) + C.CFoo(cstr) +} diff --git a/misc/cgo/callback_c.c b/misc/cgo/callback_c.c new file mode 100644 index 000000000..dcaa1c8bb --- /dev/null +++ b/misc/cgo/callback_c.c @@ -0,0 +1,4 @@ +int CFoo(const char* c) { + GoFoo(c); +} + diff --git a/misc/cgo/cstuff.h b/misc/cgo/cstuff.h new file mode 100644 index 000000000..3d9c3c010 --- /dev/null +++ b/misc/cgo/cstuff.h @@ -0,0 +1 @@ +extern void CFoo(const char* c); diff --git a/misc/cgo/main.go b/misc/cgo/main.go new file mode 100644 index 000000000..4c442b17c --- /dev/null +++ b/misc/cgo/main.go @@ -0,0 +1,6 @@ +package main + +func main() { + CallC() +} + diff --git a/misc/cgo/use.go b/misc/cgo/use.go new file mode 100644 index 000000000..bf4c18bc4 --- /dev/null +++ b/misc/cgo/use.go @@ -0,0 +1,10 @@ +package main + +import ( + "camdev/ctest" +) + +func main() { + ctest.CallC("foo") +} +