代码之家  ›  专栏  ›  技术社区  ›  monoceres

嵌入Python并向解释器中添加C函数

  •  1
  • monoceres  · 技术社区  · 15 年前

    我目前正在编写一个嵌入python解释器的应用程序。其思想是让程序调用用户指定的脚本来处理程序中的某些事件。我管理了这个部分,但现在我希望脚本能够调用我的程序中的函数。

    #include "python.h"
    
    
    static PyObject* myTest(PyObject* self,PyObject *args)
    {
        return Py_BuildValue("s","123456789");
    }
    
    static PyMethodDef myMethods[] = {{"myTest",myTest},{NULL,NULL}};
    
    int main()
    {
    
        Py_Initialize();
        Py_InitModule("PROGRAM",myMethods);
    
        PyRun_SimpleString("print PROGRAM.myTest()");
    
    
        Py_Finalize();
    }
    

    1 回复  |  直到 15 年前
        1
  •  2
  •   evilpie    15 年前

    您需要将该函数绑定到某个模块,请参见 http://docs.python.org/extending/embedding.html#extending-embedded-python

    编辑: