test.go
package main
/*
#include "load_so.h"
#cgo LDFLAGS: -L ./ -lload_so
*/
import "C"
import "fmt"
func main() {
fmt.Println("20*30=", C.do_test_so_func(20, 30))
fmt.Println("20+30=", C.dd_test_so_func(20,30))
}
load_so.c
#include "load_so.h"
#include "dlfcn.h"
int do_test_so_func(int a,int b)
{
void* handle;
typedef int (*FPTR)(int,int);
handle = dlopen("./test_so.so", 1);
FPTR fptr = (FPTR)dlsym(handle, "test_so_func");
int result = (*fptr)(a,b);
return result;
}
int dd_test_so_func(int a,int b)
{
void* handle;
typedef int (*FPTR)(int,int);
handle = dlopen("./test_so.so", 1);
FPTR fptr = (FPTR)dlsym(handle, "dd_so_func");
int result = (*fptr)(a,b);
return result;
}
test_so.c
#include "test_so.h"
int test_so_func(int a,int b)
{
return a*b;
}
int dd_so_func(int a,int b){
return a+b;
}
gcc -shared ./test_so.c -o test_so.so
gcc -o libload_so.so -O2 -fPIC -shared load_so.c -ldl
gcc 可以通过
g++不能通过
请有过go 调用c++ so的经验