(原)python使用ctypes调用C/C++接口

发布时间:2016-12-19 12:10:13编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"(原)python使用ctypes调用C/C++接口 ",主要涉及到(原)python使用ctypes调用C/C++接口 方面的内容,对于(原)python使用ctypes调用C/C++接口 感兴趣的同学可以参考一下。

转载请注明出处:

http://www.cnblogs.com/darkknightzh/p/6135514.html

参考网址:

https://docs.python.org/2/library/ctypes.html——ctypes的官方文档

http://eli.thegreenplace.net/2008/08/31/ctypes-calling-cc-code-from-python/——提供了一个不涉及类的例子

http://stackoverflow.com/questions/145270/calling-c-c-from-python——建议使用ctypes,并提供了一个简单的例子

http://stackoverflow.com/questions/7142169/pils-image-frombuffer-expected-data-length-when-using-ctypes-array——提供了HYRY直接使用c_ubyte进行处理的例子

import Imagefrom ctypes import c_ubyte, cast, POINTERbuf = (c_ubyte * 400)()pbuf = cast(buf, POINTER(c_ubyte))pbuf2 = cast(pbuf, POINTER(c_ubyte*400))

buf is an ubyte array, pbuf is a pointer to ubyte, pbuf2 is a pointer to ubyte[400]. img1 is created from buf directly, img2 is created from pubf2.contents.

http://www.linuxidc.com/Linux/2011-10/44838.htm——传结构体的简单例子

一 传简单的指针:

具体步骤:

1. 新建mathBuf.cpp:

#include <iostream>#include "subBuf.h"extern "C" {    int addBuf(char* data, int num, char* outData);    subBuf* subBuf_new(){ return new subBuf(); }    int subBuf_sub(subBuf* subfuf, char* data, int num, char* outData){ subfuf->cursubBuf(data, num, outData); }}int addBuf(char* data, int num, char* outData){    for (int i = 0; i < num; ++i)    {        outData[i] = data[i] + 3;    }    return num;}

2. 新建subBuf.h:

#include <iostream>class subBuf{    public:        subBuf(){}        int cursubBuf(char* data, int num, char* outData)        {            for (int i = 0; i < num; ++i)            {                outData[i] = data[i] - 5;            }            return num;        }};

3. 终端中输入如下命令,生成libmathBuf.so:

g++ -std=c++11 -shared -fPIC -o libmathBuf.so mathBuf.cpp

4. 新建test.py:

from ctypes import * # cdll, c_intlib = cdll.LoadLibrary('libmathBuf.so')class callsubBuf(object):    def __init__(self):        self.obj = lib.subBuf_new()    def callcursubBuf(self, data, num, outData):        lib.subBuf_sub(self.obj, data, num, outData)callAddBuf = lib.addBufnum = 4numbytes = c_int(num)data_in = (c_byte * num)()for i in range(num):    data_in[i] = iprint("initial input data buf:")for i in range(num):    print(data_in[i])#pdata_in = cast(data_in, POINTER(c_ubyte))#pdata_in2 = cast(pdata_in, POINTER(c_ubyte*num))data_out = (c_byte * num)()ret = callAddBuf(data_in, numbytes, data_out)print("after call addBuf with C, output buf:")for i in range(num):    print(data_out[i])f = callsubBuf()f.callcursubBuf(data_in, numbytes, data_out)print("after call cursubBuf with  C++ class, output buf:")for i in range(num):    print(data_out[i])

5. 运行test.py,输出如下:

说明:

1) test.py如果使用c_byte,则对应C中的unsigned char。

2) 程序使用了2个文件,subBuf.h和mathBuf.cpp。实际上可以使用一个文件,但是class要在extern "C"的上面,否则即便声明了class subBuf,也会提示invalid use of incomplete type ‘class subBuf’:

3) addSub函数的实现要在extern "C"的下面,否则会提示error: conflicting declaration of XXX with ‘C’ linkage:

4) ctypes中对应的c和python类型如下(具体参见ctypes的官方文档):


上一篇:nginx+lua
下一篇:站在两个世界的边缘——程浩

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款