前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang调用动态库so

Golang调用动态库so

作者头像
李海彬
发布2018-03-27 15:55:08
2.8K0
发布2018-03-27 15:55:08
举报
测试动态库

test_so.h

  1. int test_so_func(int a,int b);

test_so.c

#include "test_so.h"

int test_so_func(int a,int b)

{

    return a*b;

}

生成so

  1. gcc -shared ./test_so.c -o test_so.so

复制so文件到golang项目目录

golang项目目录,建立

load_so.h

  1. int do_test_so_func(int a,int b);

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;

}
复制代码
test.go
package main



/*

#include "load_so.h"

#cgo LDFLAGS: -ldl

*/

import "C"

import "fmt"



func main() {

    fmt.Println("20*30=", C.do_test_so_func(20, 30))

    fmt.Println("hello world")

}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-09-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档