Python调用C/C++程序

简介: 编程中会遇到调用其他语言到库,这里记录一下Python调用C++。 Python底层是C, 所以调用C还是比较方便。调用C++有些麻烦。 Python提供了ctypes, 方便将Python类型转为C类型,实现传参数、函数返回类型的对应。

编程中会遇到调用其他语言到库,这里记录一下Python调用C++。

Python底层是C, 所以调用C还是比较方便。调用C++有些麻烦。

Python提供了ctypes, 方便将Python类型转为C类型,实现传参数、函数返回类型的对应。ctypes网址:https://docs.python.org/2/library/ctypes.html

 

使用Python调用C/C++主要有三步:

(1) 编写好C/C++函数

(2) 把C/C++函数打包成库文件

(3) Python加载库文件并调用


代码记录一下:

1. pycall.h

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 class PythonTest{
 6 public:
 7     PythonTest():_is_inited(false), _num(0){
 8     
 9     }   
10 
11     int init(int num){
12         _num = num;
13         _is_inited = true;
14         printf("inited ok\n");
15         return 0;
16     }   
17     
18     int str2(char *src, char* dest, int len){
19         if (src == NULL || len <= 0){ 
20             return 0;
21         }   
22 
23         int src_len = strlen(src);
24         int num = snprintf(dest, len, "%s%s", src, src);
25         return (num < len -1)? num:0;
26     }   
27 
28     bool is_inited(){
29         printf("_num = %d\n", _num);
30         return _is_inited;
31     }   
32 
33 private:
34     bool _is_inited;
35     int _num;
36 };

 2. pycall_so.cpp

 1 #include "pycall.h"
 2 
 3 extern "C" {
 4 
 5 PythonTest py; 
 6 
 7 int init(int num){
 8     return py.init(num);
 9 }
10 
11 bool is_inited(){
12     return py.is_inited();
13 }
14 
15 int str2(char* src, char* dest, int len){
16     return py.str2(src, dest, len);
17 }
18 
19 int add(int a, int b){ 
20     return a + b;
21 }
22 
23 }

 3. pycall.py

 1 #coding=utf-8
 2 
 3 import ctypes 
 4 from ctypes import *
 5 
 6 ##加载库文件
 7 ll = ctypes.cdll.LoadLibrary  
 8 lib = ll("./libpycall.so")   
 9 
10 ##call
11 fun=lib.init    ###类似C/C++函数指针
12 fun.restype = c_int ##设置函数返回值类型
13 print fun(8);
14 print "*" * 20
15 
16 ##call
17 fun=lib.is_inited
18 fun.restype = c_bool
19 print fun();
20 print "*" * 20
21 
22 ##call
23 fun=lib.str2
24 src = "hello world "
25 dest = "*" * 30     ###申请buf, 用于保存返回结果 
26 num = fun(src, dest, len(dest)) ###传递指针作为参数
27 if num != 0:
28     print dest[:num]
29 else:
30     print "buf is not ok"
31 print "*" * 20
32 
33 ##call
34 print lib.add(1, 2); 
35 print "*" * 20

 执行结果:

 

相关文章
|
23小时前
|
监控 测试技术 持续交付
Python自动化测试代理程序可用性
总之,通过编写测试用例、自动化测试和设置监控系统,您可以确保Python自动化测试代理程序的可用性,并及时发现和解决问题。这有助于提供更可靠和高性能的代理服务。
10 4
|
2天前
|
Python
简单的 Python 计算器程序
这是一个简单的Python计算器程序,实现了加、减、乘、除功能。用户选择运算类型及输入两个数字后,程序依据选择调用相应函数进行计算并显示结果。若输入非法,程序显示错误信息。
10 3
|
5天前
|
运维 Serverless Go
Serverless 应用引擎产品使用之在阿里云函数计算中c++模板,将编译好的C++程序放进去部署如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
9 1
|
6天前
|
监控 测试技术 API
Python Web应用程序构建
【4月更文挑战第11天】Python Web开发涉及多种框架,如Django、Flask和FastAPI,选择合适框架是成功的关键。示例展示了使用Flask创建简单Web应用,以及如何使用ORM(如SQLAlchemy)管理数据库。
17 4
|
7天前
|
人工智能 数据库 开发者
Python中的atexit模块:优雅地处理程序退出
Python中的atexit模块:优雅地处理程序退出
8 3
|
9天前
|
存储 索引 Python
Python从入门到精通——1.3.1练习编写简单程序
Python从入门到精通——1.3.1练习编写简单程序
|
9天前
|
安全 编译器 C++
C++从入门到精通:3.2异常处理——掌握C++的异常处理机制,提高程序健壮性
C++从入门到精通:3.2异常处理——掌握C++的异常处理机制,提高程序健壮性
|
9天前
|
存储 IDE 编译器
C++从入门到精通:1.3.1了解IDE与C++程序的编写、编译和运行
C++从入门到精通:1.3.1了解IDE与C++程序的编写、编译和运行
|
9天前
|
存储 程序员 数据库
C++从入门到精通:1.2.2简单程序与接收用户输入
C++从入门到精通:1.2.2简单程序与接收用户输入
|
9天前
|
存储 编译器 C++
C++从入门到精通:1.2.1简单程序编写与基本操作
C++从入门到精通:1.2.1简单程序编写与基本操作