cgo

package main

import (
"unsafe"
"fmt"
)


/*
#cgo CFLAGS: -I./
#cgo LDFLAGS: -L./
#include <test.h>
*/
import "C"

//export println
func println(str *C.char) {
fmt.Println(C.GoString(str))
}

//export callback
func callback(ptr unsafe.Pointer) {
f := *(*func(str *C.char))(ptr)
f(C.CString("Hello,This from Golang"))
}

func main() {
var fn = println
C.test(unsafe.Pointer(&fn))
}

/////////////////////////////////

to install TDM-GCC-64

go build

/////////////////test.c///////////
#include <test.h>
int test(void* fn)
{
callback(fn);
println("Hello,This from Clang");
return 0;
}

/////////////test.h////////////

int test(void* fn);
void println(char* str);
void callback(void* fn);
posted @ 2017-08-14 12:34  飞晨信息  阅读(449)  评论(0编辑  收藏  举报