From 0ce098033bc4d63399a068aa78ce8f164798d750 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 1 Dec 2024 18:21:40 +0800 Subject: [PATCH] feat: add go-ffi demo --- src/core_ffi/go_ffi/demo.c | 7 +++++++ src/core_ffi/go_ffi/demo.cc | 7 +++++++ src/core_ffi/go_ffi/demo.h | 11 +++++++++++ src/core_ffi/go_ffi/klotski.go | 13 +++++++++++++ src/core_ffi/go_ffi/test_c.sh | 7 +++++++ src/core_ffi/go_ffi/test_cpp.sh | 7 +++++++ 6 files changed, 52 insertions(+) create mode 100644 src/core_ffi/go_ffi/demo.c create mode 100644 src/core_ffi/go_ffi/demo.cc create mode 100644 src/core_ffi/go_ffi/demo.h create mode 100644 src/core_ffi/go_ffi/klotski.go create mode 100755 src/core_ffi/go_ffi/test_c.sh create mode 100755 src/core_ffi/go_ffi/test_cpp.sh diff --git a/src/core_ffi/go_ffi/demo.c b/src/core_ffi/go_ffi/demo.c new file mode 100644 index 0000000..1964cae --- /dev/null +++ b/src/core_ffi/go_ffi/demo.c @@ -0,0 +1,7 @@ +#include "demo.h" + +#include + +void hello() { + printf("hello from c\n"); +} diff --git a/src/core_ffi/go_ffi/demo.cc b/src/core_ffi/go_ffi/demo.cc new file mode 100644 index 0000000..228e362 --- /dev/null +++ b/src/core_ffi/go_ffi/demo.cc @@ -0,0 +1,7 @@ +#include "demo.h" + +#include + +void hello() { + std::cout << "hello from c++" << std::endl; +} diff --git a/src/core_ffi/go_ffi/demo.h b/src/core_ffi/go_ffi/demo.h new file mode 100644 index 0000000..a5889f0 --- /dev/null +++ b/src/core_ffi/go_ffi/demo.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +void hello(); + +#ifdef __cplusplus +} +#endif diff --git a/src/core_ffi/go_ffi/klotski.go b/src/core_ffi/go_ffi/klotski.go new file mode 100644 index 0000000..033b78d --- /dev/null +++ b/src/core_ffi/go_ffi/klotski.go @@ -0,0 +1,13 @@ +package main + +/* +#cgo LDFLAGS: -L${SRCDIR}/ -ldemo -lstdc++ +#include "demo.h" +*/ +import "C" +import "fmt" + +func main() { + fmt.Println("hello from go") + C.hello() +} diff --git a/src/core_ffi/go_ffi/test_c.sh b/src/core_ffi/go_ffi/test_c.sh new file mode 100755 index 0000000..4cb3030 --- /dev/null +++ b/src/core_ffi/go_ffi/test_c.sh @@ -0,0 +1,7 @@ +gcc -O3 -c demo.c -o demo.o +ar rcs libdemo.a demo.o +rm -f demo.o +ranlib libdemo.a + +go run klotski.go +rm -f libdemo.a diff --git a/src/core_ffi/go_ffi/test_cpp.sh b/src/core_ffi/go_ffi/test_cpp.sh new file mode 100755 index 0000000..822a912 --- /dev/null +++ b/src/core_ffi/go_ffi/test_cpp.sh @@ -0,0 +1,7 @@ +g++ -O3 -c demo.cc -o demo.o +ar rcs libdemo.a demo.o +rm -f demo.o +ranlib libdemo.a + +go run klotski.go +rm -f libdemo.a