zhangzhihai98 2015-09-29 14:52 采纳率: 0%
浏览 3042

go调用c++ so问题 cgo

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的经验

  • 写回答

2条回答

  • devmiao 2015-09-29 15:54
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题