资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python识别c函数的简单介绍

python 调用c函数里面的函数吗

若你是想调用 c 编写的DLL,可以使用ctypes调入使用;

为雁江等地区用户提供了全套网页设计制作服务,及雁江网站建设行业解决方案。主营业务为成都做网站、网站设计、雁江网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

#!/usr/bin/python

from ctypes import *

import os 

#需要使用绝对路径

extest = cdll.LoadLibrary(os.getcwd() + '/DemoC.so')

或在windows下

#!/usr/bin/python

import ctypes

import os 

if os.name == 'nt': # windows系统

_lib_name = os.getcwd() + '/DemoC.DLL'

dl200_lib = ctypes.WinDLL(dl200_lib_name)

python调用c函数

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调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.

首先在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.

#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源文件的函数名

class stdata(Structure):

_fields_ = [('pBuf', c_char_p), ('buflen', c_int)]

N=100

buf = create_string_buffer(N)

d = stdata()

d.buflen = N

d.pBuf = cast(buf, c_char_p)

n = CallMyCFunc_GetData(byref(d))

关键在于create_string_buffer创建可写buffer;cast转换为char*类型。

如何让python调用C和C++代码

要搞明白如何让python调用C/C++代码(也就是写python的extension),你需要征服手册中的Extending embedding厚厚的一章。在昨天花了一个小时看地头晕脑胀,仍然不知道如何写python的extension后,查阅了一些其他书籍,最终在Python Programming On Win32书中找到了教程。

1. 首先要明白的是,所谓的python扩展(也就是你提供给python的c/c++代码,不一定是c/c++代码,可以是其他语言写的代码)是一个dll,并且这个dll放在本机python安装目录下的DLLs目录下(譬如我机器上的路径是:F:/Program Files/Python25/DLLs),假如我们接下来要写的扩展module名为mb,python调用的代码为:import mbmb.showMsg("Python's really amazing, I kindda love it!")

2. 搭建环境,我们要使用python提供的c头文件和lib库来进行扩展的开发。

在vs 2005下点击菜单 "工具"-"选项", 打开选项对话框,选择"项目和解决方案-VC++目录", 然后在右边"显示以下内容的目录"得comboBox上选择"包含文件”,添加python的include目录(我的机器上是"F:/Program Files/Python25/include"),然后选择库文件,添加python的libs目录(我的机器上是"F:/Program Files/Python25/libs")。

既然扩展是一个dll,接下来我们要建立一个“动态链接库”工程,然后开始写代码:

#include python.h //python.h是包含python一些定义的头文件,在python的include目录下/*我的python版本是2.5, 因为安装python后它没提供debug下的lib库文件,因此你必须生成release版的dll,

想要生成dll版本的,你要到python官网上自己去下载python源代码,当然你可以继续生成release版本的dll,但dll中包含调试信息*/#pragma comment(lib, "python25.lib")//先不管static PyObject* mb_showMsg(PyObject* self, PyObject *args);/*如果你的扩展是mb,那么必须实现一个initmb函数,并且从dll中导出这个函数,但我们在python中调用import mb时,python会去dll里去调用

extern "C" __declspec(dllexport) void initmb(){/*当调用mb.showMsg("Python's really amazing, I kindda love it!")时, 相当于你告诉python我有一个showMsg函数,我们怎么告诉python去调用我们dll里的mb_showMsg函数呢?技巧就是下面的方式,定义一个字典数据结构,key = showMsg, value =mb_showMsg,METH_VARARGS是函数调用方式,仔细查手册吧*/static PyMethodDef mbMethods[] = {

{"showMsg", mb_showMsg, METH_VARARGS},

{NULL, NULL, NULL} /*sentinel,哨兵,用来标识结束*/};//告诉python我们的模块名叫mb, 模块包含的函数都在mbMethods字典里

PyObject *m = Py_InitModule("mb", mbMethods);}/*接下来实现核心功能showMsg*///第一个self参数我们用不着,具体查手册,第二个参数是python传给我们的参数,它是一个python的参数tuple

static PyObject* mb_showMsg(PyObject* self, PyObject *args){//我们的showMsg函数需要的是一个字符串参数

const char* msg = NULL;/*调用特殊参数解码python传递给我们的参数,s是string,我们传递接收参数的变量地址,

如果你的功能函数需要两个参数,在PyArg_parseTuple后面继续添加接受参数的变量地址,

这个函数的原型是类似printf的不定参数的形式

PyAPI_FUNC(int) PyArg_ParseTuple(PyObject *, const char *, ...);*/if (!PyArg_ParseTuple(args, "s", msg))

return NULL;//调用MBint r = ::MessageBox(NULL, "hello", "Caption:Form C module", MB_ICONINFORMATION | MB_OK);//返回值return Py_BuildValue("i", r);}将上面这段混杂着大量注释的代码拷贝到你的编辑器里,然后编译生成mb.dll,修改后缀成mb.pyd,然后拷贝到python的DLLs目录下,打开idle(python的交互程序),写入代码:import mbmb.showMsg("Python's really amazing, I kindda love it!")


分享文章:python识别c函数的简单介绍
本文网址:http://cdkjz.cn/article/dsehohc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220