网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python调用C虚函数 python有没有虚函数

怎样让Python脚本与C++程序互相调用

二、Python调用C/C++\x0d\x0a\x0d\x0a\x0d\x0a1、Python调用C动态链接库\x0d\x0a\x0d\x0a Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。\x0d\x0a(1)C语言文件:pycall.c\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0a/***gcc -o libpycall.so -shared -fPIC pycall.c*/ \x0d\x0a#include \x0d\x0a#include \x0d\x0aint foo(int a, int b) \x0d\x0a{ \x0d\x0a printf("you input %d and %d\n", a, b); \x0d\x0a return a+b; \x0d\x0a} \x0d\x0a(2)gcc编译生成动态库libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern "C"来进行编译。\x0d\x0a(3)Python调用动态库的文件:pycall.py\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0aimport ctypes \x0d\x0all = ctypes.cdll.LoadLibrary \x0d\x0alib = ll("./libpycall.so") \x0d\x0alib.foo(1, 3) \x0d\x0aprint '***finish***' \x0d\x0a(4)运行结果:\x0d\x0a\x0d\x0a\x0d\x0a2、Python调用C++(类)动态链接库 \x0d\x0a\x0d\x0a 需要extern "C"来辅助,也就是说还是只能调用C函数,不能直接调用方法,但是能解析C++方法。不是用extern "C",构建后的动态链接库没有这些函数的符号表。\x0d\x0a(1)C++类文件:pycallclass.cpp\x0d\x0a\x0d\x0a[html] view plain copy \x0d\x0a#include \x0d\x0ausing namespace std; \x0d\x0a \x0d\x0aclass TestLib \x0d\x0a{ \x0d\x0a public: \x0d\x0a void display(); \x0d\x0a void display(int a); \x0d\x0a}; \x0d\x0avoid TestLib::display() { \x0d\x0a cout \x0d\x0ausing namespace std; \x0d\x0aint test() \x0d\x0a{ \x0d\x0a int a = 10, b = 5; \x0d\x0a return a+b; \x0d\x0a} \x0d\x0aint main() \x0d\x0a{ \x0d\x0a cout \x0d\x0a#include \x0d\x0a#include \x0d\x0a \x0d\x0aint fac(int n) \x0d\x0a{ \x0d\x0a if (n

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的大埔网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

回答于 2022-11-16

如何让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函数

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虚函数 python有没有虚函数
标题路径:http://bjjierui.cn/article/dopecii.html

其他资讯