浏览 3928 次
锁定老帖子 主题:python c++ extension
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-02-16
最后修改:2011-02-16
并自己根据例子写了一下效果还不错,以下代码供大家学习:) 我本人对c不大了解,欢迎大家,分析install 流程 :) 执行结果: 编写代码spam.cxx /* How to use : import spam status = spam.system("ls -l") dome is C++ Extension */ #define VERSION "1.2a3" #if defined(_MSC_VER) #define WINDOWS_LEAN_AND_MEAN #include <windows.h> #endif #include "Python.h" #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x01060000 #if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE) /* defining this enables unicode support (default under 1.6a1 and later) */ #define HAVE_UNICODE #endif #endif /* Function Define and AttributesDefine */ static PyObject *CmdError; static PyObject * cmd(PyObject *self, PyObject *args) { const char *command; int sts; /* int PyArg_ParseTuple(PyObject *args, const char *format, ...) return true or false | exception */ if (!PyArg_ParseTuple(args, "s", &command)) { PyErr_SetString(CmdError, "System command failed"); return NULL; } else{ printf("run command is:"); puts(command); //or use PyObject_Print for debug } sts = system(command); //PyObject* Py_BuildValue(const char *format, ...) return Py_BuildValue("i", sts); } static PyMethodDef CmdMethods[] = { {"cmd", (PyCFunction) cmd, METH_VARARGS}, {NULL, NULL} }; /* Initial Modules */ PyMODINIT_FUNC initspam(void) { PyObject *m; m = Py_InitModule("spam", CmdMethods); if (m == NULL) return; /* PyObject* PyErr_NewException(char *name, PyObject *base, PyObject *dict) */ CmdError = PyErr_NewException("system.error", NULL, NULL); Py_INCREF(CmdError); PyModule_AddObject(m, "error", CmdError); }; extern "C" DL_EXPORT(void) initaggdraw(void) { /* Initialize the Python interpreter. Required. */ Py_Initialize(); /* Add a static module */ initspam(); PyObject* g = PyDict_New(); PyDict_SetItemString(g, "__builtins__", PyEval_GetBuiltins()); PyRun_String("print 'welcome!'\n", Py_file_input, g, NULL); } setup.py from distutils.core import setup, Extension moduleSpam = Extension('spam',sources = ['spammodule.cxx']) setup(name = 'Spam system', version = '1.0', description = 'This is a demo package for run system command', ext_modules = [moduleSpam]) 跟踪了一下安装过程,发现setup 查找Setup.local文件: /* 在 Setup.local 文件中我们可以指定输出 */ spam spammodule.so :execve("/usr/bin/python", ["python", "setup.py", "install"], [/* 57 vars */]) = 0 104:stat("/usr/bin/Modules/Setup", 0x7fff47082070) = -1 ENOENT (No such file or directory) 419:stat("/usr/local/lib64/python2.6/site-packages/setuptools-0.6c11-py2.6.egg", {st_mode=S_IFREG|0644, st_size=333581, ...}) = 0 446:open("/usr/local/lib64/python2.6/site-packages/setuptools.pth", O_RDONLY) = 5 450:read(5, "./setuptools-0.6c11-py2.6.egg\n", 8192) = 30 469:stat("/usr/local/lib64/python2.6/site-packages/setuptools-0.6c11-py2.6.egg", {st_mode=S_IFREG|0644, st_size=333581, ...}) = 0 470:open("/usr/local/lib64/python2.6/site-packages/setuptools-0.6c11-py2.6.egg", O_RDONLY) = 4 2738:lstat("/home/bmc/test/ct/Modules/setup.py", {st_mode=S_IFREG|0644, st_size=262, ...}) = 0 2739:stat("setup.py", {st_mode=S_IFREG|0644, st_size=262, ...}) = 0 2740:open("setup.py", O_RDONLY) = 3 2745:read(3, "from distutils.core import setup"..., 240) = 240 2749:stat("setup.py", {st_mode=S_IFREG|0644, st_size=262, ...}) = 0 2750:open("setup.py", O_RDONLY) = 3 2756:read(3, "from distutils.core import setup"..., 4096) = 262 2853:read(4, "\0\0\0error in %s setup command: %s"..., 4096) = 3617 3915:stat("setup.cfg", 0x7fff47083e70) = -1 ENOENT (No such file or directory) 3999:stat("/usr/bin/Modules/Setup.dist", 0x7fff4707c980) = -1 ENOENT (No such file or directory) 4000:stat("/usr/bin/Modules/Setup.local", 0x7fff4707c980) = -1 ENOENT (No such file or directory) 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-02-27
a handy solution is to use swig or boost.python(for c++ extension only) to wrap your 3rd libs...
|
|
返回顶楼 | |
发表时间:2011-03-04
的确,目前我写些简单的代码,用boost很方便。
我最近在学的Gaudi.Python,好像也很牛。 大致是用c++写好算法,然后可以用python来调用。 大家感兴趣可以看看:https://lhcb-comp.web.cern.ch/lhcb-comp/ |
|
返回顶楼 | |