用C/C++扩展Python(Extending Python with C or C++)
2010-04-02 14:25
387 查看
用c/c++扩展python的方式有很多,比较常见的方式:1)native,2)swig,3)boost_python,这里简单介绍一下python自带的native方式。官方文档:http://docs.python.org/extending/extending.html1.简单示例: 1.1 span_system.cpp//span_system.cpp #include <Python.h>static PyObject *spam_system(PyObject *self, PyObject *args)
{ const char *command; int sts; if (!PyArg_ParseTuple(args, "s", &command)) return NULL; sts = system(command); return Py_BuildValue("i", sts); }
static PyMethodDef libmypyext_methods[]
{
{"spam_system", spam_system, METH_VARARGS, "Description"},
{0, 0}
};
PyMODINIT_FUNC initlibgongmingpyext(){Py_InitModule3("libmypyext", libmypyext_methods, "A Python Extension for Test");}
1.2 编译链接成so
g++ -o libmypyext.so -export-dynamic -m64 -shared span_system.cpp1.3 python调用#span_system.pyimport libmypyextdef spam_system(command):print libmypyext.spam_system()if __name__ == '__main__': if len(sys.argv) == 2: spam_system(sys.argv[1]) else: print '''python span_system.py your_command''' sys.exit()
相关文章推荐
- 用C/C++扩展Python(Extending Python with C or C++)
- 编写Python扩展(Extending Python with C or C++)
- Extending Python with C or C++
- Extending Python with C or C++¶
- Extending Python with C or C++
- Extending Python with C\C++ 实践问题
- python extending with c++
- Multi-Language Programming - Extending Flash Lite with Java ME or Symbian C++
- 使用c/c++扩展python
- windows下安装python的C扩展编译环境(解决“error: command 'cl.exe' failed: No such file or directory”)
- Linux安装Python-MySQL 遇到(set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
- [ZT]C++ 扩展和嵌入 Python
- Using GDI+ with MFC or native C/C++
- 用C/C++扩展Python
- 高效开发Python扩展模块方法:用SWIG封装C/C++代码
- Distutils发布C++扩展的Python模块
- C++ 扩展和嵌入 Python
- 使用C/C++扩展Python
- 用vs2008做python的C/C++扩展
- windows平台使用Microsoft Visual C++ Compiler for Python 2.7编译python扩展