Python使用ctypes调用C/C++的方法
作者:JimmyU1 时间:2023-09-01 21:27:47
python使用ctypes调用C/C++
1. ctpes介绍
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
官方文档地址: https://docs.python.org/3/library/ctypes.html
2.使用ctypes调用C语言
python对于标准C的兼容做的相当不错,调用起来相对比较简单,下面给大家上案例
1. 创建一个adder.c文件,代码如下:
#include <stdio.h>
int add_int(int, int);
float add_float(float, float);
int add_int(int num1, int num2)
{
return num1 + num2;
}
float add_float(float num1, float num2)
{
return num1 + num2;
}
这是一个标准C编写的连个函数,用来计算两个整数相加和两个单精度浮点数相加
2. 编译生成.so文件供python程序调用
在linux环境下使用gcc对adder.c进行编译
命令: gcc adder.c -fPIC -shared -o adder.so
3.在python程序中调用.so
创建一个python文件,命令为adder.py,代码如下:
from ctypes import *
adder = CDLL("./adder.so")
res_int = adder.add_int(4,5)
print("result: " + str(res_int))
然后在终端里执行命令: python adder.py
执行命令之后就能获得到输出了。
3. 使用ctypes调用C++
与调用C语言不通,在调用C++代码时,需要在源文件中增加一个extern "C"
的代码块
1.创建一个adder_plus.cpp文件,代码如下:
#include <stdio.h>
#include <iostream>
using namespace std;
int add_integer(int a , int b);
float add_float(float a, float b);
int add_integer(int a , int b){
return a + b;
}
float add_float(float a, float b){
return a + b;
}
extern "C"{
int add_integer_plus(int a , int b){
return add_integer(a, b);
}
float add_float_plus(float a, float b){
return add_float(a ,b);
}
}
在extern "C"
里的代码是暴露给的python的接口,可以使用CDLL进行调用
2. 编译生成.so文件供python程序调用
在linux环境下使用gcc对adder.c进行编译
命令: gcc adder_plus.cpp -fPIC -shared -o adder_plus.so
3. 在python文件中调用.so
创建一个python文件,命名为adder_plus.py,代码如下:
from ctypes import *
add_plus = CDLL('./add_pluse.so')
sum = add_plus.add_integer_plus(4,5)
print("sum: " + str(sum))
# 使用float类型变量作为函数参数
a = c_float(5.5)
b = c_float(4.1)
add_float = add_plus.add_float_plus
add_float.restype = c_float
sum_float = add_plus.add_float_plus(a,b)
print(sum_float)
然后在终端里执行命令: python adder_plus.py
来源:https://blog.csdn.net/u012449363/article/details/75452374
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP的Yii框架中行为的定义与绑定方法讲解
Yolov5多边形标签和JSON数据格式转换
![](https://img.aspxhome.com/file/2023/4/117944_0s.jpg)
Python实现OCR识别之pytesseract案例详解
![](https://img.aspxhome.com/file/2023/2/94702_0s.png)
浅析SQL Server授予了CREATE TABLE权限但是无法创建表
![](https://img.aspxhome.com/file/2023/4/104114_0s.png)
MySQL的数据库数据备份和恢复详解
python实现12306火车票查询器
![](https://img.aspxhome.com/file/2023/8/126968_0s.jpg)
ERROR 1222 (21000): The used SELECT statements have a different number of columns
解决pyCharm中 module 调用失败的问题
![](https://img.aspxhome.com/file/2023/7/82617_0s.jpg)
python实现串口通信的示例代码
![](https://img.aspxhome.com/file/2023/7/61737_0s.jpg)
Windows11下MySQL 8.0.29 安装配置方法图文教程
![](https://img.aspxhome.com/file/2023/7/117807_0s.jpg)
在Python程序中实现分布式进程的教程
![](https://img.aspxhome.com/file/2023/5/76725_0s.png)
MySQL的存储引擎InnoDB和MyISAM
![](https://img.aspxhome.com/file/2023/3/103563_0s.webp)
Python中缓存lru_cache的基本介绍和讲解
python的多重继承的理解
![](https://img.aspxhome.com/file/2023/7/101737_0s.jpg)