返回首页

Python与C/C++的混合编程

时间:2018-11-01 22:47来源:未知 作者:whhq 点击:
混合编程的原理 首先要说一下Python只是一个语言规范,实际上python有很多实现:CPython是标准Python,是由C编写的,python脚本被编译成CPython字节码,然后由虚拟机解释执行,垃圾回收使用
  

混合编程的原理

首先要说一下Python只是一个语言规范,实际上python有很多实现:CPython是标准Python,是由C编写的,python脚本被编译成CPython字节码,然后由虚拟机解释执行,垃圾回收使用引用计数,我们谈与C/C++混合编程实际指的是基于CPython解释上的。除此之外,还有Jython、IronPython、PyPy、Pyston,Jython是Java编写的,使用JVM的垃圾回收,可以与Java混合编程,IronPython面向.NET平台。
python与C/C++混合编程的本质是python调用C/C++编译的动态链接库,关键就是把python中的数据类型转换成c/c++中的数据类型,给编译函数处理,然后返回参数再转换成python中的数据类型。

python中使用ctypes moduel,将python类型转成c/c++类型

首先,编写一段累加数值的c代码:

extern "C" 
{
    int addBuf(char* data, int num, char* outData);
}
int addBuf(char* data, int num, char* outData)
{
    for (int i = 0; i < num; ++i)
    {
        outData[i] = data[i] + 3;  
    }
    return num;
}

然后,将上面的代码编译成so库,使用下面的编译指令

>gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC addbuf.c -o addbuf.o

最后编写python代码,使用ctypes库,将python类型转换成c语言需要的类型,然后传参调用so库函数:

from ctypes import * # cdll, c_int
lib = cdll.LoadLibrary('libmathBuf.so')
callAddBuf = lib.addBuf
num = 4
numbytes = c_int(num)
data_in = (c_byte * num)()
for i in range(num):
    data_in[i] = i
data_out = (c_byte * num)()
ret = lib.addBuf(data_in, numbytes, data_out)  #调用so库中的函数

在C/C++程序中使用Python.h,写wrap包装接口

这种方法需要修改c/c++代码,在外部函数中处理入/出参,适配python的参数。写一段c代码将外部入参作为shell命令执行:


        #include <Python.h>
static PyObject* SpamError;
static PyObject* spam_system(PyObject* self, PyObject* args)
{
        const char* command;
        int sts;
        if (!PyArg_ParseTuple(args, "s", &command))  //将args参数按照string类型处理,给command赋值
                return NULL;
        sts = system(command); //调用系统命令
        if (sts < 0) {
                PyErr_SetString(SpamError, "System command failed");
                return NULL;
        }
        return PyLong_FromLong(sts);    //将返回结果转换为PyObject类型
}
//方法表
static PyMethodDef SpamMethods[] = {
        {"system", spam_system, METH_VARARGS,
        "Execute a shell command."},
        {NULL, NULL, 0, NULL}
};
//模块初始化函数
PyMODINIT_FUNC initspam(void)
{
        PyObject* m;
        //m = PyModule_Create(&spammodule); // v3.4
        m = Py_InitModule("spam", SpamMethods);
        if (m == NULL)
                return;
        SpamError = PyErr_NewException("spam.error",NULL,NULL);
        Py_INCREF(SpamError);
        PyModule_AddObject(m,"error",SpamError);
}

处理上所有的入参、出参都作为PyObject对象来处理,然后使用转换函数把python的数据类型转换成c/c++中的类型,返回参数按相同方式处理。比第一种方法多了初始化函数,这部分是把编译的so库当做python module所必需要做的。
python这样使用:

imoprt spam
spam.system("ls")

使用c/c++编写python扩展可以参见:http://docs.python.org/2.7/extending/extending.html

------分隔线----------------------------

  • 李老师
  • 李老师
  • 胡老师
  • 胡老师
合作伙伴
  • 武汉工程大学合作培训机构

  • 国家信息技术紧缺人才培养工程(NITE)

  • ARM公司全球授权培训中心

  • 国内首家Symbian授权培训

  • 微软全球嵌入式合作伙伴

  • Altera全球合作培训机构

在线客服
  • 客服热线:
    139-8620-3604
    159-2752-9536



  • 官方微信
  •  咨询微信二维码