博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python调用C/C++的种种方法
阅读量:2396 次
发布时间:2019-05-10

本文共 3286 字,大约阅读时间需要 10 分钟。

Python是解释性语言, 底层就是用c实现的, 所以用python调用C是很容易的, 下面就总结一下各种调用的方法, 给出例子, 所有例子都在ubuntu9.10, python2.6下试过.

1. Python 调用 C (base)

想在python中调用c函数, 如这儿的fact

#include

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

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

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 2.3 and higher. In 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

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

转载地址:http://pifob.baihongyu.com/

你可能感兴趣的文章
go语言学习初体验
查看>>
儿童世界的一点解读
查看>>
MySQL中的自增列
查看>>
Python实现工厂模式的两个例子
查看>>
逻辑,问题到底在哪里?
查看>>
不要做”积极废人”
查看>>
两个看似奇怪的MySQL语句问题
查看>>
通过shell脚本检测MySQL服务信息
查看>>
Greenplum集群部署小记
查看>>
通过java画文本格式的统计图
查看>>
MySQL存储过程的权限问题
查看>>
大分区表的手工并行优化
查看>>
从“悲剧”的几个运维场景谈谈运维开发的痛点
查看>>
运维工作中几点深刻的经验和教训
查看>>
mysql常用命令
查看>>
运维工作中的脚本化和工具化
查看>>
本周搞几件事情,说说你的计划吧
查看>>
又一个轮回(r15笔记第100天)
查看>>
RAC 单节点报ora-1105 ora-01606的解决
查看>>
多行数据的批处理之bulk collect
查看>>