如何通过Python调用CC++编写的函数进行方法解析?

2026-05-16 18:051阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计978个文字,预计阅读时间需要4分钟。

如何通过Python调用C/C++编写的函数进行方法解析?

Python 是解释性语言,底层用 C 实现,因此用 Python 调用 C 非常方便。以下是一些调用方法及示例,均在 Ubuntu 9.10、Python 2.6 下测试过:

1. Python 调用 C (base) - 使用 `ctypes` 库,直接调用 C 库函数。 python import ctypes

lib=ctypes.CDLL('/path/to/lib.so') result=lib.some_function()

2. 使用 `cffi` 库 - `cffi` 提供了更灵活的接口。 python import cffi

ffi=cffi.FFI() ffi.cdef(int some_function();) lib=ffi.dlopen('/path/to/lib.so') result=lib.some_function()

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)

如何通过Python调用C/C++编写的函数进行方法解析?

Boost库是非常强大的库, 其中的python库可以用来封装c++被python调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.

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) packagefor Python 2.3 and higher. In Python 2.5 it is alreadyincluded.

ctypes allows to call functions in dlls/shared libraries and hasextensive facilities to create, access and manipulate simple andcomplicated C data types in Python - in other words: wraplibraries in pure Python. It is even possible to implement Ccallback functions in pure Python.

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

到此这篇关于Python调用C/C++的方法解析的文章就介绍到这了,更多相关Python调用C/C++的方法内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计978个文字,预计阅读时间需要4分钟。

如何通过Python调用C/C++编写的函数进行方法解析?

Python 是解释性语言,底层用 C 实现,因此用 Python 调用 C 非常方便。以下是一些调用方法及示例,均在 Ubuntu 9.10、Python 2.6 下测试过:

1. Python 调用 C (base) - 使用 `ctypes` 库,直接调用 C 库函数。 python import ctypes

lib=ctypes.CDLL('/path/to/lib.so') result=lib.some_function()

2. 使用 `cffi` 库 - `cffi` 提供了更灵活的接口。 python import cffi

ffi=cffi.FFI() ffi.cdef(int some_function();) lib=ffi.dlopen('/path/to/lib.so') result=lib.some_function()

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)

如何通过Python调用C/C++编写的函数进行方法解析?

Boost库是非常强大的库, 其中的python库可以用来封装c++被python调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.

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) packagefor Python 2.3 and higher. In Python 2.5 it is alreadyincluded.

ctypes allows to call functions in dlls/shared libraries and hasextensive facilities to create, access and manipulate simple andcomplicated C data types in Python - in other words: wraplibraries in pure Python. It is even possible to implement Ccallback functions in pure Python.

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

到此这篇关于Python调用C/C++的方法解析的文章就介绍到这了,更多相关Python调用C/C++的方法内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!