python调用c++动态库_python使用ctypes调用C/C++动态库

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

以 windows中的动态库为例(使用VS2015生成动态库,python版本为3.5):

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

C/C++函数:

#include

#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT int __stdcall add(int a, int b)

{

int c;

c = a + b;

return c;

}

python:

import ctypes

Cfun = ctypes.WinDLL('./Ccode.dll/x64/Release/Ccode_dll.dll') ##动态链接库所在目录

Num1 = ctypes.c_int(10) ##创建第一个输入参数,将参数类型指定为c_int即C语言中的int类型

Num2 = ctypes.c_int(30)

Cfun.add.restype=ctypes.c_int ##将C函数的返回值类型定位c_int即C语言中的int类型,如果不先声明类型在使用非int变量时,返回值会不对

Sum=Cfun.add(Num1,Num2)

print("Sum is %d"%Sum)

结果:

Sum is 40

类似的创建浮点数加法函数:

#include

#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT float __stdcall add_float(float a, float b)

{

return a+b;

}

import ctypes

Cfun = ctypes.WinDLL('./Ccode.dll/x64/Release/Ccode_dll.dll') ##动态链接库所在目录

Num1 = ctypes.c_float(10.2)

Num2 = ctypes.c_float(20.5)

Cfun.add_float.restype=ctypes.c_float

Sum=Cfun.add_float(Num1,Num2)

print("Sum is %f"%Sum)

结果:

Sum is 30.700001

ctypes中的变量类型与C中对应如下:

ctypes数据类型 C数据类型

c_char char

c_short short

c_int int

c_long long

c_float float

c_double double

c_void_p void

c_uint8 unsigned char

指针的使用方法:

在C中创建add_point函数:

#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT void __stdcall add_point(float* a, float* b, float* c)

{

*c = *a + *b;

*a = 129.7;

}

python:

import ctypes

Cfun = ctypes.WinDLL('./Ccode.dll/x64/Release/Ccode_dll.dll') ##动态链接库所在目录

x1 = ctypes.c_float(1.9)

x2 = ctypes.c_float(10.1)

x3 = ctypes.c_float(0)

Cfun.add_point(ctypes.byref(x1), ctypes.byref(x2), ctypes.byref(x3)) ## ctypes.byref()将参数指定为C中的指针类型

print("Sum is %f,x1 is %f" % (x3.value, x1.value)) ##value代表变量的值

结果:

Sum is 12.000000,x1 is 129.699997

可以发现在C函数中更改指针的值,在python中确实被修改了。

接收C函数返回的指针:

DLLEXPORT int* __stdcall point(int* x)

{

int* y=NULL;

y = x;

return y;

}

python:

x = ctypes.c_int(2560)

Cfun.point.restype = ctypes.POINTER(ctypes.c_int) ##声明函数返回值为int*

y = Cfun.point(ctypes.byref(x))

print("y is %d" % y[0])

结果:

y is 2560

数组的使用方法:

在C中创建array函数:

#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT void __stdcall array(int x[])

{

printf("x[0]= %d x[1]=%d x[2]=%d x[3]=%d \n", *x,x[1],x[2],x[3]);

*x = 100;

}

python:

import ctypes

Cfun = ctypes.WinDLL('./Ccode.dll/x64/Release/Ccode_dll.dll') ##动态链接库所在目录

Array = ctypes.c_int * 4; ##声明一维数组,数组长度为4

a = Array(0, 1, 2, 3) ##初始化数组

Cfun.array(a)

print(a[0], a[1], a[2], a[3])

结果:

x[0]= 0 x[1]=1 x[2]=2 x[3]=3

100 1 2 3

在C中修改数组的值,在python中确实被修改。

声明二维数组的方法:

Array = (ctypes.c_int * 4)*5 ##声明二维数组

a=Array()

###使用循环对其进行赋值

for i in range(5):

for j in range(4):

a[i][j]=i*j

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值