python调用c函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python调用c函数相关的知识,希望对你有一定的参考价值。

c:
char * a(char *b)

return b;


求python
传参与接收代码(ctypes)
注意char *不是字符串,里面有一堆\0

Python是解释性语言, 底层就是用c实现的, 所以用python调用C是很容易的, 下面就总结一下各种调用的方法, 给出例子, 所有例子都在ubuntu9.10, python2.6下试过
1. Python 调用 C (base)
想在python中调用c函数, 如这儿的fact
#include <Python.h>

int fact(int n)

if (n <= 1)
return 1;
else
return n * fact(n - 1);


PyObject* wrap_fact(PyObject* self, PyObject* args)

int n, result;

if (! PyArg_ParseTuple(args, "i:fact", &n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);


static PyMethodDef exampleMethods[] =

"fact", wrap_fact, METH_VARARGS, "Caculate N!",
NULL, NULL
;

void initexample()

PyObject* m;
m = Py_InitModule("example", exampleMethods);


把这段代码存为wrapper.c, 编成so库,
gcc -fPIC wrapper.c -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然后在有此so库的目录, 进入python, 可以如下使用
import example
example.fact(4)

2. Python 调用 C++ (base)
在python中调用C++类成员函数, 如下调用TestFact类中的fact函数,
#include <Python.h>

class TestFact
public:
TestFact();
~TestFact();
int fact(int n);
;

int TestFact::fact(int n)

if (n <= 1)
return 1;
else
return n * (n - 1);


int fact(int n)

TestFact t;
return t.fact(n);

PyObject* wrap_fact(PyObject* self, PyObject* args)

int n, result;

if (! PyArg_ParseTuple(args, "i:fact", &n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);


static PyMethodDef exampleMethods[] =

"fact", wrap_fact, METH_VARARGS, "Caculate N!",
NULL, NULL
;

extern "C" //不加会导致找不到initexample
void initexample()

PyObject* m;
m = Py_InitModule("example", exampleMethods);


把这段代码存为wrapper.cpp, 编成so库,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然后在有此so库的目录, 进入python, 可以如下使用
import example
example.fact(4)

3. Python 调用 C++ (Boost.Python)
Boost库是非常强大的库, 其中的python库可以用来封装c++被python调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.
http://dev.gameres.com/Program/Abstract/Building%20Hybrid%20Systems%20with%20Boost_Python.CHN.by.JERRY.htm
首先在ubuntu下安装boost.python, apt-get install libboost-python-dev
#include <boost/python.hpp>
char const* greet()

return "hello, world";


BOOST_PYTHON_MODULE(hello)

using namespace boost::python;
def("greet", greet);


把代码存为hello.cpp, 编译成so库
g++ hello.cpp -o hello.so -shared -I/usr/include/python2.5 -I/usr/lib/python2.5/config -lboost_python-gcc42-mt-1_34_1

此处python路径设为你的python路径, 并且必须加-lboost_python-gcc42-mt-1_34_1, 这个库名不一定是这个, 去/user/lib查

然后在有此so库的目录, 进入python, 可以如下使用
>>> import hello
>>> hello.greet()
'hello, world'

4. python 调用 c++ (ctypes)
ctypes is an advanced ffi (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 it is already included.
ctypes allows to call functions in dlls/shared libraries and has extensive facilities to create, access and manipulate simple and complicated C data types in Python - in other words: wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.
http://python.net/crew/theller/ctypes/

#include <Python.h>

class TestFact
public:
TestFact();
~TestFact();
int fact(int n);
;

int TestFact::fact(int n)

if (n <= 1)
return 1;
else
return n * (n - 1);


extern "C"
int fact(int n)

TestFact t;
return t.fact(n);

将代码存为wrapper.cpp不用写python接口封装, 直接编译成so库,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

进入python, 可以如下使用
>>> import ctypes
>>> pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')
>>> pdll.fact(4)
12
参考技术A 我觉得会受到限制的,因为c_char_p是遵循c字符串标准的,会以NULL为结束。下面的代码只输出hello,也许真要传递内嵌NULL的,只能靠编写python扩展了,也很简单的,用swig。

from ctypes import *
import struct

example=cdll.LoadLibrary("example.dll")
s=create_string_buffer('hello\x00world')
example.test.restype=c_char_p
example.test.argtypes = [c_char_p]
r=example.test(s) #("hello\x00world")
print r追问

给个例子吧
怎么把带‘\x00’的字符串传回python?

追答

可以参考python附带的ctypes的说明文档。比如把返回类型声明为c_char*20,就会返回20个字符的数组,不过由于你不知道原来多长,所以这个20还是有问题的。估计彻底的方法只能使用Python的c API写扩展了。那样灵活度更高

本回答被提问者采纳
参考技术B 如果有一堆\0,你怎么知道这个指针指向的内容什么时候结束?比如应该读取10个字节,还是100个字节。
最起码要加一个标示指针所指内容长度的参数。

python 调用c函数

Python调用c

一个典型的Python扩展模块至少应该包含三个部分:导出函数、方法列表和初始化函数。 例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
example.c
int fact(int n)
{
        if (n <= 1)
        return 1;
        else
        return n * fact(n - 1);
}
 
wrap.c
#include <Python.h>
PyObject* wrap_fact(PyObject* self, PyObject* args)
{
        int n, result;
         
        if (! PyArg_ParseTuple(args, "i:fact", &n))
        return NULL;
        result = fact(n);
        return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
        {"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
        {NULL, NULL}
};
void initexample()
{
        PyObject* m;
        m = Py_InitModule("example", exampleMethods);
}

1. 导出函数 要在Python解释器中使用C语言中的某个函数,首先要为其编写相应的导出函数,上述例子中的导出函数为wrap_fact。在Python的C语言扩展中,所有的导出函数都具有相同的函数原型:

1
PyObject* method(PyObject* self, PyObject* args);

 

该函数是Python解释器和C函数进行交互的接口,带有两个参数:self和args。参数self只在C函数被实现为内联方法(built-in method)时才被用到,通常该参数的值为空(NULL)。参数args中包含了Python解释器要传递给C函数的所有参数,通常使用Python的C语言扩展接口提供的函数PyArg_ParseTuple()来获得这些参数值。 所有的导出函数都返回一个PyObject指针,如果对应的C函数没有真正的返回值(即返回值类型为void),则应返回一个全局的None对象(Py_None),并将其引用计数增1,如下所示:

1
2
3
4
5
PyObject* method(PyObject *self, PyObject *args)
{
        Py_INCREF(Py_None);
        return Py_None;
}

2. 方法列表 方法列表中给出了所有可以被Python解释器使用的方法,上述例子对应的方法列表为:

1
2
3
4
5
static PyMethodDef exampleMethods[] =
{
        {"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
        {NULL, NULL}
};

方法列表中的每项由四个部分组成:方法名、导出函数、参数传递方式和方法描述。方法名是从Python解释器中调用该方法时所使用的名字。参数传递方式则规定了Python向C函数传递参数的具体形式,可选的两种方式是METH_VARARGS和METH_KEYWORDS,其中METH_VARARGS是参数传递的标准形式,它通过Python的元组在Python解释器和C函数之间传递参数,若采用METH_KEYWORD方式,则Python解释器和C函数之间将通过Python的字典类型在两者之间进行参数传递。

3. 初始化函数 所有的Python扩展模块都必须要有一个初始化函数,以便Python解释器能够对模块进行正确的初始化。Python解释器规定所有的初始化函数的函数名都必须以init开头,并加上模块的名字。对于模块example来说,则相应的初始化函数为:

1
2
3
4
5
void initexample()
{
        PyObject* m;
        m = Py_InitModule("example", exampleMethods);
}

当Python解释器需要导入该模块时,将根据该模块的名称查找相应的初始化函数,一旦找到则调用该函数进行相应的初始化工作,初始化函数则通过调用Python的C语言扩展接口所提供的函数Py_InitModule(),来向Python解释器注册该模块中所有可以用到的方法。

4. 编译 在linux下,编译成so库,python可以直接import。windows下,需要将dll的后缀修改成pyd,python才能识别。

1
2
3
4
5
gcc -fpic -c -I/usr/include/python2.5 \
-I /usr/lib/python2.5/config \
example.c wrapper.c
 
gcc -shared -o example.so example.o wrapper.o

5. python调用

1
2
3
4
>>> import example
>>> example.fact(4)
24
>>>

以上是关于python调用c函数的主要内容,如果未能解决你的问题,请参考以下文章

python 调用c函数

Python 外部函数调用库ctypes简介

如何在 MicroPython 中从 C 调用 python 函数

python--ctypes模块:调用C函数

C 语言调用python 脚本函数

Arduino中调用.c文件中的函数报错