php调用golang so,CGO golang 调用c so文件的方式

同事用c++写的so 文件做php组件调用加解密,由于个人不会c++却会golang

所以一直考虑通过go调用so文件今天花了点时间终于跑通了记录一下

提醒:目前Go的版本不支持链接静态共享库 c文件要进行动态共享

foo.go

==================================

// #cgo LDFLAGS: -L ./ -lfoo

// #include

// #include

// #include "foo.h"

import "C"

import "fmt“

func main() {

fmt.Println(C.count)

C.foo()

}

我们看到上面例子中通过#cgo指示符告诉go编译器链接当前目录下的libfoo共享库。C.count变量和C.foo函数的定义都在libfoo共享库中。我们来创建这个共享库:

=======================

foo.h

int count;

void foo();

=========================

foo.c

#include "foo.h"

int count = 6;

void foo() {

printf("I am

foo!\n");

}

我们首先创建一个静态共享库libfoo.a,不过在编译Go源文件时我们遇到了问题:

$> go build foo.go

# command-line-arguments

/tmp/go-build565913544/command-line-arguments.a(foo.cgo2.)(.text):

foo: not defined

foo(0): not defined

那我们来创建一个动态共享库试试:

>gcc -c foo.c

>gcc -o libfoo.so -O2 -fPIC -shared

foo.c

>go build foo.go

>./foo

./foo: error while loading shared libraries: libfoo.so:

cannot open shared obje

找不到libfoo.so文件

>cp libfoo.so /usr/lib/

>./foo

6

I am foo!

以上为实现步骤

​考虑调用so 文件写了以下步骤

​test so

测试动态库

test_so.h

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

​gcc -shared ./test_so.c -o

test_so.so

golang项目目录,建立

==================

load_so.h

int do_test_so_func(int a,int b);

=============

load_so.c

#include "load_so.h"

#include

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;

}

================

package main

import "C"

import "fmt"

func main() {

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

}

===================

​> gcc -shared

./test_so.c -o test_so.so

> gcc -o libload_so.so -O2 -fPIC -shared

load_so.c -ldl

-ldl 为编译参数不然的话go执行会出错

.//libload_so.so: undefined reference to `dlopen'

.//libload_so.so: undefined reference to `dlsym'

>./test

20*30= 600

参考文档

http://www.cnblogs.com/ziyouchutuwenwu/p/3657285.html​

http://tonybai.com/2012/09/26/interoperability-between-go-and-c/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值