用C/C++扩展Python(Extending Python with C or C++)
2010-07-26 13:40
495 查看
用c/c++扩展python的方式有很多,比较常见的方式:1)native,2)swig,3)boost_python,这里简单介绍一下python自带的native方式。
官方文档:http://docs.python.org/extending/extending.html
1.简单示例:
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.cpp
1.3 python调用
#span_system.py import libmypyext def 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++)
- Extending Python with C or C++¶
- Extending Python with C or C++
- Extending Python with C or C++
- 编写Python扩展(Extending Python with C or C++)
- python extending with c++
- Extending Python with C\C++ 实践问题
- Multi-Language Programming - Extending Flash Lite with Java ME or Symbian C++
- 使用Visual Studio 2008开发Python的C++扩展之准备步骤
- 使用C/C++扩展Python
- windows平台使用Microsoft Visual C++ Compiler for Python 2.7编译python扩展
- megento api soap xmlrpc接口 自定义扩展 Creating a Custom API or Extending the Core API
- Using GDI+ with MFC or native C/C++
- mac下安装mysql-python包报unable to execute llvm-gcc-4.2: No such file or directory error: command 'llvm-gcc-4.2' failed with exit status 1解决方法
- 使用C++扩展Python的功能
- 用C/C++扩展Python
- 使用C++扩展Python的功能详解
- Python 扩展使用 C/C++ 给 Python 写扩展模块的方法
- 初用C/C++扩展Python,提高性能
- C++ 扩展和嵌入 Python