当前位置: 移动技术网 > IT编程>脚本编程>Python > Python调用C/C++的方法解析

Python调用C/C++的方法解析

2020年08月06日  | 移动技术网IT编程  | 我要评论

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) 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.

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++的方法内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网