700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python编写dll_python调用c/c++写的dll

python编写dll_python调用c/c++写的dll

时间:2021-05-21 12:14:56

相关推荐

python编写dll_python调用c/c++写的dll

首先,作者推荐了Cython可以作为一个更好的C的python封装,我没去用,直接用vs的工具来生成如下dll吧

1,编写源码

C程序

//test.c

__declspec(dllexport) int sum(int a, int b) {

return a + b;

}

C++

//test.cpp

#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT int sum(int a, int b) {

return a + b;

}

windows用户__declspec(dllexport)是必需的,也能使程序不需要.def文件就能访问,其实我不懂啥意思,Linux用户可以忽略掉此前缀。

C++的前缀比C又多了截extern "C",是为了避免编译器添加额外的东西,所以如果是做一个C++的dll的话,也是必须的,其实你照抄前缀就可以了。

其实大多数示例都是直接写成extern "C" __declspec(dllexport) int sum(int a, int b) 这样的,这只是可读性的问题。

还可以添加一个头文件,类似C#的接口,想省事就不要

//test.h

int sum(int, int);

2,编译成DLL/SO

既然是教你们直接生成dll,那么当然不是建一个类库项目,直接用vs提供的命令行工具吧,导航到test.c/test.cpp目录,然后执行:

>cl /LD test.c

[...]

/out:test.dll

/dll

/implib:test.lib

test.obj

Creating library test.lib and object test.exp

Note: cl use the file extension (.c or .cpp) to know if the source is written in C or C++.

Linux用户用gcc/g++生成一个.so文件:

gcc -Wall -Wextra -O -ansi -pedantic -shared test.c -o test.so

Note: the -shared option is responsible to create the .so.

Note: You can also use Dependency Walker or similar programs to see the list of the exported functions and check if the sum function is there.

以上未测试

3, 用ctypes模块访问dll/so

从python2.5起已经默认包含了ctypes,否则请自行安装(easyinstall或pip,或下载)

>>> from ctypes import cdll

>>> mydll = cdll.LoadLibrary('test.dll')

>>> mydll

windows在当前目录自动搜索,Linux请传入路径:

>>> from ctypes import cdll

>>> mydll = cdll.LoadLibrary('/home/wolf/test.so')

>>> mydll

测试:

>>> mydll.sum

<_FuncPtr object at 0x00AF6918>

>>> mydll.sum(5, 3)

>>> 8

==好了,以上是原文,我的问题如下:

1, 因为开发环境是x64,也就装了64位版的python,结果老是失败,装回32位版的,以上测试直接通过,绝对是64位的原因,哪怕这个dll是在我的64位机器上编译的。几台装了x64的python都没有调成功

2, 下面是这样的一个C方法,需要传入一个int数组和一个字符数组

int CCaptionApp(int times,int length, int *ids,char *message)

{

int n,m=0;

for(n=0;n

{

m+=ids[n];

}

return m;

}

经实测,字符数组直接传string即可,int数组我也想如法炮制:

>>> from ctypes import cdll

>>> mydll = CDLL('test.dll') #顺便演示另一种加载方式,书写快一些

>>> aptionApp(1,3,[3,4,5],'hello')

输出:

Traceback (most recent call last):

File "", line 1, in

ctypes.ArgumentError: argument 3: : Don't know how to convert parameter 3看样子这个巧是不能取的(BTW,.net直接用int[]调即可),查了下文档,其中的15.17.1.13 Array一节:

>>> from ctypes import *

>>> TenIntegers = c_int * 10

>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

>>> print ii

>>> for i in ii: print i,

...

1 2 3 4 5 6 7 8 9 10

>>>

那就是要我们用长度构造一个数组,OK,测试:

>>> from ctypes import *

>>> mydll = CDLL('test.dll')

>>> aptionApp(1,3,(c_Int*3)(3,4,5),'hello')

>>> 12

成功

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。