PYTHON 与C相互交互调用实例解析
2004-11-05 20:05
781 查看
PYTHON 与C相互交互调用实例解析
使用前工具:Vc++编译器Python 解释器如没有装VC,可以去微软网站下一个C++的编译器,地址如下:http://download.microsoft.com/download/3/9/b/39bac755-0a1e-4d0b-b72c-3a158b7444c4/VCToolkitSetup.exe 装完后,在环境变量中把PYTHON的INCLUDE和LIBS分别加入下面2个宏INCLUDELIB 1、C中调用PYTHON#include <Python.h> int main(int argc, char *argv[]) { Py_Initialize(); PyRun_SimpleString("from time import time,ctime/n" "print 'Today is',ctime(time())/n"); Py_Finalize(); return 0; } 直接用CL 文件名 编译就是 2、用C生成DLL,用PYTHON调用C代码:如FOO.C#include <Python.h>/* Define the method table. */static PyObject *foo_bar(PyObject *self, PyObject *args);static PyMethodDef FooMethods[] = {{"bar", foo_bar, METH_VARARGS},{NULL, NULL}};/* Here's the initialization function. We don't need to do anythingfor our own needs, but Python needs that method table. */void initfoo(){(void) Py_InitModule("foo", FooMethods);}/* Finally, let's do something ... involved ... as an example function. */static PyObject *foo_bar(PyObject *self, PyObject *args){char *string;int len;if (!PyArg_ParseTuple(args, "s", &string))return NULL;len = strlen(string);return Py_BuildValue("i", len);}C定义文件:foo.defEXPORTSinitfoo编译生成foo.dllCl -c foo.c;link foo.obj /dll /def:foo.def /OUT:foo.dll 在PYTHON中调用:Import fooDir(foo)…即可以看到结果:>>> import foo>>> dir(foo)['__doc__', '__file__', '__name__', 'bar']>>> ^Z (注:文章然简单,但很适合初学者,还请笑纳 :) )
使用前工具:Vc++编译器Python 解释器如没有装VC,可以去微软网站下一个C++的编译器,地址如下:http://download.microsoft.com/download/3/9/b/39bac755-0a1e-4d0b-b72c-3a158b7444c4/VCToolkitSetup.exe 装完后,在环境变量中把PYTHON的INCLUDE和LIBS分别加入下面2个宏INCLUDELIB 1、C中调用PYTHON#include <Python.h> int main(int argc, char *argv[]) { Py_Initialize(); PyRun_SimpleString("from time import time,ctime/n" "print 'Today is',ctime(time())/n"); Py_Finalize(); return 0; } 直接用CL 文件名 编译就是 2、用C生成DLL,用PYTHON调用C代码:如FOO.C#include <Python.h>/* Define the method table. */static PyObject *foo_bar(PyObject *self, PyObject *args);static PyMethodDef FooMethods[] = {{"bar", foo_bar, METH_VARARGS},{NULL, NULL}};/* Here's the initialization function. We don't need to do anythingfor our own needs, but Python needs that method table. */void initfoo(){(void) Py_InitModule("foo", FooMethods);}/* Finally, let's do something ... involved ... as an example function. */static PyObject *foo_bar(PyObject *self, PyObject *args){char *string;int len;if (!PyArg_ParseTuple(args, "s", &string))return NULL;len = strlen(string);return Py_BuildValue("i", len);}C定义文件:foo.defEXPORTSinitfoo编译生成foo.dllCl -c foo.c;link foo.obj /dll /def:foo.def /OUT:foo.dll 在PYTHON中调用:Import fooDir(foo)…即可以看到结果:>>> import foo>>> dir(foo)['__doc__', '__file__', '__name__', 'bar']>>> ^Z (注:文章然简单,但很适合初学者,还请笑纳 :) )
相关文章推荐
- PYTHON 与C相互交互调用实例解析
- PYTHON 与C相互交互调用实例解析
- Python实例浅谈之三Python与C/C++相互调用
- Python实例浅谈之三Python与C/C++相互调用
- JAVA 与 Python 脚本间相互调用实例
- Python实例浅谈之三Python与C/C++相互调用
- Python接口(3):Python实例浅谈之三Python与C/C++相互调用
- Python实例浅谈之三Python与C/C++相互调用
- Python实例浅谈之三Python与C/C++相互调用
- Python实例浅谈之三Python与C/C++相互调用
- Python实例浅谈之三Python与C/C++相互调用
- Python实例浅谈之三Python与C/C++相互调用
- Python PDF 文件解析及二次处理 实例
- Python与C之间的相互调用(Python C API及Python ctypes库)
- 朴素贝叶斯算法原理及Spark MLlib调用实例(Scala/Java/Python)
- python KMeans算法实例(调用python库以及自己实现Kmeans)
- 实例解析Ruby程序中调用REXML来解析XML格式数据的用法
- 关于云打码平台的'PythonHttp'调用实例的使用
- scala---文档主题生成模型(LDA)算法原理及Spark MLlib调用实例(Scala/Java/python)
- Python中使用ConfigParser解析ini配置文件实例