Python调用C/C++的种种方法
Python调用C/C++的种种方法
2010-12-07 09:59 28433人阅读 评论(1) 收藏
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
Python调用C/C++的种种方法的更多相关文章
- Python调用C/C++动态链接库的方法详解
Python调用C/C++动态链接库的方法详解 投稿:shichen2014 这篇文章主要介绍了Python调用C/C++动态链接库的方法,需要的朋友可以参考下 本文以实例讲解了Python调用C/C ...
- python 调用shell命令三种方法
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...
- python调用html内的js方法
这方面资料不多,不懂html,不懂js,略懂python的我,稍微看了点html和js,好几天的摸索,终于测试成功了. PYQT+HTML利用PYQT的webview调用JS内方法 1.python调 ...
- python调用其他程序或脚本方法(转)
python运行(调用)其他程序或脚本 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地 ...
- Python调用jar包中的方法
[本文出自天外归云的博客园] 需求 最近在后台项目代码中一段自定义的AES加解密的程序在平时的测试工作中应用频繁.因为写脚本经常会需要使用,而经过各种尝试,比如jpype等,都不尽如人意.最后转换思路 ...
- python 调用内部类的两种方法
class Car:#外部类 class Door:#内部类 def open(self): print('open door') class Wheel: def run(self): print( ...
- python 调用 C++ code
本文以实例code讲解python 调用 C++的方法. 1. 如果没有参数传递从python传递至C++,python调用C++的最简单方法是将函数声明为C可用函数,然后作为C code被pytho ...
- python调用.so
python调用动态链接库的基本过程 动态链接库在Windows中为.dll文件,在linux中为.so文件.以linux平台为例说明python调用.so文件的使用方法. 本例中默认读者已经掌握动态 ...
- 【转】Python调用C语言动态链接库
转自:https://www.cnblogs.com/fariver/p/6573112.html 动态链接库在Windows中为.dll文件,在linux中为.so文件.以linux平台为例说明py ...
随机推荐
- linux中断--进程上下文和中断上下文
一.前言 中断发生以后,CPU跳到内核设置好的中断处理代码中去,由这部分内核代码来处理中断.这个处理过程中的上下文就是中断上下文. 为什么可能导致睡眠的函数都不能在中断上下文中使用呢? 首先睡眠的含义 ...
- 转载 Jquery中AJAX参数详细介绍
Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求方式 ...
- 解决pycharm无法导入本地包的问题(Unresolved reference 'tutorial')
在用scrapy(python2.7)写爬虫的时候 from tutorail.items import DmozItem 这一行死活不成功 也就是出现 Unresolved reference 't ...
- IC卡、M1卡、CPU卡、SAM卡、PSAM卡的联系与区别
一. 技术方面(非接触式IC卡) 1. 逻辑加密卡又叫存储卡,卡内的集成电路具有加密逻辑和EEPROM(电可擦除可编程只读存储器). 2. CPU卡又叫智能卡,卡内的集成电路包括中央处理器(CPU ...
- paip.c++ qt 外部dll共享库的导入以及引用
paip.c++ qt 外部dll共享库的导入以及引用 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn. ...
- Spring、Bean 的作用域
Singleton作用域(默认) 当一个bean的作用域为singleton,那么Spring Ioc容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则 ...
- cf-公式专场
A. Again Twenty Five! time limit per test 0.5 seconds memory limit per test 64 megabytes input stand ...
- 佩特来项目经验小集合(2)___组合查询存储过程,报错 "varchar JBID='' 转换成数据类型 int 时失败"
今天写一个组合查询的存储过程遇到这样一个问题:在将 varchar 值 'SELECT * FROM View_DLS_WXJD_Customer WHERE 1=1 and JBID ='' ...
- Entityframework 伪CodeFirst开发模式应用于Sqlite数据库
因为最近没有时间深入的研究EntityFramework的内部机制,所以具体的实现并不十分了解.微软最初的初衷是开发出一套通用的数据库访问逻辑,实现对Dal数据访问层的高度封装,其中就用到了工厂模式和 ...
- sql2008中时间类型问题
DATEDIFF (DD ,@sdate ,getdate() ) eg30 计算从开始日期到今天的天数 datename(weekday,@sdate) eg星期三 查询那一天是星期几 SQL Se ...