Browse Source

feat: add go-ffi demo

master
Dnomd343 4 weeks ago
parent
commit
0ce098033b
  1. 7
      src/core_ffi/go_ffi/demo.c
  2. 7
      src/core_ffi/go_ffi/demo.cc
  3. 11
      src/core_ffi/go_ffi/demo.h
  4. 13
      src/core_ffi/go_ffi/klotski.go
  5. 7
      src/core_ffi/go_ffi/test_c.sh
  6. 7
      src/core_ffi/go_ffi/test_cpp.sh

7
src/core_ffi/go_ffi/demo.c

@ -0,0 +1,7 @@
#include "demo.h"
#include <stdio.h>
void hello() {
printf("hello from c\n");
}

7
src/core_ffi/go_ffi/demo.cc

@ -0,0 +1,7 @@
#include "demo.h"
#include <iostream>
void hello() {
std::cout << "hello from c++" << std::endl;
}

11
src/core_ffi/go_ffi/demo.h

@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void hello();
#ifdef __cplusplus
}
#endif

13
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()
}

7
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

7
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
Loading…
Cancel
Save