东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2411|回复: 0
打印 上一主题 下一主题

[Python] python调用c/c++编译出的so库(没有使用swig)

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14437
QQ
跳转到指定楼层
楼主
发表于 2021-12-6 14:10:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式







python调用c/c++编译出的so库(没有使用swig)




  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <vector>
  5. #include <stdio.h>

  6. // 将cpp编译成so
  7. // g++ -shared -Wl,-soname,test -o test.so -fPIC test.cpp


  8. class Test{
  9.     private:
  10.         double _calculate(int a, double b);
  11.     public:
  12.         double calculate(int a, double b, char c[], int * d, double * e, char ** f);
  13. };

  14. double Test::_calculate(int a, double b){
  15.     double res = a+b;
  16.     std::cout<<"res: "<<res<<std::endl;
  17.     return res;
  18. }

  19. double Test::calculate(int a, double b, char c[], int * d, double * e, char ** f){
  20.     std::cout<<"a: "<<a<<std::endl;
  21.     std::cout<<"b: "<<b<<std::endl;
  22.     std::cout<<"c: "<<c<<std::endl;
  23.     std::cout<<"d: "<<d[0] << ","<<d[1]<<std::endl;
  24.     std::cout<<"e: "<<e[0] << ","<<e[1]<<std::endl;
  25.     std::cout<<"f: "<<f[0] << ","<<f[1]<<std::endl;
  26.     return this->_calculate(a, b);
  27. }


  28. // 封装C接口  这样lib才能访问得到 这是暴露接口给python使用
  29. extern "C"{
  30.     // 创建对象
  31.     Test* test_new(){
  32.         // 谁调这个 函数test_new()?
  33.         return new Test;
  34.     }
  35.     double my_calculate(Test* t, int a, double b, char c[], int * d, double * e, char ** f){
  36.         return t->calculate(a, b,c,d,e,f);
  37.     }
  38. }
复制代码



  1. # -*- coding: utf-8 -*-
  2. __author__ = u'东方耀 微信:dfy_88888'
  3. __date__ = '2021/12/6 下午1:36'
  4. __product__ = 'PyCharm'
  5. __filename__ = 'test'

  6. import ctypes

  7. # python调用c/c++编译出的so库(没有使用swig)

  8. # 指定动态链接库
  9. lib = ctypes.cdll.LoadLibrary('./test.so')
  10. # 需要指定返回值的类型,默认是int
  11. lib.my_calculate.restype = ctypes.c_double


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

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


  19. # 将python类型转换成c类型,支持int, float,string的变量和数组的转换
  20. def convert_type(input):
  21.     # python调用c++ py的类型需要转换为c++可以识别的类型啊
  22.     ctypes_map = {int: ctypes.c_int,
  23.                   float: ctypes.c_double,
  24.                   str: ctypes.c_char_p
  25.                   }
  26.     input_type = type(input)
  27.     if input_type is list:
  28.         length = len(input)
  29.         if length == 0:
  30.             print("convert type failed...input is " + input)
  31.             return None
  32.         else:
  33.             arr = (ctypes_map[type(input[0])] * length)()
  34.             for i in range(length):
  35.                 arr[i] = bytes(input[i], encoding="utf-8") if (type(input[0]) is str) else input[i]
  36.             return arr
  37.     else:
  38.         if input_type in ctypes_map:
  39.             return ctypes_map[input_type](bytes(input, encoding="utf-8") if type(input) is str else input)
  40.         else:
  41.             print("convert type failed...input is " + input)
  42.             return None


  43. if __name__ == '__main__':
  44.     t = Test()
  45.     A1 = 123
  46.     A2 = 0.789
  47.     A3 = "C789"
  48.     A4 = [456, 789]
  49.     A5 = [0.123, 0.456]
  50.     A6 = ["A123", "B456"]
  51.     print(t.calculate(convert_type(A1), convert_type(A2), convert_type(A3), convert_type(A4), convert_type(A5),
  52.                       convert_type(A6)))
复制代码





让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-6 02:31 , Processed in 0.173249 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表