python3调用cpp的方法——python调用so

10 篇文章 0 订阅

python中使用ctypes模块可以在python中直接调用C/C++。
首先要将C/C++编译成动态库(.so),之后python中调用即可

特别注意在调用C++函数需要在函数声明时,加入前缀“ extern “C” ”,这是由于C++支持函数重载功能,在编译时会更改函数名。在函数声明时,前缀extern "C"则确保按C的方式编译。

值得注意的是,一定要有函数输入输出类型的声明,int型不用转换,float和double类型需要进行转换,
ctypes中的变量类型与C中对应如下:

ctypes数据类型C数据类型
c_charchar
c_shortshort
c_intint
c_longlong
c_floatfloat
c_doubledouble
c_void_pvoid
c_uint8unsigned char

使用步骤:

1、编写c++代码
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <stdio.h>


class Test{
    private:
        double _calculate(int a, double b);
    public:
        double calculate(int a, double b, char c[], int * d, double * e, char ** f);
};

double Test::_calculate(int a, double b){
    double res = a+b;
    std::cout<<"res: "<<res<<std::endl;
    return res;
}

double Test::calculate(int a, double b, char c[], int * d, double * e, char ** f){
    std::cout<<"a: "<<a<<std::endl;
    std::cout<<"b: "<<b<<std::endl;
    std::cout<<"c: "<<c<<std::endl;
    std::cout<<"d: "<<d[0]<<d[1]<<std::endl;
    std::cout<<"e: "<<e[0]<<e[1]<<std::endl;
    std::cout<<"f: "<<f[0]<<f[1]<<std::endl;
    return this->_calculate(a, b);
}


// 封装C接口
extern "C"{
// 创建对象
    Test* test_new(){
        return new Test;
    }
    double my_calculate(Test* t, int a, double b, char c[], int * d, double * e, char ** f){
        return t->calculate(a, b,c,d,e,f);
    }
}
2、将cpp编译成so
g++ -shared -Wl,-soname,test -o test.so -fPIC test.cpp

其中test为cpp的名称

3、使用python调用即可
# -*- coding: utf-8 -*-
import ctypes
# 指定动态链接库
lib = ctypes.cdll.LoadLibrary('./test.so')
#需要指定返回值的类型,默认是int
lib.my_calculate.restype = ctypes.c_double

class Test(object):
    def __init__(self):
        # 动态链接对象
        self.obj = lib.test_new()

    def calculate(self, a, b,c,d,e,f):
        res = lib.my_calculate(self.obj, a, b,c,d,e,f)
        return res

#将python类型转换成c类型,支持int, float,string的变量和数组的转换
def convert_type(input):
    ctypes_map = {int:ctypes.c_int,
              float:ctypes.c_double,
              str:ctypes.c_char_p
              }
    input_type = type(input)
    if input_type is list:
        length = len(input)
        if length==0:
            print("convert type failed...input is "+input)
            return null
        else:
            arr = (ctypes_map[type(input[0])] * length)()
            for i in range(length):
                arr[i] = bytes(input[i],encoding="utf-8") if (type(input[0]) is str) else input[i]
            return arr
    else:
        if input_type in ctypes_map:
            return ctypes_map[input_type](bytes(input,encoding="utf-8") if type(input) is str else input)
        else:
            print("convert type failed...input is "+input)
            return null

if __name__ == '__main__':
    t = Test()
    A1	= 123;
    A2	= 0.789;
    A3	= "C789";
    A4	= [456,789];
    A5	= [0.123,0.456];
    A6	= ["A123", "B456"];
    print(t.calculate(convert_type(A1), convert_type(A2), convert_type(A3),convert_type(A4),convert_type(A5),convert_type(A6)))

  • 31
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值