1

我目前正在编写一个嵌入 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();
}

谢谢!

4

1 回答 1

2

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

编辑:基本上你的代码应该可以工作。什么不工作?

于 2010-05-11T15:08:14.107 回答